线性模型L1正则化——套索回归

Posted st-lovaer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线性模型L1正则化——套索回归相关的知识,希望对你有一定的参考价值。

 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
")
 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
")
 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
")
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
")
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
")
 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()

 

以上是关于线性模型L1正则化——套索回归的主要内容,如果未能解决你的问题,请参考以下文章

机器学习之路: python线性回归 过拟合 L1与L2正则化

正则化项L1和L2的直观理解及L1不可导处理

机器学习中正则化项L1和L2的直观理解

H2O 不应该标准化正则化 GLM 模型(套索、岭、弹性网)的分类预测变量吗?

L1正则化和L2正则化

使用正则化 (L1 / L2) Lasso 和 Ridge 的逻辑回归模型