解析python实现Lasso回归

yipeiwu_com5年前Python基础

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设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

SublimeText 2编译python出错的解决方法(The system cannot find the file specified)

[Error 2] The system cannot find the file specified 解决方法:1.环境变量path添加:C:\Python32\Tools\Scrip...

Python中常见的异常总结

一、异常错误    a、语法错误 错误一: if 错误二: def  text:       pass...

粗略分析Python中的内存泄漏

引子 之前一直盲目的认为 Python 不会存在内存泄露, 但是眼看着上线的项目随着运行时间的增长 而越来越大的内存占用, 我意识到我写的程序在发生内存泄露, 之前 debug 过 lo...

Python实现定制自动化业务流量报表周报功能【XlsxWriter模块】

Python实现定制自动化业务流量报表周报功能【XlsxWriter模块】

本文实例讲述了Python实现定制自动化业务流量报表周报功能。分享给大家供大家参考,具体如下: 一 点睛 本次实践通过定制网站5个频道的流量报表周报,通过XlsxWriter 模块将流量...

Python后台开发Django的教程详解(启动)

Python后台开发Django的教程详解(启动)

Django版本为:2.1.7 Python的web框架,MTV思想 MVC Model(模板文件,数据库操作)  view(视图模板文件  )controller(...