Compute R² = 1 − SS_res/SS_tot where SS_res = Σ(yᵢ−ŷᵢ)² (residual sum of squares) and SS_tot = Σ(yᵢ−ȳ)² (total sum of squares). One loop for the mean, one loop accumulating both SS_res and SS_tot together. Library: scipy.stats.linregress(x, y).rvalue**2 (R² = r² in simple linear regression). RESULT: R² (rounded).

By hand

With the library

scipy.stats.linregress(x, y).rvalue**2 gives R² directly (r=2/√5, r²=4/5=0.8). The snapshot also shows r for comparison with ch04.

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

x = [1, 2, 3, 4, 5]
y = [2, 4, 4, 4, 6]
fit = stats.linregress(x, y)
r2 = float(fit.rvalue ** 2)
print('r:', round(float(fit.rvalue), 4))
print('r_squared:', round(r2, 4))
print('RESULT:', round(r2, 4))
r: 0.8944
r_squared: 0.8
RESULT: 0.8

Honesty

This lesson shows the computation of R² exactly, on a tiny pinned sample. The value is arithmetically correct and reproducible, but it measures only how well the line fits these specific in-sample points — on so few points a high R² overstates fit and says nothing about out-of-sample or population performance. Treat it as "how the formula is computed," not as evidence the model generalizes; real evaluation needs held-out data and an adequate sample size.

Implementation notes

  • R²=r² holds only for simple (one-predictor) linear regression; in multiple regression R² ≠ Pearson r².
  • R²=0.8 means the line explains 80% of the variance in y.
  • SS_tot = SS_res + SS_reg (regression sum of squares); R² = SS_reg/SS_tot.
  • Cross-reference: residuals-and-sse (this chapter) for SS_res; pearson-correlation (ch04) for r whose square gives R² here.