import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model from sklearn.model_selection import train_test_split def load_data(): diabetes = datasets.load_diabetes() return train_test_split(diabetes.data,diabetes.target,test_size=0.25,random_state=0) #Lasso回归 def test_Lasso(*data): X_train,X_test,y_train,y_test=data regr = linear_model.Lasso() regr.fit(X_train, y_train) print('Coefficients:%s, intercept %.2f'%(regr.coef_,regr.intercept_)) print("Residual sum of squares: %.2f"% np.mean((regr.predict(X_test) - y_test) ** 2)) print('Score: %.2f' % regr.score(X_test, y_test)) # 产生用于回归问题的数据集 X_train,X_test,y_train,y_test=load_data() # 调用 test_Lasso test_Lasso(X_train,X_test,y_train,y_test) def test_Lasso_alpha(*data): X_train,X_test,y_train,y_test=data alphas=[0.01,0.02,0.05,0.1,0.2,0.5,1,2,5,10,20,50,100,200,500,1000] scores=[] for i,alpha in enumerate(alphas): regr = linear_model.Lasso(alpha=alpha) regr.fit(X_train, y_train) scores.append(regr.score(X_test, y_test)) ## 绘图 fig=plt.figure() ax=fig.add_subplot(1,1,1) ax.plot(alphas,scores) ax.set_xlabel(r"$\alpha$") ax.set_ylabel(r"score") ax.set_xscale('log') ax.set_title("Lasso") plt.show() # 调用 test_Lasso_alpha test_Lasso_alpha(X_train,X_test,y_train,y_test)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理