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:
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.
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.5Squares 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
MediumA 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.
- Score each line by its sum of squared residuals.
- 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 1Try 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler