Lasso原理

Lasso与弹性拟合比较python实现

import numpy as np

import matplotlib.pyplot as plt

from sklearn.metrics import r2_score

#def main():

# 产生一些稀疏数据

np.random.seed(42)

n_samples, n_features = 50, 200

X = np.random.randn(n_samples, n_features) # randn(...)产生的是正态分布的数据

coef = 3 * np.random.randn(n_features) # 每个特征对应一个系数

inds = np.arange(n_features)

np.random.shuffle(inds)

coef[inds[10:]] = 0 # 稀疏化系数--随机的把系数向量1x200的其中10个值变为0

y = np.dot(X, coef) # 线性运算 -- y = X.*w

# 添加噪声:零均值,标准差为 0.01 的高斯噪声

y += 0.01 * np.random.normal(size=n_samples)

# 把数据划分成训练集和测试集

n_samples = X.shape[0]

X_train, y_train = X[:n_samples // 2], y[:n_samples // 2]

X_test, y_test = X[n_samples // 2:], y[n_samples // 2:]

# 训练 Lasso 模型

from sklearn.linear_model import Lasso

alpha = 0.1

lasso = Lasso(alpha=alpha)

y_pred_lasso = lasso.fit(X_train, y_train).predict(X_test)

r2_score_lasso = r2_score(y_test, y_pred_lasso)

print(lasso)

print("r^2 on test data : %f" % r2_score_lasso)

# 训练 ElasticNet 模型

from sklearn.linear_model import ElasticNet

enet = ElasticNet(alpha=alpha, l1_ratio=0.7)

y_pred_enet = enet.fit(X_train, y_train).predict(X_test)

r2_score_enet = r2_score(y_test, y_pred_enet)

print(enet)

print("r^2 on test data : %f" % r2_score_enet)

plt.plot(enet.coef_, color='lightgreen', linewidth=2,

label='Elastic net coefficients')

plt.plot(lasso.coef_, color='gold', linewidth=2,

label='Lasso coefficients')

plt.plot(coef, '--', color='navy', label='original coefficients')

plt.legend(loc='best')

plt.title("Lasso R^2: %f, Elastic Net R^2: %f"

% (r2_score_lasso, r2_score_enet))

plt.show()

运行结果

总结

以上所述是小编给大家介绍的python实现Lasso回归,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

python lasso回归分析_解析python实现Lasso回归相关推荐

  1. python方法定义..._解析Python类中的方法定义

    最近在学习类过程中,绑定方法这个概念没有理解透彻,所以在网上找了很多相关博客.文章研究到底是怎么一回事.因为有的文章所陈述与我在python3.5版本实际实验中有些出入,所以经过实践后总结出以下结论. ...

  2. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  3. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  4. 用python做lasso回归_解析python实现Lasso回归

    Lasso原理 Lasso与弹性拟合比较python实现 import numpy as np import matplotlib.pyplot as plt from sklearn.metrics ...

  5. python 参数封装_扩展Python模块系列(三)----参数解析与结果封装

    在上一节中,通过一个简单的例子介绍了C语言扩展Python内建模块的整体流程,从本节开始讲开始深入讨论一些细节问题,在细节讨论中从始至终都会涉及[引用计数]的问题.首先讨论C语言封装的Python函数 ...

  6. python中3or5什么意思_解析python中and与or用法

    在Python 中,and 和 or 执行布尔逻辑演算,如你所期待的一样,但是它们并不返回布尔值:而是,返回它们实际进行比较的值之一.>>> 1 and 2 and 3 3 > ...

  7. python 网页编程_通过Python编程检索网页

    python 网页编程 The internet and the World Wide Web (WWW), is probably the most prominent source of info ...

  8. python机器学习预测_使用Python和机器学习预测未来的股市趋势

    python机器学习预测 Note from Towards Data Science's editors: While we allow independent authors to publish ...

  9. 阿里云大学考试python中级题目及解析-python中级

    阿里云大学考试python中级题目及解析 1.WEB开发中,下列选项中能够实现客户端重定向的设置是() A.响应头设置Location+状态码200 B.响应头设置Location+状态码302 C. ...

  10. python 创意项目_针对python开发人员的10个很棒的python项目创意

    python 创意项目 The joy of coding Python should be in seeing short, concise, readable classes that expre ...

最新文章

  1. 深挖谷歌 DeepMind 和它背后的技术
  2. 江湖永在:金庸先生和阿里人的那些记忆
  3. centos7装机教程
  4. Shift and Reverse
  5. Simple Mail Transfer Protocol --- SMTP协议
  6. android 5.0 开启网卡 权限请求,Aurora Droid | F-Droid - Free and Open Source Android App Repository...
  7. platform_get_resource
  8. python中idle什么意思_python的idle是什么
  9. 定义一个复数类Complex,使得下面的代码能够工作:
  10. 数据中心交换机基础自学系列 | 汇总
  11. uniapp开发写了key 但微信小程序时警告-Now you can provide attr `wx:key` for a `wx:for` to improve performance.
  12. 苹果砸烂“密薪制”的枷锁
  13. 考研政治和数学的思维导图(2020)
  14. Qt虚拟键盘相关内容
  15. 爬虫天坑系列-百度指数爬虫
  16. 做程序员攒钱10年,够一百万左右回农村存银行一个月6千利息现实吗?
  17. 【C#】QQ消息自动发送代码
  18. java 面试 sql_sql 面试问题
  19. 诺基亚手机使用GPS功能费用问题的解释
  20. fatfs文件系统的宏定义使用

热门文章

  1. Git - 命令行 - 使用 Tag 标记你的代码
  2. matlab模式识别大作业_史上最萌最认真的机器学习/深度学习/模式识别入门指导手册(二)...
  3. $.post 提交文件_PHP表单提交
  4. 指针的意义和linux的内存回收艺术
  5. 《Linux指令从入门到精通》——4.4 Linux下的文本编辑指令
  6. shell 后台运行程序并写pid文件
  7. IOS 获取软件版本号的方法
  8. IOS小工具以及精彩的博客
  9. 使用getopt函数对命令行短形参进行处理
  10. 11月7日简单标签与Listener培训日记