Menu
CoddyTech

Residuals

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 35 of 64.

This chapter draws a trend line through data, predicts with it and grades it. Here is where it leads:

Five lawn jobs as dots, minutes against lawn area, with an orange trend line through them and a big green star on the line predicting 50 minutes for a new lawn of 10

How good is a line? A lawn service guesses 4.5 * area + 5 minutes per job. Each red gap is a residual: actual minus predicted.

The five lawn jobs and the guessed line, with a red vertical gap from each dot to the line labelled +6, -5, +3.5, -7 and +3.5

Square and add them to score the line:

area = np.array([4, 6, 9, 12, 15])
minutes = np.array([29, 27, 49, 52, 76])
residuals = minutes - (4.5 * area + 5)
print(residuals)
print((residuals ** 2).sum())

This prints:

[ 6.  -5.   3.5 -7.   3.5]
134.5

Squares stop +6 and -6 from cancelling out. The smaller this sum of squared residuals, the better the line. A model learns by shrinking it.

challenge icon

Challenge

Medium

A moving company guesses how long a move takes from its number of boxes. boxes and hours hold eight past moves, and the program reads two guessed lines: slope1, intercept1, then slope2, intercept2.

  1. Score each line by its sum of squared residuals.
  2. Print both sums, rounded to 2 decimals, and the better line, as below.

For the lines 0.16 * boxes + 0.8 and 0.12 * boxes + 2.5:

Line 1 SSR: 0.57
Line 2 SSR: 4.1
Better: line 1

Try it yourself

import numpy as np

boxes = np.array([10, 18, 25, 32, 40, 47, 55, 60])
hours = np.array([2.5, 3.5, 5.0, 5.5, 7.0, 8.0, 9.5, 10.0])
slope1 = float(input())
intercept1 = float(input())
slope2 = float(input())
intercept2 = float(input())

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler