Curvingo · Made in India

Glossary

Select one of the keywords on the left…

Linear RegressionFinding the Best Line

Reading time: ~15 min

With the slider you found the best slope by trying values one after another. A real model may have thousands of numbers to learn, and it cannot try every combination. It needs a smarter way.

Walking Down the Error Valley

Imagine standing blindfolded on a hillside, trying to reach the bottom of the valley. You feel which way the ground slopes under your feet and take one step downhill. Then you feel again, and step again — until the ground is flat. This is gradient descent, the method that trains almost every machine-learning model.

For Riya's line, the ground is the error for each slope m — the purple valley below — and the ball is the current slope. Each step follows one rule:

m_{\text{new}} = m - \alpha \times (\text{slope of the valley at } m)

The number \alpha (alpha) is the learning rate: how big a step to take. Pick a learning rate and take steps.

Note

The "slope of the valley" is the derivative dE/dm from Class 12 calculus. For Riya's data the valley is E(m) = 30.8 + 22(m - 6)^2, so dE/dm = 44(m - 6): it is negative left of the bottom (step right), positive right of it (step left) and zero at m = 6. A real model has many numbers to learn; the list of all their slopes together is called the gradient, which gives the method its name.

The Formula Shortcut

For a straight line there is also a shortcut. Setting the derivatives to zero gives a formula that lands on the bottom of the valley in one jump:

m = \dfrac{\sum (x - \bar{x})(y - \bar{y})}{\sum (x - \bar{x})^2}, \qquad c = \bar{y} - m\bar{x}

Let's try it on Aman's study log: hours studied x and marks y in four weekly tests — (1, 50), (2, 55), (3, 65) and (4, 70). First the means: \bar{x} = and \bar{y} = .

xyx - \bar{x}y - \bar{y}product(x - \bar{x})^2
150−1.5−10152.25
255−0.5−52.50.25
3650.552.50.25
4701.510152.25
Sum5

So m = 35 \div 5 = and c = 60 - 7 \times 2.5 = .

Aman's model: marks ≈ 7 × hours + 42.5. Each extra hour of study is worth about 7 marks.

Play With the Formula

Here the computer runs that formula every time you tap. Tap the grid to add points and watch the purple best line jump to its new place.

Did you notice how a single point far from the others can swing the whole line? Such points are called outliers. Always look at your data before you trust the line.

If the formula finds the answer in one jump, why use step-by-step gradient descent at all? With many features, the shortcut needs a big matrix inverse ((X^TX)^{-1}, from the Matrices chapter). For a dataset with millions of rows and thousands of features, the shortcut becomes

much slower than gradient descent
much faster than gradient descent
less accurate than gradient descent

That is why gradient descent is the engine inside modern machine learning, from this little line all the way to the neural networks behind chatbots.

Sina