Linear Regression
Draw the straight line that best predicts one number from another.
Drag the line or its round handles, or use the sliders. The equation, the meter and the table follow every move.
- a
- where the line starts: the height at age 0
- b
- the slope: growth for each extra week
- e
- the miss: real height minus prediction
ŷ = 43.0 + 0.0 × 4.0 = 43.0 cm
| 1 | 7.6 | 56 | 43.0 | 13.0 |
| 2 | 0.7 | 16 | 43.0 | −27.0 |
| 3 | 9.4 | 69 | 43.0 | 26.0 |
| 4 | 2.4 | 23 | 43.0 | −20.0 |
| 5 | 4.7 | 46 | 43.0 | 3.0 |
| 6 | 6.7 | 55 | 43.0 | 12.0 |
| 7 | 3.4 | 34 | 43.0 | −9.0 |
| 8 | 8.5 | 65 | 43.0 | 22.0 |
| 9 | 5.3 | 42 | 43.0 | −1.0 |
| 10 | 1.6 | 22 | 43.0 | −21.0 |
Try it in Python
The same 10 measurements as the Lab, in a few lines of Python. Plain Python works the best line out by hand, and checks your line from the Lab too.
What you are looking at
Each dot is one measurement of a growing plant. Across the graph (x) is the plant's age in weeks; up the graph (y) is its height in centimetres. The table lists the same 10 measurements, one per row.
The numbers are made up. Each plant starts between 1 and 10 cm tall and grows between 1 and 10 cm a week, and every measurement is a little off, just as real measurements are. Refresh Data shows a different plant, with its own start and its own growth.
What each symbol means
- x
- The input we know: the plant's age, in weeks.
- y
- The real result: the plant's measured height.
- ŷ
- "y-hat": the line's prediction of the height. The hat means "estimated".
- a
- The intercept: the height of the line at week 0, when the plant was first measured. It moves the whole line up or down.
- b
- The slope: how many centimetres the plant grows each week. It tilts the line.
- e
- The miss for one point: e = y − ŷ. Positive when the point is above the line, negative when it is below.
- MSE
- Mean squared error: the average of all the misses, each one squared.
- Typical miss
- The square root of the MSE. It is back in centimetres, so it reads as "the line is usually off by about this much".
How good is a line?
No straight line goes through every point, so every line misses a little. The miss for one point is the gap between the real value and the prediction:
To score the whole line, square every miss and take the average. Squaring makes every miss count as positive, so misses above and below the line cannot cancel out, and it punishes big misses more than small ones:
The smaller the MSE, the better the line fits. Turn on Misses to see each e as a dotted line from the point to your line.
Finding the best line: the optimization
Training a linear regression model means choosing a and b so that the MSE is as small as possible. That search is the optimization.
Imagine trying every pair of a and b and writing down the MSE of each line. The results form a smooth bowl with exactly one lowest point, and that point is the best line. For a straight line, two formulas jump straight to it:
x̄ and ȳ are the averages of x and y, so the best line always passes through the average point. Turn on Best Line to compare it with yours. Because every measurement is a little off, the best line lands close to the plant's real start and growth, but rarely exactly on them: the line can only learn from the data it sees.
Bigger models have no such formula. There, the computer walks down the bowl instead: it nudges a and b a little in the direction that lowers the error, again and again, until the error stops shrinking. This is called gradient descent, and it is how most machine-learning models are trained.
The model in short
- Choose a line: ŷ = a + b·x turns any age into a predicted height.
- Measure how wrong it is: square every miss and average them (the MSE).
- Optimize: find the a and b that make the MSE as small as possible.
- Predict: for a new x, read ŷ off the line. For the first plant, the best line predicts that at 4 weeks old it is about 36 cm tall.