吴裕雄 python 机器学习——Lasso回归
2024-10-18 23:23:22
import numpy as np
import matplotlib.pyplot as plt from sklearn import datasets, linear_model
from sklearn.model_selection import train_test_split def load_data():
diabetes = datasets.load_diabetes()
return train_test_split(diabetes.data,diabetes.target,test_size=0.25,random_state=0) #Lasso回归
def test_Lasso(*data):
X_train,X_test,y_train,y_test=data
regr = linear_model.Lasso()
regr.fit(X_train, y_train)
print('Coefficients:%s, intercept %.2f'%(regr.coef_,regr.intercept_))
print("Residual sum of squares: %.2f"% np.mean((regr.predict(X_test) - y_test) ** 2))
print('Score: %.2f' % regr.score(X_test, y_test)) # 产生用于回归问题的数据集
X_train,X_test,y_train,y_test=load_data()
# 调用 test_Lasso
test_Lasso(X_train,X_test,y_train,y_test) def test_Lasso_alpha(*data):
X_train,X_test,y_train,y_test=data
alphas=[0.01,0.02,0.05,0.1,0.2,0.5,1,2,5,10,20,50,100,200,500,1000]
scores=[]
for i,alpha in enumerate(alphas):
regr = linear_model.Lasso(alpha=alpha)
regr.fit(X_train, y_train)
scores.append(regr.score(X_test, y_test))
## 绘图
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(alphas,scores)
ax.set_xlabel(r"$\alpha$")
ax.set_ylabel(r"score")
ax.set_xscale('log')
ax.set_title("Lasso")
plt.show() # 调用 test_Lasso_alpha
test_Lasso_alpha(X_train,X_test,y_train,y_test)
最新文章
- js如何求一组数中的极值
- Deep Learning 11_深度学习UFLDL教程:数据预处理(斯坦福大学深度学习教程)
- centos下安装node js
- HDU 2199 Can you solve this equation? (二分 水题)
- iOS8之后CoreLocation定位的使用
- COJ 0538 购物问题
- 在 iPad和 iPhone的浏览器上查看网页源代码
- HeadFirst设计模式 之 C++实现(二):Observer(观察者模式)
- android脚步---UI界面修改,关于activity中增加按钮和监听
- onload、DOMContentLoaded与性能问题
- es6+require混合开发,兼容es6 module,import,export
- JWT(JSON Web Token) 多网站的单点登录,放弃session
- OAF开发概念和案例总结(项目总结)
- angularjs html 转义
- 133、 Android 自动化测试(转载)
- 2018年4月中旬的PTA(三)
- Spark 在Hadoop HA下配置HistoryServer问题
- 超强OCR文字识别软件首选ABBYY FineReader
- Linux下文件的打包、解压缩指令——tar,gzip,bzip2,unzip,rar
- scala 时间格式转换(String、Long、Date)
热门文章
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in range(128)
- 截止2017年,最新的全国行政区划代码数据源(xml)
- 微软MSDN原版Windows Server 2008 R2 With SP1下载
- Ubuntu 12.04 LTS 查看网关地址方法汇总
- golang实现的简单优先队列
- Promise.all处理多个异步请求
- jar包和war包的介绍和区别
- Quartus Linux usbblaster
- python守护进程
- scrapy 快速入门