1.12 Lab: Bias-Variance Through Experiments
[markdown] content available only on the platform
Flashcards · 1.12 Lab: Bias-Variance Through Experiments
np.polyfit() — NumPy function that fits a polynomial of a given degree and returns its coefficients.
coef = np.polyfit(x_train, y_train, 3)
np.polyval() — Evaluates a polynomial with given coefficients at chosen x values to get predictions.
y_hat = np.polyval(coef, x_test)
Polynomial degree — Controls model complexity: higher degree means a more flexible curve with more coefficients.
Degree 9 has 10 coefficients.
Underfitting — A model too simple for the pattern; error is high on both training and test data (high bias).
Degree 1 on a sine wave: train 0.220, test 0.353.
Overfitting — A model so flexible it fits the noise; training error near 0 but test error high (high variance).
Degree 9 on 10 points: train 0.000, test 0.914.
Noise level — The irreducible error from random noise; no model's test MSE can go meaningfully below it.
Noise σ = 0.3 gives σ² = 0.09.
Train-test gap — The difference between training and test error; a large gap is the signal of high variance.
Train 0.000 vs test 0.914.
More data reduces variance — With more training samples, a flexible model can no longer memorise the noise so easily.
Degree 9 with 100 points: test MSE about 0.109.
np.random.default_rng(seed) — Creates a random generator with a fixed seed so everyone gets the same synthetic data.
rng = np.random.default_rng(2)
Choose the simpler model — When two models have almost the same test error, prefer the less complex one.
Degree 3 over degree 5 at similar test error.
[file] /api/lesson-media/files/9b417aa2-5e9f-4ae3-a9db-2a3b7f03f372-1794890093879-d381ba1627b914c5.zip
