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

slope=0.8, intercept=1.6, x=[1,2,3,4,5], y=[2,4,4,4,6]. Predictions: 2.4, 3.2, 4.0, 4.8, 5.6. Residuals: −0.4, 0.8, 0.0, −0.8, 0.4. SSE = 0.16+0.64+0+0.64+0.16 = 1.6.

naive.py
Replay: real traced execution (multi-file project)
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))
  1. slope ← 0.8

    1slope = 0.82intercept = 1.6
    values this step0.8slope
  2. intercept ← 1.6

    1slope = 0.82intercept = 1.63x = [1, 2, 3, 4, 5]
    values this step1.6intercept
  3. x ← [1, 2, 3, 4, 5]

    2intercept = 1.63x = [1, 2, 3, 4, 5]4y = [2, 4, 4, 4, 6]
    values this step[1, 2, 3, 4, 5]x
  4. y ← [2, 4, 4, 4, 6]

    3x = [1, 2, 3, 4, 5]4y = [2, 4, 4, 4, 6]5n = len(x)
    values this step[2, 4, 4, 4, 6]y
  5. n ← 5

    4y = [2, 4, 4, 4, 6]5n = len(x)6sse = 0.0
    values this step5n
  6. sse ← 0.0

    5n = len(x)6sse = 0.07for i in range(n):
    values this step0.0sse
  7. i ← 0

    6sse = 0.07for i in range(n):8    resid = y[i] - (slope * x[i] + intercept)
    values this step0i
  8. resid ← -0.40000000000000036

    7for i in range(n):8    resid = y[i] - (slope * x[i] + intercept)9    sse = sse + resid * resid
    values this step-0.40000000000000036resid
  9. sse ← 0.16000000000000028

    8    resid = y[i] - (slope * x[i] + intercept)9    sse = sse + resid * resid10print('RESULT:', round(sse, 4))
    values this step0.0 0.16000000000000028sse
  10. i ← 1

    6sse = 0.07for i in range(n):8    resid = y[i] - (slope * x[i] + intercept)
    values this step0 1i
  11. resid ← 0.7999999999999998

    7for i in range(n):8    resid = y[i] - (slope * x[i] + intercept)9    sse = sse + resid * resid
    values this step-0.40000000000000036 0.7999999999999998resid
  12. sse ← 0.7999999999999999

    8    resid = y[i] - (slope * x[i] + intercept)9    sse = sse + resid * resid10print('RESULT:', round(sse, 4))
    values this step0.16000000000000028 0.7999999999999999sse
  13. i ← 2

    6sse = 0.07for i in range(n):8    resid = y[i] - (slope * x[i] + intercept)
    values this step1 2i
  14. resid ← 0.0

    7for i in range(n):8    resid = y[i] - (slope * x[i] + intercept)9    sse = sse + resid * resid
    values this step0.7999999999999998 0.0resid
  15. sse = sse + resid * resid

    8    resid = y[i] - (slope * x[i] + intercept)9    sse = sse + resid * resid10print('RESULT:', round(sse, 4))
  16. i ← 3

    6sse = 0.07for i in range(n):8    resid = y[i] - (slope * x[i] + intercept)
    values this step2 3i
  17. resid ← -0.8000000000000007

    7for i in range(n):8    resid = y[i] - (slope * x[i] + intercept)9    sse = sse + resid * resid
    values this step0.0 -0.8000000000000007resid
  18. sse ← 1.440000000000001

    8    resid = y[i] - (slope * x[i] + intercept)9    sse = sse + resid * resid10print('RESULT:', round(sse, 4))
    values this step0.7999999999999999 1.440000000000001sse
  19. i ← 4

    6sse = 0.07for i in range(n):8    resid = y[i] - (slope * x[i] + intercept)
    values this step3 4i
  20. resid ← 0.40000000000000036

    7for i in range(n):8    resid = y[i] - (slope * x[i] + intercept)9    sse = sse + resid * resid
    values this step-0.8000000000000007 0.40000000000000036resid
  21. sse ← 1.6000000000000014

    8    resid = y[i] - (slope * x[i] + intercept)9    sse = sse + resid * resid10print('RESULT:', round(sse, 4))
    values this step1.440000000000001 1.6000000000000014sse
  22. for i in range(n):

    6sse = 0.07for i in range(n):8    resid = y[i] - (slope * x[i] + intercept)
  23. stdout ← RESULT: 1.6

    9    sse = sse + resid * resid10print('RESULT:', round(sse, 4))
    values this stepRESULT: 1.6stdout

With the library

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

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.