首页 > 其他 > 详细

套索回归

时间:2020-02-01 13:14:34      阅读:79      评论:0      收藏:0      [点我收藏+]
 1 from sklearn.model_selection import train_test_split
 2 from sklearn.datasets import load_diabetes
 3 X,y=load_diabetes().data,load_diabetes().target
 4 X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=8)
 5 
 6 from sklearn.linear_model import Lasso
 7 import numpy as np
 8 lasso=Lasso().fit(X_train,y_train)
 9 print("the coefficient:{}".format(lasso.coef_))
10 print(the intercept:{}.format(lasso.intercept_))
11 print("the score of this model:{:.3f}".format(lasso.score(X_test,y_test)))
12 print("the model uses {}".format(np.sum(lasso.coef_!=0))+" features\n")
 1 lasso01=Lasso(alpha=0.1,max_iter=100000).fit(X_train,y_train)
 2 print("the coefficient:{}".format(lasso01.coef_))
 3 print(the intercept:{}.format(lasso01.intercept_))
 4 print("the score of this model:{:.3f}".format(lasso01.score(X_test,y_test)))
 5 print("the model uses {}".format(np.sum(lasso01.coef_!=0))+" features\n")
 6 
 7 lasso001=Lasso(alpha=0.01,max_iter=100000).fit(X_train,y_train)
 8 print("the coefficient:{}".format(lasso001.coef_))
 9 print(the intercept:{}.format(lasso001.intercept_))
10 print("the score of this model:{:.3f}".format(lasso001.score(X_test,y_test)))
11 print("the model uses {}".format(np.sum(lasso001.coef_!=0))+" features\n")
12 
13 lasso0001=Lasso(alpha=0.001,max_iter=100000).fit(X_train,y_train)
14 print("the coefficient:{}".format(lasso0001.coef_))
15 print(the intercept:{}.format(lasso0001.intercept_))
16 print("the score of this model:{:.3f}".format(lasso0001.score(X_test,y_test)))
17 print("the model uses {}".format(np.sum(lasso0001.coef_!=0))+" features\n")
18 
19 lasso00001=Lasso(alpha=0.0001,max_iter=100000).fit(X_train,y_train)
20 print("the coefficient:{}".format(lasso00001.coef_))
21 print(the intercept:{}.format(lasso00001.intercept_))
22 print("the score of this model:{:.3f}".format(lasso00001.score(X_test,y_test)))
23 print("the model uses {}".format(np.sum(lasso00001.coef_!=0))+" features\n")
 1 import matplotlib.pyplot as plt
 2 plt.plot(lasso.coef_,s,label=Lasso alpha=1)
 3 plt.plot(lasso01.coef_,^,label=Lasso alpha=0.1)
 4 plt.plot(lasso001.coef_,v,label=Lasso alpha=0.01)
 5 plt.plot(lasso0001.coef_,o,label=Lasso alpha=0.001)
 6 plt.plot(lasso00001.coef_,*,label=Lasso alpha=0.0001)
 7 plt.xlabel("coeffient index")
 8 plt.ylabel("coeffient magnitude")
 9 plt.legend(loc=(0,1.05))
10 plt.show()

 

套索回归

原文:https://www.cnblogs.com/St-Lovaer/p/12245978.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!