4.弹性网络( Elastic Net)

ElasticNet 是一种使用L1和L2先验作为正则化矩阵的线性回归模型.这种组合用于只有很少的权重非零的稀疏模型,比如:class:Lasso, 但是又能保持:class:Ridge 的正则化属性.我们可以使用 l1_ratio 参数来调节L1和L2的凸组合(一类特殊的线性组合)。

当多个特征和另一个特征相关的时候弹性网络非常有用。Lasso 倾向于随机选择其中一个,而弹性网络更倾向于选择两个.
在实践中,Lasso 和 Ridge 之间权衡的一个优势是它允许在循环过程(Under rotate)中继承 Ridge 的稳定性.
弹性网络的目标函数是最小化:

\underset{w}{min\,} { \frac{1}{2n_{samples}} ||X w - y||_2 ^ 2 + \alpha \rho ||w||_1 +\frac{\alpha(1-\rho)}{2} ||w||_2 ^ 2}

ElasticNetCV 可以通过交叉验证来用来设置参数 alpha (\alpha) 和 l1_ratio (\rho)




print(__doc__)

import numpy as np
import matplotlib.pyplot as plt

from sklearn.linear_model import lasso_path, enet_path
from sklearn import datasets

diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target

X /= X.std(axis=0)  # Standardize data (easier to set the l1_ratio parameter)

# 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, fit_intercept=False)

print("Computing regularization path using the positive lasso...")
alphas_positive_lasso, coefs_positive_lasso, _ = lasso_path(
    X, y, eps, positive=True, fit_intercept=False)
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)

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

# Display results

plt.figure(1)
ax = plt.gca()
ax.set_color_cycle(2 * ['b', 'r', 'g', 'c', 'k'])
l1 = plt.plot(-np.log10(alphas_lasso), coefs_lasso.T)
l2 = plt.plot(-np.log10(alphas_enet), coefs_enet.T, linestyle='--')

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)
ax = plt.gca()
ax.set_color_cycle(2 * ['b', 'r', 'g', 'c', 'k'])
l1 = plt.plot(-np.log10(alphas_lasso), coefs_lasso.T)
l2 = plt.plot(-np.log10(alphas_positive_lasso), coefs_positive_lasso.T,
              linestyle='--')

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)
ax = plt.gca()
ax.set_color_cycle(2 * ['b', 'r', 'g', 'c', 'k'])
l1 = plt.plot(-np.log10(alphas_enet), coefs_enet.T)
l2 = plt.plot(-np.log10(alphas_positive_enet), coefs_positive_enet.T,
              linestyle='--')

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


  • 10
    点赞
  • 99
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
【开发课程目的】随着人工智能时代到来,人们的工作、生活和学习方式将发生颠覆性的变化。巨变中,传统行业或将改变、或将消失,同时也会有大量人工智能相关岗位涌现出来。Python作为人工智能产品开发的首选语言,自然成为需求热点,掌握Python编程技能已成为大势所趋。因此,我们设计开发了人工智能编程系列课程:第一篇:Python编程基础第二篇:Python数据分析第三篇:Python机器学习第四篇:Python深度学习【零基础机器学习与深度学习课特色】1、基础篇课程设计考虑后续发展,为数据分析方向、机器学习方向打下坚实基础2、从设计到实现,讲解不放过每一行代码,帮助学员快速形成编程能力3、机器学习篇课程内容全面,包括13种监督学习模型、6种无监督学习模型、8种数据预处理与特征工程技术、10种模型调优技术与评估指标4、机器学习课程包括鸢尾花分类、手写数字识别、人脸特征提取与重建、泰坦尼克号生存预测等十几个应用案例5、深度学习篇课程包括神经网络基础、计算机视觉、序列、生成式深度学习4个单元,涉及密集网络、卷积网络、循环网络和对抗网络等6、深度学习课程包括手写数字识别、情感分析、猫狗分类、诗词创作、生成艺术风格照片等十几个应用案例【学习目标】 完成从小白到胜任Python机器学习或深度学习岗位这个从0到1的蜕变。购课的学员请发站内消息领取课程相关代码、课件
《狂神聊Elasticsearch.md》是一篇关于Elasticsearch的文章。Elasticsearch是一个开源的分布式搜索和分析引擎,它被广泛应用于全文搜索、数据分析、日志处理等场景。这篇文章详细介绍了Elasticsearch的原理、基本操作和常用功能。 文章首先介绍了Elasticsearch的基本概念和架构。它采用分布式的倒排索引,以实现高效的全文搜索。集群中的每个节点都可以承担不同的角色,包括主节点、数据节点和协调节点。这种设计使得Elasticsearch具有高可用性和弹性伸缩性。 接下来,文章详细介绍了如何安装和配置Elasticsearch。它提供了两种安装方式,一种是通过官方下载安装包进行安装,另一种是通过Docker容器进行安装。然后,文章介绍了如何配置Elasticsearch的参数,包括网络配置、集群配置和节点配置等。 文章还介绍了Elasticsearch的常用功能,包括索引管理、数据查询、聚合分析和文档更新等。通过示例代码和详细讲解,读者可以了解到如何创建索引、添加文档、执行查询和聚合操作。文章还介绍了如何使用Kibana进行数据可视化和监控。 最后,文章提到了Elasticsearch的一些高级功能,包括分布式搜索、索引优化和集群监控等。它介绍了如何通过查询路由和复制机制实现分布式搜索,并讲解了如何通过分片和副本优化索引的性能和可靠性。此外,文章还介绍了如何使用Elasticsearch的API和插件进行集群监控和故障诊断。 总之,这篇文章全面而详细地介绍了Elasticsearch的原理、操作和常用功能,对于想要学习和使用Elasticsearch的人来说,是一篇非常有价值的参考资料。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值