岭回归
Posted st-lovaer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了岭回归相关的知识,希望对你有一定的参考价值。
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} ".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} ".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} ".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()
以上是关于岭回归的主要内容,如果未能解决你的问题,请参考以下文章