机器学习之弹性网络(Elastic Net)

弹性网络

代码原文

下面代码参考scikit-learn中文社区,链接在上面。

但是由于scikit-learn中文社区上的代码有些地方跑不通,故对此代码做了修改,输出结果与社区中显示的结果相同。

对弹性网络进行简单的介绍:

ElasticNet是一个训练时同时用ℓ1和ℓ2范数进行正则化的线性回归模型,lasso是使用ℓ1范数进行正则化的线性回归模型。
弹性网络简弹性网络简介弹性网络简

python 复制代码
from itertools import cycle
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import lasso_path, enet_path
from sklearn import datasets

X, y = datasets.load_diabetes(return_X_y=True)


X /= X.std(axis=0)  # Standardize data (easier to set the l1_ratio parameter)
print("------------------------------------")
print(X)
print("------------------------------------")
print(y)
# Compute paths

eps = 5e-3  # the smaller it is the longer is the path

print("Computing regularization path using the lasso...")
# alphas_lasso, coefs_lasso, _ = lasso_path(X, y, eps=eps, fit_intercept=False)
alphas_lasso, coefs_lasso, _ = lasso_path(X, y)

print("Computing regularization path using the positive lasso...")
# alphas_positive_lasso, coefs_positive_lasso, _ = lasso_path(
#     X, y, eps=eps, positive=True, fit_intercept=False)
alphas_positive_lasso, coefs_positive_lasso, _ = lasso_path(
    X, y, eps=eps, positive=True)

print("Computing regularization path using the elastic net...")
# alphas_enet, coefs_enet, _ = enet_path(
#     X, y, eps=eps, l1_ratio=0.8, fit_intercept=False)
alphas_enet, coefs_enet, _ = enet_path(
    X, y, eps=eps, l1_ratio=0.8)

print("Computing regularization path using the positive elastic net...")
# alphas_positive_enet, coefs_positive_enet, _ = enet_path(
#     X, y, eps=eps, l1_ratio=0.8, positive=True, fit_intercept=False)
alphas_positive_enet, coefs_positive_enet, _ = enet_path(
    X, y, eps=eps, l1_ratio=0.8, positive=True)
print("------------------------------------")
print(alphas_positive_enet)
print("------------------------------------")
print(coefs_positive_enet)
# Display results

plt.figure(1)
colors = cycle(['b', 'r', 'g', 'c', 'k'])
neg_log_alphas_lasso = -np.log10(alphas_lasso)
neg_log_alphas_enet = -np.log10(alphas_enet)
for coef_l, coef_e, c in zip(coefs_lasso, coefs_enet, colors):
    l1 = plt.plot(neg_log_alphas_lasso, coef_l, c=c)
    l2 = plt.plot(neg_log_alphas_enet, coef_e, linestyle='--', c=c)

plt.xlabel('-Log(alpha)')
plt.ylabel('coefficients')
plt.title('Lasso and Elastic-Net Paths')
plt.legend((l1[-1], l2[-1]), ('Lasso', 'Elastic-Net'), loc='lower left')
plt.axis('tight')


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('coefficients')
plt.title('Lasso and positive Lasso')
plt.legend((l1[-1], l2[-1]), ('Lasso', 'positive Lasso'), loc='lower left')
plt.axis('tight')


plt.figure(3)
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('coefficients')
plt.title('Elastic-Net and positive Elastic-Net')
plt.legend((l1[-1], l2[-1]), ('Elastic-Net', 'positive Elastic-Net'),
           loc='lower left')
plt.axis('tight')
plt.show()
相关推荐
程序媛-徐师姐11 分钟前
基于 Python Django 的校园互助平台(附源码,文档)
开发语言·python·django·校园互助·校园互助平台
神经星星31 分钟前
无机材料逆合成效率飙升,韩国团队推出Retrieval-Retro,成果入选NeurIPS 2024
人工智能·深度学习·机器学习
大数据追光猿35 分钟前
【深度学习】Pytorch项目实战-基于协同过滤实现物品推荐系统
人工智能·pytorch·python·深度学习·ai编程·推荐算法
师范大学生44 分钟前
基于CNN的FashionMNIST数据集识别2——模型训练
python·深度学习·cnn
web137656076431 小时前
纯 Python、Django、FastAPI、Flask、Pyramid、Jupyter、dbt 解析和差异分析
python·django·fastapi
大模型铲屎官1 小时前
哈希表入门到精通:从原理到 Python 实现全解析
开发语言·数据结构·python·算法·哈希算法·哈希表
qq4054251971 小时前
基于python的旅客游记和轨迹分析可视化系统设计(新)
开发语言·python
m0_594526302 小时前
基于 PyQt5 实现分组列表滚动吸顶效果
开发语言·python·qt
Felaim2 小时前
基于模仿学习(IL)的端到端自动驾驶发展路径
人工智能·深度学习·自动驾驶
thinkMoreAndDoMore3 小时前
深度学习(3)-TensorFlow入门(常数张量和变量)
开发语言·人工智能·python