Skip to content
StackDecode
AI & ML Atlas
Machine LearningalgorithmInteractive simulation

Linear Regression

Fits the straight line that best predicts a number from an input.

The idea

Choose a slope and intercept so that the squared vertical distances between the line and the data points are as small as possible. You can solve it directly (the normal equation) or walk downhill with gradient descent. The line is easy to interpret: the slope says how much the output changes per unit of input.

ŷ = w·x + b, minimise Σ (y − ŷ)²

Watch it run

This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.

  • data
  • fitted line
  • residual

A bad first guess for the line. The orange segments are the errors (residuals).

step:
0
slope:
-0.4
intercept:
0.95
mse:
0.163
1/14

Gradient descent walks slope and intercept downhill on the squared-error surface.

Step by step

  1. 1Plot the (x, y) data.
  2. 2Start with a guess for slope w and intercept b.
  3. 3Measure the mean squared error of the predictions.
  4. 4Nudge w and b in the direction that reduces the error.
  5. 5Repeat until the error stops falling.

Where it is used

Forecasting, trend analysis, a quick baseline for any numeric prediction.

Watch out for

Outliers pull the line strongly, and it cannot capture curved relationships.

Related