Sub topic 2
The Bias-Variance Trade-off
Assume you know Python, NumPy, supervised learning, linear regression, and scikit-learn’s fit and predict workflow.
Bias and variance
A model with high bias is too simple. It misses real patterns and underfits. A model with high variance is too flexible. It fits noise in the training set and overfits.
Increasing model capacity usually lowers training error. Validation error often falls at first, then rises when variance becomes dominant. The useful model lies near the lowest validation error, not necessarily the lowest training error.
Cross-validation for more reliable estimates
A single validation split can give a noisy estimate. K-fold cross-validation divides the training data into k parts. It trains on k-1 parts and validates on the remaining part, repeating until every part has been used for validation.
Use cross-validation to compare models and tune hyperparameters. Keep the final test set untouched until the end.
from sklearn.datasets import load_diabetes
from sklearn.linear_model import Ridge
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X, y = load_diabetes(return_X_y=True)
for alpha in (0.01, 1, 100):
model = make_pipeline(StandardScaler(), Ridge(alpha=alpha))
scores = cross_val_score(model, X, y, cv=5,
scoring="neg_mean_squared_error")
print(alpha, round(-scores.mean(), 2))Reading learning curves
A learning curve plots error against the number of training examples.
- High training and validation error that remain close suggest high bias.
- Low training error but much higher validation error suggests high variance.
- If both errors improve as data grows, more data may help.
- If the curves flatten at a high error, change the features or model.
Data leakage and how to avoid it
Data leakage occurs when training uses information that would be unavailable at prediction time. Scaling the complete dataset before cross-validation leaks validation-fold statistics into training.
Put preprocessing and the estimator in one Pipeline, as in the example. Fit the pipeline separately inside each fold. Never use the test set to choose a model or hyperparameter.
> callout warning > Leakage can produce an impressive validation score and poor production predictions.
Regularisation: penalising complexity
Regularisation adds a penalty for large coefficients to the training objective. It discourages unnecessarily complex fits and can reduce variance.
Feature scaling and L2 regularisation
Penalties compare coefficient sizes. Without scaling, a feature measured in large units can receive a small coefficient while a feature measured in small units receives a large one. The penalty then treats equivalent effects unfairly.
L2 regularisation, used by ridge regression and weight decay, adds the squared coefficient sizes:
loss + α × Σ coefficient²
A larger α shrinks coefficients more strongly. It usually does not make them exactly zero. Select α with cross-validation.
Exercise
A model has training MSE 0.05 and validation MSE 0.80. Does it show high bias or high variance, and what is one suitable remedy?
Show answer
High variance. Try stronger regularisation, more training data, or a simpler model.