Skip to content
AI360Xpert
Beta
Core ML

Visual explainer

Linear Regression

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

Scattered points hide a trend you need for the next prediction.
Scattered points hide a trend you need for the next prediction.

Your data arrives as a scattered cloud with no rule attached. Each dot is one past observation, and a new input sits waiting with no output. You need one straight trend that speaks for all of them.

Errors Run Vertical

Each error is measured straight up and down from the line.
Each error is measured straight up and down from the line.

Draw a candidate line and judge it gap by gap. Each error is the vertical distance from a dot to the line, never sideways. For (1,2), (2,3), (3,5) the gaps read 0.17, minus 0.33, 0.17.

Square Every Gap

Squaring the three gaps and adding them gives 0.167, the score to beat.
Squaring the three gaps and adding them gives 0.167, the score to beat.

Squaring stops plus and minus gaps from cancelling out, and it punishes distant dots hardest. Your three gaps become 0.029, 0.109, 0.029, summing to 0.167. The best line makes that total smallest.

Read Off Answers

A new input of 2.5 reads straight off the line as 4.08.
A new input of 2.5 reads straight off the line as 4.08.

Prediction is just reading. Drop from new input x = 2.5 up to the line, slide across to the axis, and you land on 4.08. The line you fitted is now a machine that answers questions.

Proof It Worked

The fitted line scores 0.17 against 4.67 for always guessing the average.
The fitted line scores 0.17 against 4.67 for always guessing the average.

Compare against laziness. Always guessing the average 3.33 scores 4.67 on the same points, while your line scores 0.17. Fitting cuts the error to one twenty eighth. That gap is what you paid for.

Match Error To Trust

Square errors on clean data, but switch to absolute error if a point may be corrupt.
Square errors on clean data, but switch to absolute error if a point may be corrupt.

Squared error trusts every dot completely, which is brave. If one reading may be corrupt, price gaps by absolute size instead: a 3.8 gap costs 3.8, not 14.4. Match your error to your trust.

Where It Breaks

One far outlier flips the slope from 1.5 to minus 0.07 for everyone.
One far outlier flips the slope from 1.5 to minus 0.07 for everyone.

Squaring gives one far dot a megaphone. Add (6,2) to your three clean points and the slope collapses from 1.5 to minus 0.07. Everyone else pays for one bad reading. Plot first, always.

The Quick Version

  • Scattered dots need one straight trend.
  • Errors run vertical, never sideways.
  • Squared gaps sum to 0.167 here.
  • New inputs read straight off the line.
  • Fitting beats guessing the average.
  • Corrupt data calls for absolute error.
  • One outlier flips the whole slope.