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)

最新文章

  1. js如何求一组数中的极值
  2. Deep Learning 11_深度学习UFLDL教程:数据预处理(斯坦福大学深度学习教程)
  3. centos下安装node js
  4. HDU 2199 Can you solve this equation? (二分 水题)
  5. iOS8之后CoreLocation定位的使用
  6. COJ 0538 购物问题
  7. 在 iPad和 iPhone的浏览器上查看网页源代码
  8. HeadFirst设计模式 之 C++实现(二):Observer(观察者模式)
  9. android脚步---UI界面修改,关于activity中增加按钮和监听
  10. onload、DOMContentLoaded与性能问题
  11. es6+require混合开发,兼容es6 module,import,export
  12. JWT(JSON Web Token) 多网站的单点登录,放弃session
  13. OAF开发概念和案例总结(项目总结)
  14. angularjs html 转义
  15. 133、 Android 自动化测试(转载)
  16. 2018年4月中旬的PTA(三)
  17. Spark 在Hadoop HA下配置HistoryServer问题
  18. 超强OCR文字识别软件首选ABBYY FineReader
  19. Linux下文件的打包、解压缩指令——tar,gzip,bzip2,unzip,rar
  20. scala 时间格式转换(String、Long、Date)

热门文章

  1. UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in range(128)
  2. 截止2017年,最新的全国行政区划代码数据源(xml)
  3. 微软MSDN原版Windows Server 2008 R2 With SP1下载
  4. Ubuntu 12.04 LTS 查看网关地址方法汇总
  5. golang实现的简单优先队列
  6. Promise.all处理多个异步请求
  7. jar包和war包的介绍和区别
  8. Quartus Linux usbblaster
  9. python守护进程
  10. scrapy 快速入门