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

这篇具有很好参考价值的文章主要介绍了机器学习之弹性网络(Elastic Net)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

弹性网络

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

对弹性网络进行简单的介绍:
ElasticNet是一个训练时同时用ℓ1和ℓ2范数进行正则化的线性回归模型,lasso是使用ℓ1范数进行正则化的线性回归模型。
弹性网络简弹性网络简介弹性网络简文章来源地址https://www.toymoban.com/news/detail-631386.html

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()

到了这里,关于机器学习之弹性网络(Elastic Net)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 大数据机器学习深入Scikit-learn:掌握Python最强大的机器学习库

    本篇博客详细介绍了Python机器学习库Scikit-learn的使用方法和主要特性。内容涵盖了如何安装和配置Scikit-learn,Scikit-learn的主要特性,如何进行数据预处理,如何使用监督学习和无监督学习算法,以及如何评估模型和进行参数调优。本文旨在帮助读者深入理解Scikit-learn,并有效

    2024年02月03日
    浏览(34)
  • R语言生存分析(机器学习)(2)——Enet(弹性网络)

    弹性网络(Elastic Net):是一种用于回归分析的统计方法,它是岭回归(Ridge Regression)和lasso回归(Lasso Regression)的结合,旨在克服它们各自的一些限制。弹性网络能够同时考虑L1正则化(lasso)和L2正则化(岭回归),从而在特定情况下对于高维数据集具有更好的性能。    

    2024年02月13日
    浏览(44)
  • 掌握 Scikit-Learn: Python 中的机器学习库入门

    机器学习 (Machine Learning) 是一个近年来频繁出现在科技新闻, 研究报告, 行业分析和实际应用中的热门领域. 机器学习 (Machine Learning) 正以前所未有的速度影响着我们的生活. 从智能音响的语音识别, 手机摄像头的人脸解锁, 到金融领域的评估, 医疗健康的预测分析. 机器学习的应

    2024年02月07日
    浏览(47)
  • 机器学习实战----使用Python和Scikit-Learn构建简单分类器

    前言: Hello大家好,我是Dream。 今天来学习一下如何使用Python和Scikit-Learn构建一个简单的分类器 今天我们将学习 使用Python和Scikit-Learn创建一个简单的文本分类器来识别垃圾邮件 。我们将先介绍数据集,并通过可视化和数据预处理方式更好地理解数据集。接着,我们将选择一

    2023年04月09日
    浏览(37)
  • 深入Scikit-learn:掌握Python最强大的机器学习库

    本篇博客详细介绍了Python机器学习库Scikit-learn的使用方法和主要特性。内容涵盖了如何安装和配置Scikit-learn,Scikit-learn的主要特性,如何进行数据预处理,如何使用监督学习和无监督学习算法,以及如何评估模型和进行参数调优。本文旨在帮助读者深入理解Scikit-learn,并有效

    2024年02月15日
    浏览(40)
  • Python机器学习:Scikit-learn和TensorFlow的应用和模型设计

    Python在机器学习领域中已经成为非常受欢迎的编程语言。Scikit-learn和TensorFlow是Python中应用最广泛的两个机器学习库,它们提供了丰富的机器学习算法和工具,帮助开发人员轻松地构建和训练机器学习模型。本文将详细介绍Scikit-learn和TensorFlow的应用和模型设计。   Scikit-learn是

    2024年02月04日
    浏览(32)
  • 【小尘送书-第三期】Python机器学习:基于PyTorch和Scikit-Learn 》

    大家好,我是小尘,欢迎关注,一起交流学习!欢迎大家在CSDN后台私信我!一起讨论学习,讨论如何找到满意的实习! 近年来,机器学习方法凭借其理解海量数据和自主决策的能力,已在医疗保健、 机器人、生物学、物理学、大众消费和互联网服务等行业得到了广泛的应用

    2024年02月15日
    浏览(43)
  • 机器学习-决策树-回归-CPU(中央处理单元)数据-python scikit-learn

    决策树是一种监督机器学习算法,用于回归和分类任务。树是可以处理复杂数据集的强大算法。 决策树特性: 不需要数值输入数据进行缩放。无论数值是多少,决策树都不在乎。 不同于其他复杂的学习算法,决策树的结果是可以解释的,决策树不是黑盒类型的模型。 虽然大

    2024年02月20日
    浏览(34)
  • AI机器学习实战 | 使用 Python 和 scikit-learn 库进行情感分析

    专栏集锦,大佬们可以收藏以备不时之需 Spring Cloud实战专栏:https://blog.csdn.net/superdangbo/category_9270827.html Python 实战专栏:https://blog.csdn.net/superdangbo/category_9271194.html Logback 详解专栏:https://blog.csdn.net/superdangbo/category_9271502.html tensorflow专栏:https://blog.csdn.net/superdangbo/category_869

    2024年02月05日
    浏览(33)
  • 〖码银送书第三期〗《Python机器学习:基于PyTorch和Scikit-Learn》

    前言 近年来,机器学习方法凭借其理解海量数据和自主决策的能力,已在医疗保健、 机器人、生物学、物理学、大众消费和互联网服务等行业得到了广泛的应用。自从AlexNet模型在2012年ImageNet大赛被提出以来,机器学习和深度学习迅猛发展,取得了一个又一个里程碑式的成就

    2024年02月15日
    浏览(77)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包