Regression
Regression predicts a numerical target from one or more predictors. Learn simple linear regression, ordinary least squares, residuals, and SSE.
5 min read · Updated August 8, 2026
Regression is the data science task of predicting the value of a numerical target — price, temperature, hours played, blood pressure — from one or more predictors. Where classification assigns a label from a finite set, regression outputs a number from a continuous range.
Every regression method follows the same template: assume a functional form relating the predictors to the target, fit it to historical data by minimizing a measure of error, then use the fitted function to score new cases. The methods in this group differ in the functional form they assume and in the error they minimize.
Simple linear regression
The simplest useful regression model relates a single predictor to the target with a straight line:
Fitting the model means choosing the intercept and slope so the line passes as close as possible to the observed points. Ordinary least squares (OLS) defines “as close as possible” as minimizing the sum of squared errors — the squared vertical distances between each observed and its prediction :
Setting the derivatives of SSE with respect to and to zero yields the normal equations, which have a closed-form solution:
The slope is the covariance of and scaled by the variance of : how much moves, on average, when moves by one unit. Squaring the errors makes OLS sensitive to outliers — a point far from the line dominates the fit.
Worked example
Five observations of a predictor and target , with and :
| 1 | 2 | −2 | −2 | 4 | 4 |
| 2 | 4 | −1 | 0 | 0 | 1 |
| 3 | 5 | 0 | 1 | 0 | 0 |
| 4 | 4 | 1 | 0 | 0 | 1 |
| 5 | 5 | 2 | 1 | 2 | 4 |
| Sum | 6 | 10 |
Plugging the sums into the normal equations:
The fitted line is . Its residuals and quality:
| 1 | 2 | 2.8 | −0.8 | 0.64 |
| 2 | 4 | 3.4 | 0.6 | 0.36 |
| 3 | 5 | 4.0 | 1.0 | 1.00 |
| 4 | 4 | 4.6 | −0.6 | 0.36 |
| 5 | 5 | 5.2 | −0.2 | 0.04 |
, and since the total sum of squares is , the model explains of the variance — 60%.
Try it: regression fitter
Drag the line yourself and watch the residuals and SSE respond; then compare your fit against the closed-form optimum:
The regression toolbox
Linear regression is one member of a family. The pages in this group cover the classic alternatives:
- Multiple linear regression — OLS with many predictors, in matrix form.
- Decision tree regression — splits the data by standard deviation reduction and predicts the leaf average.
- k-NN regression — predicts the average target of the k most similar cases.
- Support vector regression — fits the flattest line inside an ε-insensitive tube.
- Neural networks — learn a non-linear function end to end.
In practice
sklearn.linear_model.LinearRegression is OLS with any number of predictors, and Ridge/Lasso add regularization that shrinks coefficients to fight overfitting. For most tabular problems today, gradient-boosted trees (XGBoost, LightGBM) outperform a single tree or a linear model, but OLS remains the baseline every regressor is judged against and the model you reach for when you need interpretable coefficients. Evaluate regression models with MAE and RMSE rather than raw SSE so error is in target units — see model evaluation for regression.
Common pitfalls
- Extrapolating beyond the range of the training data — the line keeps going, but the relationship usually does not.
- Ignoring outliers, which OLS amplifies through the squared error; inspect a residual plot before trusting the fit.
- Confusing correlation with causation — a high says nothing about why moves.
- Forcing a line through curved data; transform variables or use a non-linear method when residuals show systematic patterns.
- Reading alone; a model can explain variance and still predict poorly on new data. Always validate on a holdout set.
Summary
Regression predicts a numerical target from a fitted function. Simple linear regression fits the line by ordinary least squares, with the normal equations giving the slope and intercept in closed form. Residuals and SSE measure fit quality, and the other pages in this group extend the idea to many predictors, trees, neighbors, and margins.