Sub topic 3
L1 Regularisation (Lasso) and Sparse Models
Assume you know features, labels, loss functions, and scikit-learn’s fit and predict workflow. This chapter focuses on remedies for overfitting.
L1 regularisation and sparse models
L1 regularisation adds the absolute values of model coefficients to the loss:
\[ \text{loss} + \alpha \sumj |wj| \]
Here, \(\alpha\) controls the penalty strength. A larger value pushes more coefficients toward zero. Some coefficients often become exactly zero, so the model ignores those features. This is called a sparse model.
import numpy as np
from sklearn.linear_model import Lasso
from sklearn.preprocessing import StandardScaler
rng = np.random.default_rng(0)
X = rng.normal(size=(100, 10))
y = 3 * X[:, 0] - 2 * X[:, 1] + rng.normal(0, 0.5, size=100)
X_scaled = StandardScaler().fit_transform(X)
model = Lasso(alpha=0.1).fit(X_scaled, y)
print(np.round(model.coef_, 2))Scaling matters because L1 penalises coefficient values. Without scaling, a feature’s units can affect whether its coefficient is penalised fairly.
Choosing regularisation strength
Choose \(\alpha\) with cross-validation on the training data. Keep the test set untouched until the final evaluation. A very small \(\alpha\) may leave overfitting. A very large \(\alpha\) may cause underfitting.
Elastic net: mixing L1 and L2
Elastic net combines L1 and L2 penalties. L1 encourages sparsity, while L2 tends to spread weight across correlated features. Use elastic net when you want some feature selection but your features are strongly related.
Early stopping
For neural networks, stop training when validation loss stops improving. Restore the best validation weights. This limits the time available for the network to memorise noise.
Dropout in neural networks
Dropout randomly removes units during training. The network must learn useful patterns through multiple partial networks rather than depending on a few units. A common dropout rate is 0.2 to 0.5, but tune it with validation data.
More data and data augmentation
More independent training examples usually reduce variance. Data augmentation creates realistic variations, such as shifted or rotated images. It must preserve the correct label. Do not apply transformations that change the task.
A practical workflow for diagnosing and fixing a model
flowchart TD
A[Compare training and validation error] --> B{Validation error high?}
B -->|Training error also high| C[Reduce bias: add capacity or features]
B -->|Training error low| D[Reduce variance: regularise or add data]
D --> E[Choose strength with cross-validation]
E --> F[Evaluate once on untouched test data]Use learning curves to check whether more data might help. Check preprocessing for leakage, and place scaling inside a pipeline so each fold learns its scaling only from its training portion.
Exercise
A model has very low training error and much higher validation error. Should you decrease or increase L1 strength first?
Show answer
Increase it gradually and compare cross-validated validation error. Also consider more data, augmentation, dropout, or early stopping.