Compute residuals (actual − predicted) and the sum of squared errors SSE = Σr²ᵢ using slope=0.8 and intercept=1.6 from least-squares-line. A single loop computes each residual inline and accumulates SSE. Library: fit with scipy.stats.linregress, apply numpy vectorized prediction, then np.sum(residuals**2). RESULT: SSE (rounded).

By hand

With the library

scipy.stats.linregress refits the line; numpy vectorized subtraction gives residuals; np.sum(residuals**2) gives SSE. Snapshot shows all residuals.

naive.py
slope = 0.8
intercept = 1.6
x = [1, 2, 3, 4, 5]
y = [2, 4, 4, 4, 6]
n = len(x)
sse = 0.0
for i in range(n):
    resid = y[i] - (slope * x[i] + intercept)
    sse = sse + resid * resid
print('RESULT:', round(sse, 4))
library.py
import numpy as np
from scipy import stats
from dalib.display import set_display
set_display()

x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 4, 4, 6])
fit = stats.linregress(x, y)
preds = fit.slope * x + fit.intercept
residuals = y - preds
sse = float(np.sum(residuals ** 2))
print('residuals:', [round(float(r), 4) for r in residuals])
print('RESULT:', round(sse, 4))
residuals: [-0.4, 0.8, 0.0, -0.8, 0.4]
RESULT: 1.6

Honesty

This lesson shows the computation of the residuals and SSE exactly, on a tiny pinned sample. The numbers are correct and reproducible, but on so few points they measure fit to these specific points, not generalization — a small SSE here is not evidence the model is good. Read it as the mechanism of how residual error is summed, not as a conclusion about out-of-sample or population fit; that would need an adequate sample and held-out data.

Implementation notes

  • SSE is the quantity least-squares minimizes: no other line through this data produces a smaller sum of squared residuals.
  • Residuals sum to zero (a property of OLS): −0.4+0.8+0+−0.8+0.4=0.
  • Cross-reference: least-squares-line (this chapter) for the slope and intercept used here; r-squared (this chapter) uses SSE as SS_res.