Back to topic
Optional
Bias–variance on polynomial regression
Average predictions over seeds: bias², variance, MSE. Self-check in the notebook.
- Version
- v1
- Minutes
- ~25
- Evidence
- Self-check in the notebook
Until platform auto-check exists, compare cell output with the expected numbers in the notebook. This is not a debt and does not affect mastery.
Download the notebook and run it locally. Its environment requirements are listed inside.
Notebook contents
Readable on your phone. The code below has not been executed in the browser; download the notebook to run it.
ML · Bias–variance на линейной регрессии / Bias–variance toy
Activity version: 1 · Seed: 3 · Данные: , линейный fit, 300 датасетов × 12 точек.
Ожидаемые числа (среднее по сетке из 21 точки)
| Quantity | Value | Tolerance |
|---|---|---|
| bias² | ≈ 0.000267 |
abs ≤ 0.005 |
| variance | ≈ 0.032626 |
abs ≤ 0.01 |
Самопроверка в ноутбуке
Не влияет на mastery.
import numpy as np
rng = np.random.default_rng(3)
x_grid = np.linspace(0, 1, 21)
f = 2.0 * x_grid - 0.5
n_datasets = 300
preds = []
for _ in range(n_datasets):
x = rng.uniform(0, 1, size=12)
y = 2.0 * x - 0.5 + rng.normal(scale=0.4, size=12)
coef = np.polyfit(x, y, 1)
preds.append(np.polyval(coef, x_grid))
preds = np.stack(preds)
mean_pred = preds.mean(axis=0)
bias2 = ((mean_pred - f) ** 2).mean()
var = preds.var(axis=0).mean()
print("bias2", bias2, "var", var, "sum", bias2 + var)
assert abs(bias2 - 0.00026699) < 0.005
assert abs(var - 0.03262608) < 0.01
print("OK: bias-variance toy")
Самопроверка в ноутбуке
- bias² и variance в допусках таблицы
- MSE ≈ bias² + variance (без шума)