Lasso vs Ridge regularization

notion image

Regularization 的核心作用是:限制模型的自由度,防止它把噪声当成规律学进去

在机器学习中,模型训练的目标通常是:最小化训练误差
但问题是:
  • 如果模型太灵活
  • 参数太多
  • 特征高度相关
👉 模型会过度贴合训练数据(Overfitting)
👉 在新数据上表现反而变差

Regularization 的思想是:不仅要拟合数据,还要为“复杂模型”付出代价

notion image
 

Lasso (L1 Regularization)

  • Lasso 会把不重要的系数直接压到 0
    • 这意味着:
    • 某些 feature 的 coefficient = 0
    • 这些 feature 完全从模型中消失
  • Lasso 适合什么场景?
    • 特征很多
    • 但你怀疑:
      • 有不少特征其实是没用的
    • 希望:
      • 模型更简单
      • 更容易解释
 
notion image

Ridge (L2 Regularization)

  • Ridge 不会把系数压到 0,只会压得“很小”
    • 这意味着:
    • 所有特征仍然在模型中
    • 但:不重要的特征影响被弱化
    • 所以:Ridge = 降低过拟合,但不做特征选择
  • Ridge 适合什么场景?
    • 你确信:
      • 很多特征都“有点用”
    • 存在:
      • collinearity(特征高度相关)
    • 不希望:
      • 随便删掉变量
      📌 时间序列 / 宏观因子 / 金融多因子模型中常见
notion image

Choosing Between Them

  • Feature Selection: Lasso performs it, while Ridge does not.
  • Model Complexity: Lasso tends to produce sparser models.
  • Stability: Ridge is more stable in scenarios with multicollinearity.
对比维度
Lasso
Ridge
Regularization 类型
L1
L2
系数是否会变 0
✅ 会
❌ 不会
是否自动特征选择
✅ 是
❌ 否
模型稀疏性
解释性
更强
较弱
处理共线性
一般
很强
方法
英文名
核心作用
Lasso
Lasso Regression
直接把不重要特征“干掉”
Ridge
Ridge Regression
压小系数,但不删除特征
  • Use Lasso if you believe some features are irrelevant.
  • Use Ridge when all features are potentially useful and to handle multicollinearity.

Implementation Example in Python Lasso

# Import modules from sklearn.linear_model import Lasso, LassoCV from sklearn.metrics import mean_squared_error # Train/test split X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=123, test_size=0.3) # Instantiate cross-validated lasso, fit lasso_cv = LassoCV(alphas=None, cv=10, max_iter=10000) lasso_cv.fit(X_train, y_train) # Instantiate lasso, fit, predict and print MSE lasso = Lasso(alpha = lasso_cv.alpha_) lasso.fit(X_train, y_train) print(mean_squared_error(y_true=y_test, y_pred=lasso.predict(X_test)))