线性模型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\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()
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!