Syllabus Lesson 20 of 246 · Module 1: Python for this course
Module 1: Python for this course

Mini project: fit a line by nudging

Why it matters This is how every neural network learns, with the fancy words taken out. You will do it here before it has a name.

Some points lie roughly on a line y = slope * x + intercept, but we do not know the slope or intercept. Start with both at 0. Measure how wrong the line is: the error is the average of (prediction minus truth) squared. Then nudge.

The nudging recipe

  1. Try the slope a little higher, and a little lower. Keep whichever lowers the error, if either does.
  2. Do the same for the intercept.
  3. Repeat. If no nudge helps, make the nudge smaller, and stop when it is tiny.

The error falls step by step until the line sits on the points. In Module 3 you will replace "try both directions" with a formula that tells you which way is downhill straight away. That formula is the gradient, and walking along it is called gradient descent. What you build here, trying each setting up and down in turn, has its own name: coordinate search. It is slower, but it is the same idea of walking downhill.

Mean squared error: the average of each mistake squared. Squaring makes every mistake positive and punishes big ones more.

Recap Measure how wrong you are, nudge, keep what helps, repeat.

Words and symbols in this lesson
mean, spread
the average, and roughly how far values sit from it (standard deviation)
Your turn

Write mse(slope, intercept, x, y) and fit_line(x, y). fit_line uses the nudging recipe: start at 0 and 0 with a nudge of 1.0, halve the nudge whenever no nudge helps, stop when the nudge is below 0.0001, and return (slope, intercept). The hidden test checks the error on new data is close to the best possible.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.