← All simulations · Pillar 4: Making predictions
The line of best fit
What it is
Here are some dots: each one is a day at a lemonade stand — how warm it was, and how many cups sold. The dots roughly march upward: warmer days sell more. The line of best fit is the single straight line that comes as close as possible to all the dots at once. Once you have it, you can predict a day you’ve never seen.
Go deeper: “closest” means the smallest total miss. A line is just y = m·x + b — a slope m and a starting height b. Finding the m and b with the smallest total miss is exactly what linear regression does.
Why care
Fitting a line to dots is one of the most-used ideas in all of science and AI: predicting a house price from its size, a plant’s growth from sunlight, tomorrow’s sales from today’s. Whenever someone says “the trend shows…,” there’s usually a line of best fit underneath.
The idea, intuitively
Imagine stretching a rubber band straight across the dots. Each dot pulls the band toward itself; a dot far from the line pulls harder. The line settles where the pulls balance out. In the picture below, each little orange stick is one dot’s miss — how far the line is from that dot. Add up all the misses and you get the wrongness. The best line is the one with the smallest wrongness.
Peek at the data first
Every machine-learning project starts the same way: look at the data. Here are all the
lemonade-stand records — a day’s temperature and how many cups sold — with a
quick summary of each column. This is exactly what Spectra’s describe_data
shows you.
Try it
Drag the two blue handles to tilt and raise the line (or click a handle and use the up/down arrow keys). Watch the orange misses and the wrongness bar change. Then tick Show the computer’s best fit and try to match it.
Where it shows up
- Predicting prices. Estimate a house’s price from its size, or a used car’s price from its mileage — a line through past sales.
- Science. Researchers fit lines to measurements to find relationships, like how a medicine’s dose relates to its effect.
- Forecasting. Stretch the line past the last dot to make a careful guess about what comes next — with the honesty that the farther out you go, the shakier it gets.
Where it came from
The method behind the best line — least squares — was first published by Adrien-Marie Legendre in 1805. Carl Friedrich Gauss said he had used it earlier and published his own version in 1809, which sparked a famous priority argument; historians usually credit both. The word “regression” came later, from Francis Galton in the 1880s, who noticed that very tall parents tend to have children a bit closer to average height — a “regression toward the mean.”
Try it in code
The Studio fits a real line for you and plots it over the data:
data = load "lemonade_stand" model = make_model "regressor" train_model model, on: data, predict: "cups", using: ["temperature"] say "On a 30 degree day, about", predict(model, temperature: 30), "cups." plot_data data, x: "temperature", y: "cups", line: model
Check your understanding
- What does a long orange stick mean about that dot?
- Why does tilting the line to match the dots’ slope shrink the wrongness?
- Could a curve ever fit these dots better than a straight line? When might that be a good idea — and when might it be cheating?