当前位置: 首页 > news >正文

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

弹性网络

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

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

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 datasetsX, 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 pathseps = 5e-3  # the smaller it is the longer is the pathprint("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 resultsplt.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()
http://www.lryc.cn/news/108499.html

相关文章:

  • 嵌入式入门教学——C51
  • 2023-08-03力扣每日一题
  • 【蓝桥杯备考资料】如何进入国赛?
  • QtWebApp开发https服务器,完成客户端与服务器基于ssl的双向认证
  • 动态IP代理的优势展现与应用场景
  • ad+硬件每日学习十个知识点(22)23.8.2(LDO datasheet手册解读)
  • 这可是全网最全的网络工程师零基础实战视频整理,最新版分享
  • 笔记本WIFI连接无网络【实测有效解决方案,不用重启电脑】
  • js 正则表达式配合replace进行过滤html字符串遇到的性能问题
  • 2022牛客寒假算法基础集训营1
  • API对接:构建连接不同系统的技术桥梁
  • 【MySQL】仓储--维护出入库流水、库存,去重数量逻辑修正
  • 用Log4j 2记录日志
  • 【Java面试】Paxos和Raft协议的区别?
  • 手机浏览器H5打开微信小程序支付,自定义传参
  • Aligning Large Language Models with Human: A Survey
  • windows图标白了,刷新图标
  • C++ 左值和右值
  • c++学习(智能指针)[29]
  • B站高播放又涨粉的带货UP主怎么做?
  • AD21 PCB设计的高级应用(四)FPGA的管脚交换功能
  • 超低功耗LCD段码屏驱动显示芯片VK1621原厂芯片
  • 【深入探索Docker】:开启容器化时代的技术奇迹
  • 【Ajax】笔记-JQuery发送jsonp请求
  • docker更换数据存储路径
  • GPT告诉你如何延长周末体验
  • 一百四十一、Kettle——kettle8.2在Windows本地开启carte服务以及配置子服务器
  • 你知道充电桩控制主板的结构吗?
  • LeetCode 25题:K个一组翻转链表
  • Day 19 C++ 文件操作