Skip to content

Line of Best Fit

You'll fit a straight line to a small dataset twice, once by hand in a spreadsheet and once in code, and get the same slope both ways.

About

Nearly everything in machine learning is a fancier version of this: pick a line, measure how wrong it is, and nudge it until it's less wrong. Doing it by hand first means you'll never again treat a model as a black box you call and hope. You need twenty or so pairs of numbers you care about, a spreadsheet, and any language that can multiply two lists. What counts is the same slope and intercept from both methods, to two decimal places, on numbers you chose yourself rather than a tutorial's.

At a glance

Per requirement
15 XP
Completion bonus
75 XP
Requirements
5
Takes
One evening
Skill level
Novice (never tried it)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own

Requirements

  1. 1
    Collect at least 20 pairs of numbers that might be related. Pick something close to home: outdoor temperature and your heating bill, hours of sleep and how long your first coffee lasted, distance and time from a running app, house size and asking price from a listings site. Put x in column A and y in column B. Twenty rows is the floor, because with fewer than that a single odd point owns the line. Take a screenshot of the sheet with the column headers saying what each number is and what unit it's in. Photo
  2. 2
    Plot the points and guess a line by eye. Make a scatter chart, then add a line by typing a slope and an intercept into two cells and plotting y = mx + b over the top. Adjust the two cells until it looks right to you. Write your guessed slope and intercept down before the next step, because the point is to see how close eyeballing gets you. Take a screenshot of the chart with your guessed line over the points. Photo
  3. 3
    Compute the least-squares slope and intercept with the formulas, not a built-in. Add four columns: x minus its mean, y minus its mean, the product of those two, and the square of the first. The slope is the sum of the products divided by the sum of the squares, and the intercept is mean y minus slope times mean x. Only after that, check yourself with SLOPE and INTERCEPT in two more cells. Click a cell so the formula bar shows the formula and take a screenshot that shows the formulas, not just the numbers. Photo
  4. 4
    Fit the same line in code and match the spreadsheet to two decimal places. Write it in any language without a fitting library: load the pairs, compute the means, the sums and the two numbers. Thirty lines is about right. If the answers disagree, the usual culprit is a row the spreadsheet is silently skipping or a text cell that looks like a number. Take a screenshot of the code and its output next to the spreadsheet cells. Photo
  5. 5
    Measure how wrong the line is, and say what one unit of error means. Compute the mean squared error and its square root for both your eyeballed line and the fitted one. The root is in the same units as y, so you can say something like 'the line is off by about 11 minutes on a typical day'. Write three sentences on the two errors and whether the fitted line is enough better to have been worth the arithmetic. Post a screenshot of the numbers with the write-up. Photo