Note
Go to the end to download the full example code. or to run this example in your browser via Binder
使用LARS的Lasso路径#
使用LARS算法在糖尿病数据集上计算沿正则化参数的Lasso路径。每种颜色代表系数向量的不同特征,并显示为正则化参数的函数。
Computing regularization path using the LARS ...
.
# 作者:scikit-learn 开发者
# SPDX-License-Identifier: BSD-3-Clause
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
X, y = datasets.load_diabetes(return_X_y=True)
print("Computing regularization path using the LARS ...")
_, _, coefs = linear_model.lars_path(X, y, method="lasso", verbose=True)
xx = np.sum(np.abs(coefs.T), axis=1)
xx /= xx[-1]
plt.plot(xx, coefs.T)
ymin, ymax = plt.ylim()
plt.vlines(xx, ymin, ymax, linestyle="dashed")
plt.xlabel("|coef| / max|coef|")
plt.ylabel("Coefficients")
plt.title("LASSO Path")
plt.axis("tight")
plt.show()
Total running time of the script: (0 minutes 0.050 seconds)
Related examples
SVM 边界示例
绘制岭回归系数与正则化参数的关系
随机梯度下降:凸损失函数
Lasso 和弹性网络