A residual is the gap between an observed response and the value predicted by a fitted line. Subtract the prediction from the actual value: positive residuals sit above the line, and negative residuals sit below it. section 7.9
Residuals help quantify how closely a regression fits its data:
- Square each residual and add the results to obtain the sum of squares, a loss measure for the line. section 7.9
- Smaller overall residuals indicate points closer to the fitted line; larger residuals indicate a looser fit. section 7.9
- Use residual calculations alongside visualizations and other checks rather than treating one score as proof of a good model. section 7.11 section 7.7
Sports drink sales
For each temperature, calculate predicted_y = m * x + b, then add (actual_y - predicted_y) ** 2 to a running total. The resulting sum of squares measures the line's total squared error.
import numpy as np
x = np.array([51.5, 55.1, 63.0, 71.2, 73.0, 77.1,
88.1, 91.2, 93.0, 101.6])
y = np.array([100.98, 137.44, 237.8, 254.96, 263.45,
353.89, 450.32, 521.34, 503.13, 573.91])
m = 9.74 #1
b = -405.26 #1
sum_of_squares = 0 #2
for i in range(len(x)): #2
actual_y = y[i]
predicted_y = m * x[i] + b
sum_of_squares += (actual_y - predicted_y) ** 2
print(f"Sum of squares: {sum_of_squares:.2f}") #3
Inspect the short NumPy loop that computes each residual, squares it, and accumulates the regression loss. section 7.9










