4 Regression

Learn how regression predicts numeric targets, how linear models are fitted and interpreted, and how to evaluate their performance on unseen data.

basics

is a supervised learning method for predicting a numeric value, such as a home's sale price, a trip's duration, or tomorrow's temperature. A dataset contains examples, each with one or more features (input values) and a numeric (the value to predict). A model learns a relationship between features and from training data, then uses that relationship to predict targets for new examples.

A common starting point is . With one , the prediction can be written as:

predicted target=intercept+slope×feature\text{predicted target} = \text{intercept} + \text{slope} \times \text{feature}

With several features, the model adds a weighted contribution from each one. It learns an and coefficients (weights) by fitting predictions to the training examples. chooses coefficients that minimize the sum of squared differences between actual and predicted targets.

For example, a model predicting a home's price from floor area might use:

predicted price=$50,000+$250×area in square feet\text{predicted price} = \$50{,}000 + \$250 \times \text{area in square feet}

According to this model, an additional square foot is associated with a $250\$250 increase in predicted price. For a 1,0001{,}000-square-foot home, the prediction is $300,000\$300{,}000. The is the prediction at zero square feet; it may have little practical meaning if that value is outside the data's useful range. These are model relationships, not proof that changing a causes the to change.

Training and testing

Split labeled data into a and a . Fit the model using only the , and keep the separate so it can estimate performance on examples the model did not train on. In practice, preprocessing steps and choices made during model development should also avoid using test-set information.

This Python example assumes X is a table of numeric features and y contains the corresponding numeric targets:

fit learns the model parameters; predict applies the fitted model to new rows. Compare test-set predictions with the actual targets to assess how well the model generalizes.

Residuals and prediction errors

A is the actual minus the predicted :

residual=actual target−predicted target\text{residual} = \text{actual target} - \text{predicted target}

A positive means the model predicted too low; a negative means it predicted too high. Looking at residuals can reveal patterns, such as consistently larger errors for expensive homes or errors that curve with floor area. Such patterns may indicate that the model is missing an important relationship.

Evaluating metrics

is the average absolute prediction error. It is expressed in the 's units, so an MAE of $20,000\$20{,}000 means predictions are off by $20,000\$20{,}000 on average in absolute terms. is also expressed in units, but it penalizes large errors more strongly than MAE.

compares prediction errors with the variation around the 's mean. A value of 11 indicates perfect predictions; 00 means performance equivalent to predicting the test-set mean; and a value below 00 means worse performance than that baseline. R² alone does not show the typical size of an error, so it is useful to consider it alongside an error metric.

Generalization and model fit

A model that fits the training examples extremely well may still perform poorly on unseen data; this is . Compare training and test performance, and prefer a model that performs well on new data rather than one that merely memorizes the .

A straight-line model can also miss a curved relationship. patterns and performance on held-out data help identify when the chosen model may be inadequate. Taken together, test performance and patterns help show whether a model is useful beyond its training data.