Skip to content
Saed Sayad

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 xx to the target yy with a straight line:

y^=β^0+β^1x\hat{y} = \hat{\beta}_0 + \hat{\beta}_1 x

Fitting the model means choosing the intercept β^0\hat{\beta}_0 and slope β^1\hat{\beta}_1 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 yiy_i and its prediction y^i\hat{y}_i:

SSE=i=1n(yiy^i)2=i=1nei2SSE = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 = \sum_{i=1}^{n} e_i^2

Setting the derivatives of SSE with respect to β^0\hat{\beta}_0 and β^1\hat{\beta}_1 to zero yields the normal equations, which have a closed-form solution:

β^1=i=1n(xixˉ)(yiyˉ)i=1n(xixˉ)2,β^0=yˉβ^1xˉ\hat{\beta}_1 = \frac{\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^{n} (x_i - \bar{x})^2}, \qquad \hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x}

The slope is the covariance of xx and yy scaled by the variance of xx: how much yy moves, on average, when xx 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 xx and target yy, with xˉ=3\bar{x} = 3 and yˉ=4\bar{y} = 4:

xxyyxxˉx - \bar{x}yyˉy - \bar{y}(xxˉ)(yyˉ)(x-\bar{x})(y-\bar{y})(xxˉ)2(x-\bar{x})^2
12−2−244
24−1001
350100
441001
552124
Sum610

Plugging the sums into the normal equations:

β^1=610=0.6,β^0=40.6×3=2.2\hat{\beta}_1 = \frac{6}{10} = 0.6, \qquad \hat{\beta}_0 = 4 - 0.6 \times 3 = 2.2

The fitted line is y^=2.2+0.6x\hat{y} = 2.2 + 0.6x. Its residuals and quality:

xxyyy^\hat{y}eee2e^2
122.8−0.80.64
243.40.60.36
354.01.01.00
444.6−0.60.36
555.2−0.20.04

SSE=2.4SSE = 2.4, and since the total sum of squares is SST=(yiyˉ)2=6SST = \sum (y_i - \bar{y})^2 = 6, the model explains R2=12.4/6=0.6R^2 = 1 - 2.4/6 = 0.6 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:

Static fallback: for the five points (1, 2), (2, 4), (3, 5), (4, 4), (5, 5) the least-squares best-fit line is ŷ = 2.2 + 0.6x, with SSE = 2.4 and R² = 0.60. Any other line through these points has a strictly larger SSE — for example, the intuitive line ŷ = 2 + 0.6x has SSE = 2.6.

The regression toolbox

Linear regression is one member of a family. The pages in this group cover the classic alternatives:

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 R2R^2 says nothing about why yy moves.
  • Forcing a line through curved data; transform variables or use a non-linear method when residuals show systematic patterns.
  • Reading R2R^2 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 y^=β^0+β^1x\hat{y} = \hat{\beta}_0 + \hat{\beta}_1 x 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.