码迷,mamicode.com
首页 > 其他好文 > 详细

岭回归

时间:2020-02-01 12:58:02      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:cti   nes   col   idg   --   int   shuffle   mat   tar   

1 from sklearn.model_selection import train_test_split
2 from sklearn.linear_model import LinearRegression
3 from sklearn.datasets import load_diabetes
4 X,y=load_diabetes().data,load_diabetes().target
5 X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=8)
6 lr=LinearRegression().fit(X_train,y_train)
7 print("the coefficient:{}".format(lr.coef_))
8 print(the intercept:{}.format(lr.intercept_))
9 print("the score of this model:{:.3f}".format(lr.score(X_test,y_test)))
 1 from sklearn.linear_model import Ridge
 2 ridge=Ridge().fit(X_train,y_train)
 3 print("the coefficient:{}".format(ridge.coef_))
 4 print(the intercept:{}.format(ridge.intercept_))
 5 print("the score of this model:{:.3f}\n".format(ridge.score(X_test,y_test)))
 6 
 7 ridge10=Ridge(alpha=10).fit(X_train,y_train)
 8 print("the coefficient:{}".format(ridge10.coef_))
 9 print(the intercept:{}.format(ridge10.intercept_))
10 print("the score of this model:{:.3f}\n".format(ridge10.score(X_test,y_test)))
11 
12 ridge01=Ridge(alpha=0.1).fit(X_train,y_train)
13 print("the coefficient:{}".format(ridge01.coef_))
14 print(the intercept:{}.format(ridge01.intercept_))
15 print("the score of this model:{:.3f}\n".format(ridge01.score(X_test,y_test)))
 1 import matplotlib.pyplot as plt
 2 plt.plot(ridge.coef_,s,label=Ridge alpha=1)
 3 plt.plot(ridge10.coef_,^,label=Ridge alpha=10)
 4 plt.plot(ridge01.coef_,v,label=Ridge alpha=0.1)
 5 plt.plot(lr.coef_,o,label=Linear Regression)
 6 plt.xlabel("coeffient index")
 7 plt.ylabel("coeffient magnitude")
 8 plt.hlines(0,0,len(lr.coef_))
 9 plt.legend()
10 plt.show()
 1 import numpy as np
 2 from sklearn.model_selection import learning_curve,KFold
 3 def plot_learning_curve(est,X,y):
 4     training_set_size,train_scores,test_scores=learning_curve(
 5         est,X,y,train_sizes=np.linspace(.1,1,20),cv=KFold(20,shuffle=True,random_state=1)
 6     )
 7     estimator_name=est.__class__.__name__
 8     line=plt.plot(training_set_size,train_scores.mean(axis=1),--,label="training "+estimator_name)
 9     plt.plot(training_set_size,test_scores.mean(axis=1),-,label="test "+estimator_name,c=line[0].get_color())
10     plt.xlabel("Training set size")
11     plt.ylabel("Score")
12     plt.ylim(0,1.1)
1 plot_learning_curve(Ridge(alpha=1),X,y)
2 plot_learning_curve(LinearRegression(),X,y)
3 plt.legend(loc=(0,1.05),ncol=2,fontsize=11)
4 plt.show()

 

岭回归

标签:cti   nes   col   idg   --   int   shuffle   mat   tar   

原文地址:https://www.cnblogs.com/St-Lovaer/p/12245963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!