Regression & Classification
Predicting numbers vs deciding categories
Two canonical ML tasks: predict a number (regression) or predict a category (classification). Understanding their differences, loss functions and evaluation gates is the base layer for all supervised AI work.
What you will learn
- Choose regression vs classification from the output type
- Explain the loss functions each optimizes
- Pick evaluation metrics that match the business decision
What is this?
Supervised ML splits into two workhorses. Regression predicts a continuous number — price, latency, load. Classification assigns one of a set of categories — spam/not-spam, sentiment, intent. The architecture differs (last-layer shape and loss), but the pipeline is identical; the defining difference is the output type and the metric that judges it.
Why do you need it?
- Mixing them up silently (classifying a price, regressing a category) produces nonsense metrics.
- Regression optimizes distance (MSE/MAE) — sensitive to outliers; classification optimizes a probability/decision.
- The metric you choose changes the model: MAE vs RMSE favors different errors.
- Most ML bugs in production trace to the wrong task framing, not the math.
Mental model
A scale vs a menu
Regression vs classification
A scale returns a continuous weight (regression); a menu returns one of a fixed set of dishes (classification). Same ingredients, different output — you measure each with different tools.
How it works
- Regression models output a real number; the loss (MSE, MAE, Huber) penalizes distance to the true target.
- Classification models output logits → softmax probabilities over classes; cross-entropy loss trains them.
- Binary classification is a special case: one probability, threshold to decide the class.
- Feature pipelines are usually identical; only the head, loss and metric change.
- Evaluation: regression uses R²/MAE/RMSE; classification uses accuracy, precision/recall, ROC-AUC.
From input to number or class.
Examples
Framing the task changes everything
from sklearn.linear_model import LinearRegression, LogisticRegressionfrom sklearn.metrics import mean_absolute_error, f1_score # regression: predict a pricereg = LinearRegression().fit(X_train, y_price_train)# classification: predict buy vs notclf = LogisticRegression().fit(X_train, y_buy_train) print(mean_absolute_error(y_test, reg.predict(X_test)))print(f1_score(y_test, clf.predict(X_test)))The same features feed two different heads and two different metrics; framing the target correctly is the whole game.
In production
- Cost projection regresses next-month spend; a holding-call classifier decides 'will churn'.
- Content safety thresholds a toxicity probability at the precision point the product demands.
- Recommendation re-ranking regresses engagement score; then a threshold decides what's shown.
Best practices
- Frame by output: continuous value → regression; discrete set → classification.
- Optimize the loss the metric cares about (MAE for robust, MSE for heavy-outlier).
- For classification, threshold on cost — tune the cut for precision/recall balance, not default 0.5.
- Use calibration (probability → honest confidence) if downstream takes probabilities.
Common mistakes
- Regressing a count or category and calling a rounded value a 'class'.
- Choosing accuracy on an imbalanced classifier and declaring victory.
- Judging a regression with classification metrics and vice versa.
- Ignoring outliers when optimizing MSE — a few big errors dominate the fit.
Common issues
Regression metrics terrible on a clean-looking taskOutliers dominating the loss or wrong encoding
Use Huber/MAE or clean the targets; inspect residual plots.
Classifier over-confident on every inputFeature leakage or an unbalanced training signal
Check calibration and inspect which features drive the logits.
Great offline AUC, bad in productionDistribution shift between eval and serving distributions
Monitor feature drift and retrain the decision threshold on serving data.
Debugging tips
- A 'classifier' outputting fractional values — you built a regressor under a class name.
- Probability all at 0.999 — calibration or feature leakage, check logits and inputs.
- Compare MAE vs RMSE; a wide gap flags outlier-driven errors.
Interview checkpoint
Quick quiz
Which task predicts a continuous number?
Cross-entropy is the canonical loss for classification.