Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Diagnose the Bias-Variance tradeoff and tame complex models using L1 (Lasso) and L2 (Ridge) regularization.
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Regularization strength | Higher lambda enforces simpler, smoother models | |
| Model weights | Coefficients of features |
Large weight w=10 with lambda=0.1. Ridge penalty is 0.1 * 100 = 10.0 added to loss.
Result: Optimizer forces weight down to avoid high penalty
Plot training loss and validation loss over model complexity or epochs.
When training loss keeps falling but validation loss starts rising, overfitting has begun.
Increase lambda, prune decision trees, apply dropout, or collect more training data.
Problem: Dataset has 500 patients but 20,000 gene features (high risk of extreme overfitting).
Solution: Lasso (L1) regularization forces 19,950 uninformative gene weights to 0, isolating the 50 truly causal mutations.
Problem: High-degree polynomial regression overfits noisy historical stock price spikes.
Solution: Ridge regularization shrinks coefficients, producing a smooth generalized economic trend.
Interactive activity component
See a high-degree polynomial overfit and how Ridge regression restores generalization.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install scikit-learn numpy1 from sklearn.preprocessing import PolynomialFeatures 2 from sklearn.linear_model import LinearRegression, Ridge 3 import numpy as np
from sklearn.preprocessing import PolynomialFeatures β Create polynomial features
from sklearn.linear_model import LinearRegression, Ridge β Linear & Ridge models
import numpy as np β NumPy arrays
1 # Noisy sine wave data (only 6 points) 2 X = np.array([[0.1], [0.3], [0.5], [0.7], [0.85], [1.0]]) 3 y = np.sin(X.ravel() * 3.14) + np.random.normal(0, 0.05, 6) 4 5 # Expand to 5th-degree polynomial features 6 poly = PolynomialFeatures(degree=5) 7 X_poly = poly.fit_transform(X) 8 9 # 1. Unregularized Model (Wild Overfitting!): 10 overfit_model = LinearRegression().fit(X_poly, y) 11 print("Unregularized Max Weight:", max(abs(overfit_model.coef_)).round(1)) 12 13 # 2. Ridge Regularized Model (Constrained weights!): 14 ridge_model = Ridge(alpha=1.0).fit(X_poly, y) 15 print("Ridge Regularized Max Weight:", max(abs(ridge_model.coef_)).round(1))
# Noisy sine wave data (only 6 points)
Small dataset
X = np.array([[0.1], [0.3], [0.5], [0.7], [0.85], [1.0]])
Input points
y = np.sin(X.ravel() * 3.14) + np.random.normal(0, 0.05, 6)
Noisy targets
# Expand to 5th-degree polynomial features
High complexity
poly = PolynomialFeatures(degree=5)
Creates x, x^2, x^3, x^4, x^5
X_poly = poly.fit_transform(X)
Transform features
# 1. Unregularized Model (Wild Overfitting!):
Standard OLS
overfit_model = LinearRegression().fit(X_poly, y)
Fit without leash
print("Unregularized Max Weight:", max(abs(overfit_model.coef_)).round(1))
Exploding weights
# 2. Ridge Regularized Model (Constrained weights!):
L2 penalty
ridge_model = Ridge(alpha=1.0).fit(X_poly, y)
Fit with alpha=1.0
print("Ridge Regularized Max Weight:", max(abs(ridge_model.coef_)).round(1))
Tamed weights
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Taming Overfitting with Ridge Regularization 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install scikit-learn numpy 8 9 # Step 2: Import libraries 10 from sklearn.preprocessing import PolynomialFeatures 11 from sklearn.linear_model import LinearRegression, Ridge 12 import numpy as np 13 14 # Step 3: Run Demonstration 15 # Noisy sine wave data (only 6 points) 16 X = np.array([[0.1], [0.3], [0.5], [0.7], [0.85], [1.0]]) 17 y = np.sin(X.ravel() * 3.14) + np.random.normal(0, 0.05, 6) 18 19 # Expand to 5th-degree polynomial features 20 poly = PolynomialFeatures(degree=5) 21 X_poly = poly.fit_transform(X) 22 23 # 1. Unregularized Model (Wild Overfitting!): 24 overfit_model = LinearRegression().fit(X_poly, y) 25 print("Unregularized Max Weight:", max(abs(overfit_model.coef_)).round(1)) 26 27 # 2. Ridge Regularized Model (Constrained weights!): 28 ridge_model = Ridge(alpha=1.0).fit(X_poly, y) 29 print("Ridge Regularized Max Weight:", max(abs(ridge_model.coef_)).round(1))
Weights explode to massive magnitudes (+/- 800) to pass through every noisy sample.
L2 penalty keeps weights small (< 2.0), producing a smooth generalized curve.
6 noisy sample points fitted with 5th degree polynomial
Unregularized Max Weight: ~840.2 (Overfitting) Ridge Regularized Max Weight: ~1.8 (Tamed & Generalized)
Ridge regularization squashed runaway parameter explosion, preventing the model from oscillating wildly between points.
NumPy arrays, pandas DataFrames, train-test splits, and scikit-learn models.
Test your understanding with these questions. Remember, this is for learning, not examination!