通过信息准则进行Lasso模型选择#

本示例重现了[ZHT2007]的图2示例。在糖尿病数据集上拟合LassoLarsIC估计器,并使用AIC和BIC准则选择最佳模型。

注意

重要的是要注意,使用LassoLarsIC寻找alpha的优化依赖于在样本内(即直接在训练集上)计算的AIC或BIC准则。这种方法与交叉验证过程不同。要比较这两种方法,可以参考以下示例:Lasso模型选择:AIC-BIC / 交叉验证

References

# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

我们将使用糖尿病数据集。

from sklearn.datasets import load_diabetes

X, y = load_diabetes(return_X_y=True, as_frame=True)
n_samples = X.shape[0]
X.head()
age 性别 bmi bp s1 s2 s3 s4 s5 s6
0 0.038076 0.050680 0.061696 0.021872 -0.044223 -0.034821 -0.043401 -0.002592 0.019907 -0.017646
1 -0.001882 -0.044642 -0.051474 -0.026328 -0.008449 -0.019163 0.074412 -0.039493 -0.068332 -0.092204
2 0.085299 0.050680 0.044451 -0.005670 -0.045599 -0.034194 -0.032356 -0.002592 0.002861 -0.025930
3 -0.089063 -0.044642 -0.011595 -0.036656 0.012191 0.024991 -0.036038 0.034309 0.022688 -0.009362
4 0.005383 -0.044642 -0.036385 0.021872 0.003935 0.015596 0.008142 -0.002592 -0.031988 -0.046641


Scikit-learn 提供了一个名为 LassoLarsIC 的估计器,它使用 Akaike 信息准则 (AIC) 或贝叶斯信息准则 (BIC) 来选择最佳模型。在拟合此模型之前,我们将对数据集进行缩放。

接下来,我们将拟合两个模型,比较 AIC 和 BIC 报告的值。

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

lasso_lars_ic = make_pipeline(StandardScaler(), LassoLarsIC(criterion="aic")).fit(X, y)

为了与[ZHT2007]中的定义保持一致,我们需要重新调整AIC和BIC。事实上,Zou等人忽略了一些常数项,这些常数项与线性模型的最大对数似然得出的AIC原始定义相比被忽略了。您可以参考用户指南的数学细节部分

def zou_et_al_criterion_rescaling(criterion, n_samples, noise_variance):
    """Rescale the information criterion to follow the definition of Zou et al."""
    return criterion - n_samples * np.log(2 * np.pi * noise_variance) - n_samples
import numpy as np

aic_criterion = zou_et_al_criterion_rescaling(
    lasso_lars_ic[-1].criterion_,
    n_samples,
    lasso_lars_ic[-1].noise_variance_,
)

index_alpha_path_aic = np.flatnonzero(
    lasso_lars_ic[-1].alphas_ == lasso_lars_ic[-1].alpha_
)[0]
lasso_lars_ic.set_params(lassolarsic__criterion="bic").fit(X, y)

bic_criterion = zou_et_al_criterion_rescaling(
    lasso_lars_ic[-1].criterion_,
    n_samples,
    lasso_lars_ic[-1].noise_variance_,
)

index_alpha_path_bic = np.flatnonzero(
    lasso_lars_ic[-1].alphas_ == lasso_lars_ic[-1].alpha_
)[0]

现在我们已经收集了AIC和BIC,我们也可以检查两个准则的最小值是否发生在相同的alpha处。然后,我们可以简化以下图。

index_alpha_path_aic == index_alpha_path_bic
np.True_

最后,我们可以绘制AIC和BIC准则以及随后选择的正则化参数。

import matplotlib.pyplot as plt

plt.plot(aic_criterion, color="tab:blue", marker="o", label="AIC criterion")
plt.plot(bic_criterion, color="tab:orange", marker="o", label="BIC criterion")
plt.vlines(
    index_alpha_path_bic,
    aic_criterion.min(),
    aic_criterion.max(),
    color="black",
    linestyle="--",
    label="Selected alpha",
)
plt.legend()
plt.ylabel("Information criterion")
plt.xlabel("Lasso model sequence")
_ = plt.title("Lasso model selection via AIC and BIC")
Lasso model selection via AIC and BIC

脚本总运行时间: (0 分钟 0.080 秒)

相关示例

Lasso 模型选择:AIC-BIC / 交叉验证

Lasso 模型选择:AIC-BIC / 交叉验证

高斯混合模型选择

高斯混合模型选择

随机梯度下降的早停

随机梯度下降的早停

使用多任务 Lasso 进行联合特征选择

使用多任务 Lasso 进行联合特征选择

由 Sphinx-Gallery 生成的图库