Curvingo · Made in India

Glossary

Select one of the keywords on the left…

Linear RegressionHow It Works, Briefly

Reading time: ~10 min

Before a model learns anything, it needs something to beat. The laziest possible model ignores the temperature completely and predicts Riya's average sale, 86 kulfis, every single day — the grey dashed line. Each red segment is that day's error (also called the residual): the actual sale minus the predicted sale.

On the 40 °C day Riya sold 148 kulfis, but the lazy guess says 86. The error for that day is kulfis.

On hot days the lazy line guesses too low; on cool days it guesses too high. The errors have a pattern — and a pattern in the errors means the model is missing something. Here it is missing the temperature, so let's tilt the line.

Tilt the Line

We keep the line pinned at the centre point (31 °C, 86 kulfis) — the average temperature and the average sale — and change only its slope. To score a line, we square each of the ten red errors, add them up and divide by 10. This score is the mean squared error, or MSE: the smaller, the better. Find the slope with the smallest MSE.

Slope of the line: ${m}

The best line has slope 6 and passes through (31, 86), so its intercept is c = 86 - 6 \times 31 = . Riya's trained model is

\hat{y} = 6x - 100

Note

The best-fitting straight line always passes through the point (\bar{x}, \bar{y}) — the mean of the features and the mean of the targets. That is why we could pin the line there and only search for the slope.

Predict Tomorrow

Now the model earns its keep. Tomorrow will be 38 °C, so Riya should make about \hat{y} = 6 \times 38 - 100 = kulfis.

That is the whole machine-learning loop in miniature. Put its four stages in order.

Train: find the m and c that make the error smallest
Collect examples: ten days of temperature and sales
Predict: put a new x into the line
Choose a model: a straight line ŷ = mx + c

Tip

A prediction is a best guess, not a promise. Even with the best line, some days Riya will sell more and some days less — a cricket match next door or a power cut changes things that temperature cannot explain. This leftover randomness is called noise, and no model can predict it.

Sina