Lasso回归


Lasso回归是一种线性模型,该方法是一种压缩估计。它通过构造一个惩罚函数得到一个较为精炼的模型,使得它压缩一些回归系数,即强制系数绝对值之和小于某个固定值;同时设定一些回归系数为0。也是一种处理 具有复共线性数据的有偏估计

目标函数为:

min ⁡ w 1 2 n s a m p l e s ∣ ∣ X w − y ∣ ∣ 2 2 + α ∣ ∣ w ∣ ∣ 1 = min ⁡ w 1 2 n s a m p l e s ∑ i = 1 n ( y ^ i − y i ) 2 + α ∑ i = 1 n ∣ w i ∣ (1) \min_w \frac{1}{2n_{samples}}||X_w-y||^2_2+\alpha ||w||_1=\\ \min_w \frac{1}{2n_{samples}}\sum_{i=1}^n(\hat{y}_i-y_i)^2+\alpha\sum_{i=1}^n |w_i| \tag{1} wmin2nsamples1∣∣Xwy22+α∣∣w1=wmin2nsamples1i=1n(y^iyi)2+αi=1nwi(1)

其中, α \alpha α是一个常数, ∣ ∣ w ∣ ∣ 1 ||w||_1 ∣∣w1是L1范数。

from sklearn.linear_model import Lasso

alpha = 0.1
lasso = Lasso(alpha=alpha)

y_pred_lasso = lasso.fit(X_train, y_train).predict(X_test)
r2_score_lasso = r2_score(y_test, y_pred_lasso)
print(lasso)
print("r^2 on test data : %f" % r2_score_lasso)

评价指标:r2_score

选择正则化参数

交叉验证cross-validation

有两种交叉验证——LassoCVLassoLarsCV

  • LassoCV
    基于coordinate descent算法
  • LassoLarsCV
    基于Least Angle Regression算法

对于很多共线性特征的高维数据集,LassoCV表现更好。如果样本量很小,样本量小于特征数,LassoLarsCV比较快。

LassoCV例子:

from sklearn.linear_model import LassoCV

start_time = time.time()
model = make_pipeline(StandardScaler(), LassoCV(cv=20)).fit(X, y)
fit_time = time.time() - start_time

import matplotlib.pyplot as plt

ymin, ymax = 2300, 3800
lasso = model[-1]
plt.semilogx(lasso.alphas_, lasso.mse_path_, linestyle=":")
plt.plot(
    lasso.alphas_,
    lasso.mse_path_.mean(axis=-1),
    color="black",
    label="Average across the folds",
    linewidth=2,
)
plt.axvline(lasso.alpha_, linestyle="--", color="black", label="alpha: CV estimate")

plt.ylim(ymin, ymax)
plt.xlabel(r"$\alpha$")
plt.ylabel("Mean square error")
plt.legend()
_ = plt.title(
    f"Mean square error on each fold: coordinate descent (train time: {fit_time:.2f}s)"
)

LassoLarsCV例子:

from sklearn.linear_model import LassoLarsCV

start_time = time.time()
model = make_pipeline(StandardScaler(), LassoLarsCV(cv=20)).fit(X, y)
fit_time = time.time() - start_time

lasso = model[-1]
plt.semilogx(lasso.cv_alphas_, lasso.mse_path_, ":")
plt.semilogx(
    lasso.cv_alphas_,
    lasso.mse_path_.mean(axis=-1),
    color="black",
    label="Average across the folds",
    linewidth=2,
)
plt.axvline(lasso.alpha_, linestyle="--", color="black", label="alpha CV")

plt.ylim(ymin, ymax)
plt.xlabel(r"$\alpha$")
plt.ylabel("Mean square error")
plt.legend()
_ = plt.title(f"Mean square error on each fold: Lars (train time: {fit_time:.2f}s)")

AIC/BIC准则

计数信息准则是基于训练集数据

困难:

  • 自由度的选择。
  • 要求大样本。
  • 样本量大于特征数。

计算公式为:

A I C = − 2 l o g ( L ^ ) + 2 d (2) AIC=-2log(\hat{L})+2d \tag{2} AIC=2log(L^)+2d(2)

其中, L ^ \hat{L} L^是模型的最大似然估计函数, d d d是参数个数,即自由度。

B I C BIC BIC的计算就是把式(2)中的2替换为 l o g ( N ) log(N) log(N)

B I C = − 2 l o g ( L ^ ) + l o g ( N ) d (3) BIC=-2log(\hat{L})+log(N)d \tag{3} BIC=2log(L^)+log(N)d(3)

其中 N N N是样本量。

对于一个线性高斯模型,最大似然函数的对数为:

l o g ( L ^ ) = − n 2 l o g ( 2 π ) − n 2 l n ( σ 2 ) − ∑ i = 1 n ( y i − y ^ i ) 2 2 σ 2 (4) log(\hat{L})=-\frac{n}{2}log(2\pi)-\frac{n}{2}ln(\sigma^2)-\frac{\sum_{i=1}^n (y_i-\hat{y}_i)^2}{2\sigma^2} \tag{4} log(L^)=2nlog(2π)2nln(σ2)2σ2i=1n(yiy^i)2(4)

将式(4)代入式(2)得:

A I C = n l o g ( 2 π σ 2 ) + ∑ i = 1 n ( y i − y ^ i ) 2 σ 2 + 2 d (5) AIC=nlog(2\pi\sigma^2)+\frac{\sum_{i=1}^n (y_i-\hat{y}_i)^2}{\sigma^2}+2d \tag{5} AIC=nlog(2πσ2)+σ2i=1n(yiy^i)2+2d(5)

其中 σ 2 \sigma^2 σ2是常数,由式(6)估计而得:

σ 2 = ∑ i = 1 n ( y i − y ^ i ) 2 n − p (6) \sigma^2=\frac{\sum_{i=1}^n (y_i-\hat{y}_i)^2}{n-p} \tag{6} σ2=npi=1n(yiy^i)2(6)

其中, p p p是特征个数,且仅当n_samples > n_features时成立。

import time
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LassoLarsIC
from sklearn.pipeline import make_pipeline

start_time = time.time()
lasso_lars_ic = make_pipeline(StandardScaler(), LassoLarsIC(criterion="aic")).fit(X, y)#AIC
fit_time = time.time() - start_time

results = pd.DataFrame(
    {
        "alphas": lasso_lars_ic[-1].alphas_,
        "AIC criterion": lasso_lars_ic[-1].criterion_,
    }
).set_index("alphas")
alpha_aic = lasso_lars_ic[-1].alpha_

以上是 A I C AIC AIC计算。

lasso_lars_ic.set_params(lassolarsic__criterion="bic").fit(X, y)
results["BIC criterion"] = lasso_lars_ic[-1].criterion_
alpha_bic = lasso_lars_ic[-1].alpha_

#加粗列的最小值
def highlight_min(x):
    x_min = x.min()
    return ["font-weight: bold" if v == x_min else "" for v in x]


results.style.apply(highlight_min)

这是 B I C BIC BIC的计算。

对比

一般使用交叉验证选择参数 α \alpha α,因为有很多共线性特征的高维数据集,使用交叉验证的限制较少,而是用信息准则需求更严格。

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值