简介
在本教程中,我们将学习套索回归(Lasso)和弹性网络(Elastic Net),它们是用于线性回归的技术,并通过坐标下降法实现。我们将学习如何使用套索回归和弹性网络计算正则化路径,以及如何使用 matplotlib 显示结果。
虚拟机使用提示
虚拟机启动完成后,点击左上角切换到“笔记本”标签,以访问 Jupyter Notebook 进行练习。
有时,你可能需要等待几秒钟让 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。
如果你在学习过程中遇到问题,请随时向 Labby 提问。课程结束后提供反馈,我们将立即为你解决问题。
加载数据集
在这一步中,我们将从 scikit-learn 库中加载糖尿病数据集并对数据进行标准化。
from sklearn import datasets
## 加载糖尿病数据集
X, y = datasets.load_diabetes(return_X_y=True)
## 标准化数据
X /= X.std(axis=0)
使用套索回归(Lasso)计算正则化路径
在这一步中,我们将使用套索回归(Lasso)技术计算正则化路径,并使用 matplotlib 显示结果。
from sklearn.linear_model import lasso_path
import numpy as np
import matplotlib.pyplot as plt
## 设置 eps 的值
eps = 5e-3
## 使用套索回归计算正则化路径
alphas_lasso, coefs_lasso, _ = lasso_path(X, y, eps=eps)
## 使用 matplotlib 显示结果
plt.figure(1)
colors = cycle(["b", "r", "g", "c", "k"])
neg_log_alphas_lasso = -np.log10(alphas_lasso)
for coef_l, c in zip(coefs_lasso, colors):
l1 = plt.plot(neg_log_alphas_lasso, coef_l, c=c)
plt.xlabel("-Log(alpha)")
plt.ylabel("系数")
plt.title("套索回归路径")
plt.axis("tight")
plt.show()
注:这里“cycle”未翻译,因为它可能是一个自定义函数或在特定库中有特定含义,直接保留英文更合适,不影响整体理解。如果有更多关于它的信息,可进一步准确翻译。
使用正套索回归(Positive Lasso)计算正则化路径
在这一步中,我们将使用正套索回归技术计算正则化路径,并使用 matplotlib 显示结果。
## 使用正套索回归计算正则化路径
alphas_positive_lasso, coefs_positive_lasso, _ = lasso_path(X, y, eps=eps, positive=True)
## 使用 matplotlib 显示结果
plt.figure(2)
neg_log_alphas_positive_lasso = -np.log10(alphas_positive_lasso)
for coef_l, coef_pl, c in zip(coefs_lasso, coefs_positive_lasso, colors):
l1 = plt.plot(neg_log_alphas_lasso, coef_l, c=c)
l2 = plt.plot(neg_log_alphas_positive_lasso, coef_pl, linestyle="--", c=c)
plt.xlabel("-Log(alpha)")
plt.ylabel("系数")
plt.title("套索回归与正套索回归")
plt.legend((l1[-1], l2[-1]), ("套索回归", "正套索回归"), loc="lower left")
plt.axis("tight")
plt.show()
使用弹性网络(Elastic Net)计算正则化路径
在这一步中,我们将使用弹性网络技术计算正则化路径,并使用 matplotlib 显示结果。
from sklearn.linear_model import enet_path
## 使用弹性网络计算正则化路径
alphas_enet, coefs_enet, _ = enet_path(X, y, eps=eps, l1_ratio=0.8)
## 使用 matplotlib 显示结果
plt.figure(3)
neg_log_alphas_enet = -np.log10(alphas_enet)
for coef_e, c in zip(coefs_enet, colors):
l1 = plt.plot(neg_log_alphas_enet, coef_e, c=c)
plt.xlabel("-Log(alpha)")
plt.ylabel("系数")
plt.title("弹性网络路径")
plt.axis("tight")
plt.show()
使用正弹性网络(Positive Elastic Net)计算正则化路径
在这一步中,我们将使用正弹性网络技术计算正则化路径,并使用 matplotlib 显示结果。
## 使用正弹性网络计算正则化路径
alphas_positive_enet, coefs_positive_enet, _ = enet_path(X, y, eps=eps, l1_ratio=0.8, positive=True)
## 使用 matplotlib 显示结果
plt.figure(4)
neg_log_alphas_positive_enet = -np.log10(alphas_positive_enet)
for coef_e, coef_pe, c in zip(coefs_enet, coefs_positive_enet, colors):
l1 = plt.plot(neg_log_alphas_enet, coef_e, c=c)
l2 = plt.plot(neg_log_alphas_positive_enet, coef_pe, linestyle="--", c=c)
plt.xlabel("-Log(alpha)")
plt.ylabel("系数")
plt.title("弹性网络与正弹性网络")
plt.legend((l1[-1], l2[-1]), ("弹性网络", "正弹性网络"), loc="lower left")
plt.axis("tight")
plt.show()
总结
在本教程中,我们学习了用于线性回归的套索回归(Lasso)和弹性网络(Elastic Net)技术。我们学习了如何使用套索回归和弹性网络计算正则化路径,以及如何使用 matplotlib 显示结果。我们还学习了如何使用正套索回归和正弹性网络技术计算正则化路径。