Regression
Predicting a number: the setup, the loss, and how to read errors.
Lesson 20 of 6610 minBeginner
You will learn to
- Define regression with inputs, target and loss
- Read residuals
- Pick a first regression metric
Before this lesson
Intuition
Regression is the task of predicting a number. How much will this diamond sell for? How many units will the shop sell next week? How long will the delivery take? What these questions share is that the answer lies on a continuous scale, not in a list of categories.
Every regression problem has the same parts:
- Inputs (also called features): the columns you know when you predict, such as the weight, cut and colour of a diamond.
- Target: the number you want to predict, such as the price.
- Model: a rule that turns inputs into a predicted number.
- Loss: a measure of how wrong a prediction is. Training means finding the model that makes the total loss on the training rows small. The loss functions lesson shows how.
The simplest model is a straight line. With one input it says "each extra unit of the input adds this much to the prediction". Many real models are more flexible, but they all answer to the same question: how far are the predictions from the true values?
Picture it
A point above the line has a positive residual (the model predicted too low). A point below has a negative residual. A good fit has residuals that are small and that show no pattern: some above, some below, with no stretch of the line where they all lean the same way. A visible curve in the residuals means the model is missing structure.
The math
For a row with actual value and prediction the residual is
For a straight line with one input, , where is the intercept and the slope. The classic way to choose them is least squares: pick the values that make the sum of squared residuals smallest.
Three metrics summarise the residuals over rows:
- MAE (mean absolute error) is the average size of the error, in the units of the target.
- RMSE (root mean squared error) is also in the units of the target, but gives more weight to large errors.
- R squared compares the model with the simplest possible predictor, always guessing the mean . A value of 1 is a perfect fit, 0 means no better than the mean, and negative values mean worse than the mean.
For a first metric, use MAE if you want an answer you can explain to anyone ("on average we are off by 220 dollars") and RMSE if large mistakes are costly. The lesson on regression metrics goes deeper.
Worked example
These five diamonds are made up for illustration (the price is in thousands of dollars):
| Carat | Price | Predicted | Residual |
|---|---|---|---|
| 0.5 | 1.2 | 1.33 | -0.13 |
| 0.7 | 2.4 | 2.32 | 0.08 |
| 1.0 | 4.2 | 3.82 | 0.38 |
| 1.2 | 4.4 | 4.82 | -0.42 |
| 1.5 | 6.4 | 6.31 | 0.09 |
Least squares gives the line price = -1.17 + 4.99 × carat: each extra carat adds about 5 thousand dollars to the prediction. The residuals add up to 0, which is always true for a least-squares line with an intercept.
- MAE = (0.13 + 0.08 + 0.38 + 0.42 + 0.09) / 5 = 1.10 / 5 = 0.22, so the model is off by about 220 dollars on average.
- RMSE: the squared residuals are 0.016, 0.006, 0.145, 0.174 and 0.008, which add up to 0.348. Divided by 5 that is 0.0696, and the square root is 0.26. It is larger than the MAE because of the two bigger residuals.
- A baseline. Always predicting the mean price, 3.72, gives an MAE of 1.54 and an RMSE of 1.79. The line is far better than the baseline.
- R squared = 1 - 0.348 / 15.97 = 0.98.
With five points such a high R squared says little: any line through so few points looks good. On real data you always evaluate on rows the model has not seen, with cross-validation.
Common mistakes
- Judging a regression by a single number. An RMSE is hard to read alone. Compare it with a baseline such as the mean and look at the residual plot.
- Evaluating on the training rows. The training error is optimistic. Use validation folds or a hold-out set.
- Ignoring the units. An RMSE of 300 is excellent for house prices and terrible for a temperature. Always read the error in the units of the target.
- Letting a few outliers drive the fit. Squared errors let one extreme value pull the line. Check the largest residuals and decide whether they are errors in the data or real cases.
- Predicting beyond the range of the training data. A line keeps going up forever, but the real relation may not. Be careful with inputs far outside what the model has seen.
Try it in mlflow
Train a regression pipeline
- Open Pipelines and choose New pipeline.
- Set Task to Regression, pick a dataset (the Price regression use case uses a diamonds sample) and choose the price column as the Target column.
- Open the pipeline, go to the Models part and pick a few algorithms.
- Train, then open the run and read the leaderboard, which states the metric it is ranked by.