09
Regression
Regularization
Ridge & Lasso Regression
When ordinary least squares overfits or when features are correlated, regularization helps. Ridge regression adds an L2 penalty (α‖w‖²) that shrinks coefficients toward zero without eliminating them. Lasso regression adds an L1 penalty (α‖w‖₁) that drives some coefficients exactly to zero, performing feature selection. This example trains both Ridge and Lasso on a dataset with many features, compares their coefficients, and shows how the regularization strength α controls the bias-variance tradeoff. You will see that Lasso produces sparse models while Ridge retains all features with smaller weights.
Deepbox Modules Used
deepbox/datasetsdeepbox/mldeepbox/metricsdeepbox/preprocessWhat You Will Learn
- Ridge adds L2 penalty — shrinks all coefficients but keeps them non-zero
- Lasso adds L1 penalty — drives some coefficients to exactly zero (feature selection)
- Higher alpha means stronger regularization (more bias, less variance)
- Scale features before regularized regression for fair penalty distribution
Source Code
09-ridge-lasso/index.ts
1import { loadDiabetes } from "deepbox/datasets";2import { Lasso, Ridge } from "deepbox/ml";3import { mae, r2Score } from "deepbox/metrics";4import { StandardScaler, trainTestSplit } from "deepbox/preprocess";56console.log("=== Ridge & Lasso Regression ===\n");78const data = loadDiabetes();9const [X_train, X_test, y_train, y_test] = trainTestSplit(10 data.data, data.target, { testSize: 0.2, randomState: 42 }11);1213const scaler = new StandardScaler();14scaler.fit(X_train);15const X_tr = scaler.transform(X_train);16const X_te = scaler.transform(X_test);1718// Ridge (L2 regularization)19const ridge = new Ridge({ alpha: 1.0 });20ridge.fit(X_tr, y_train);21const ridgePreds = ridge.predict(X_te);22console.log("Ridge R²:", r2Score(y_test, ridgePreds).toFixed(4));23console.log("Ridge MAE:", mae(y_test, ridgePreds).toFixed(2));24console.log("Ridge non-zero coefs:", ridge.coef.size);2526// Lasso (L1 regularization — produces sparse coefficients)27const lasso = new Lasso({ alpha: 1.0 });28lasso.fit(X_tr, y_train);29const lassoPreds = lasso.predict(X_te);30console.log("\nLasso R²:", r2Score(y_test, lassoPreds).toFixed(4));31console.log("Lasso MAE:", mae(y_test, lassoPreds).toFixed(2));Console Output
$ npx tsx 09-ridge-lasso/index.ts
=== Ridge & Lasso Regression ===
Ridge R²: 0.4532
Ridge MAE: 43.21
Ridge non-zero coefs: 10
Lasso R²: 0.4187
Lasso MAE: 44.78