Skip to content
AI360XpertBeta
Core ML

Visual explainer

Linear Regression

Fit a straight line through scattered data points by minimising squared vertical errors, giving a single trend for prediction.

A trend can predict a continuous output.
A trend can predict a continuous output.

Linear regression predicts a numerical output by adding an intercept to weighted inputs. Here we fit one straight line by minimizing squared vertical errors. The circles are three illustrative observations: (1,2), (2,3), (3,5). Inputs and outputs have arbitrary units; they are not measured performance data. The dashed query at input 2.5 has no observed output yet. Its question mark represents an unknown value, not an estimated height.

What Does Slope Change?

Slope controls rise per input step.
Slope controls rise per input step.

A line predicts y^=mx+b\hat y=mx+b: mm is slope, bb is intercept, and the hat marks a prediction. Both lines keep b=1/3b=1/3. The solid line rises 1.5 output units for one input unit; the dashed comparison rises 1. The triangle measures the solid line from input 1 to 2. The two axis scales differ, so screen angle alone is not the numerical slope.

What Does Intercept Change?

Intercept shifts a line without changing slope.
Intercept shifts a line without changing slope.

This is a deliberate zoom near the origin, over inputs 0 to 0.5. Both lines have slope 1.5. Changing the intercept from 0 to 1/3 lifts every prediction by 1/3; the two brackets have equal length. The intercept is the prediction at input zero. An intercept need not have a practical interpretation when zero lies outside the data's relevant range.

Measure Vertical Residuals

Residuals compare outputs at the same input.
Residuals compare outputs at the same input.

For our three observations, least squares gives m=3/2m=3/2 and b=1/3b=1/3. To check: the input mean is xˉ=2\bar x=2, output mean yˉ=10/3\bar y=10/3, centered input square sum is 2, and centered input-output product sum is 3. Their ratio is the slope; b=yˉ−mxˉb=\bar y-m\bar x.

The predictions are 11/6,10/3,29/611/6,10/3,29/6. A residual is observed output minus predicted output at the same input: ri=yi−y^ir_i=y_i-\hat y_i. Thus the three residuals are 1/6,−1/3,1/61/6,-1/3,1/6. This residual view subtracts the fitted output from each observation: hollow marks at zero represent the fitted value and solid dots represent the differences. All vertical stems use the same magnification, 360 drawing units per output unit. These are vertical output differences, not perpendicular distances to the fitted line.

Why Square The Gaps?

Doubling a gap quadruples its squared error.
Doubling a gap quadruples its squared error.

The detached squares use the absolute residual as side length at the same 360-unit magnification. Their sides are 60,120,60 and their areas have ratio 1:4:1. The dotted cross divides the middle square into four copies of either smaller area. The area unit here is relative; the actual squared residuals are 1/36,1/9,1/361/36,1/9,1/36 in squared output units.

The sum of squared errors, or SSE, is 1/36+1/9+1/36=1/6≈0.1671/36+1/9+1/36=1/6\approx0.167. Squaring prevents opposite signs from cancelling and gives larger gaps disproportionately more influence.

Choose The Least-Squares Fit

The fitted slope minimizes this example's squared error.
The fitted slope minimizes this example's squared error.

Each position on this curve is a candidate slope, with its intercept reoptimized as b=10/3−2mb=10/3-2m. For this particular dataset, expansion gives

SSE(m)=16+2(m−32)2.\mathrm{SSE}(m)=\frac16+2\left(m-\frac32\right)^2.

The squared term is nonnegative and vanishes at m=1.5m=1.5, proving this is the minimum. Slopes 1 and 2 both give SSE 2/32/3; the focal minimum gives 1/61/6. The curve shows a continuous objective, not merely a comparison among three sampled candidates. NIST's least-squares reference explains the model and fitting objective.

Predict A New Output

Input 2.5 gives a fitted output of about 4.08.
Input 2.5 gives a fitted output of about 4.08.

Return to the original plot scales. Follow input 2.5 vertically to the fitted line and then left to the output reading. Substitution gives y^=(3/2)(5/2)+1/3=49/12≈4.0833\hat y=(3/2)(5/2)+1/3=49/12\approx4.0833. The hollow point is a model prediction, not an additional observation. It is interpolation because 2.5 lies within the observed input range.

Compare Against A Constant

The fitted line has lower training error than the mean predictor.
The fitted line has lower training error than the mean predictor.

A constant predictor always returns 10/310/3. Its SSE on these same observations is (−4/3)2+(−1/3)2+(5/3)2=14/3(-4/3)^2+(-1/3)^2+(5/3)^2=14/3. The fitted line's SSE is 1/61/6, exactly one twenty-eighth as large. Both bars share a zero and scale; the short bar is not inflated for visibility. This is a training comparison on three points. It says nothing by itself about accuracy on unseen data.

Look For Curved Structure

A straight fit can leave a curved residual pattern.
A straight fit can leave a curved residual pattern.

We now switch explicitly to five new observations: inputs 0,1,2,3,4 with outputs 0,1,4,9,16. The dashed least-squares straight line is 4x−24x-2. Its residuals are 2,-1,-2,-1,2: both ends above the fit and the middle below. The marked vertical gaps reveal structure the straight raw-input model misses. The output scale changes to fit this larger range.

A polynomial feature such as x2x^2 can represent this curve while remaining linear in its learned coefficients. The limit here belongs to the straight model on the original input, not every model called linear regression.

Where It Breaks

One distant input can reverse the fitted slope.
One distant input can reverse the fitted slope.

Return to the original three observations and add the diamond (6,2). The input range expands to 0 through 7; the output scale matches the original plot. The dashed original fit rises with slope 1.5. Refitting all four points gives input mean 3, output mean 3, centered input square sum 14, and centered input-output product sum -1. Hence the solid fit is y^=−x/14+45/14\hat y=-x/14+45/14, with slope about −0.0714.

A distant input has leverage and can strongly alter the fit. If this new observation is corrupted, its influence damages the model for the original data; the diagram alone cannot establish corruption. Investigate data collection and the form of the relationship before choosing a remedy. Robust methods have different behavior for unusual inputs and unusual outputs; changing to absolute or Huber loss is not a universal cure. The official robust-regression discussion distinguishes those cases.

The Quick Version

  • Linear regression predicts numerical outputs from weighted inputs and an intercept.
  • Slope measures output rise per input step.
  • Intercept shifts every prediction equally while keeping slope fixed.
  • Residuals compare observed and fitted outputs at the same input.
  • Doubling a residual quadruples its squared-error contribution.
  • Least squares selects the coefficients with the smallest squared-error sum.
  • The fitted line predicts about 4.08 at input 2.5 here.
  • Lower training error does not establish unseen accuracy.
  • Curved residual patterns can expose a straight model's missing structure.
  • A distant input can reverse the least-squares slope.