线性模型L2正则化——岭回归
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()
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥