当前位置:   article > 正文

Python实现ridge和lasso_"# -*- coding: utf-8 -*- \"\"\" created on mon nov

"# -*- coding: utf-8 -*- \"\"\" created on mon nov 12 17:07:16 2018 @author: wp:"
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Nov 12 17:07:16 2018
  4. @author: wp:lasso|ridge
  5. """
  6. #经典鸢尾花数据集
  7. from sklearn.datasets import load_iris
  8. iris = load_iris()
  9. data_x = iris.data
  10. data_y = iris.target
  11. #带入需要的包、库
  12. import pandas as pd
  13. import numpy as np
  14. import matplotlib.pyplot as plt
  15. from sklearn.model_selection import train_test_split
  16. from sklearn.linear_model import Ridge,RidgeCV
  17. from sklearn.linear_model import Lasso,LassoCV
  18. from sklearn.metrics import mean_squared_error
  19. x_tr,x_te,y_tr,y_te = train_test_split(data_x,data_y,train_size = 0.7,random_state =22)
  20. ######################ridge########################################
  21. #通过不同的alpha值 生成不同的ridge模型
  22. alphas = 10**np.linspace(-10,10,100)
  23. ridge_cofficients = []
  24. for alpha in alphas:
  25. ridge = Ridge(alpha = alpha, normalize=True)
  26. ridge.fit(x_tr, y_tr)
  27. ridge_cofficients.append(ridge.coef_)
  28. #画出alpha和回归系数的关系
  29. plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
  30. plt.rcParams['axes.unicode_minus'] = False
  31. # 设置绘图风格
  32. plt.style.use('ggplot')
  33. plt.plot(alphas, ridge_cofficients)
  34. plt.xscale('log')
  35. plt.axis('tight')
  36. plt.title(r'alpha系数与岭回归系数的关系')
  37. plt.xlabel('Log Alpha')
  38. plt.ylabel('Cofficients')
  39. plt.show()
  40. #ridge交叉验证
  41. ridge_cv = RidgeCV(alphas = alphas, normalize=True, cv = 10)
  42. ridge_cv.fit(x_tr, y_tr)
  43. # 取出最佳的lambda值ridge_best_alpha = ridge_cv.alpha_
  44. ridge_best_alpha = ridge_cv.alpha_ #得到最佳lambda值
  45. #基于最佳lambda值建模
  46. ridge = Ridge(alpha = ridge_best_alpha,normalize = True)
  47. ridge.fit(x_tr,y_tr)
  48. ridge_predict = ridge.predict(x_te)
  49. rmse = np.sqrt(mean_squared_error(y_te,ridge_predict))
  50. ######################lasso##################################
  51. # LASSO回归模型的交叉验证
  52. lasso_cv = LassoCV(alphas = alphas, normalize=True, cv = 10, max_iter=10000)
  53. lasso_cv.fit(x_tr,y_tr)
  54. # 取出最佳的lambda值
  55. lasso_best_alpha = lasso_cv.alpha_
  56. lasso_best_alpha
  57. #基于最佳lambda值建模
  58. lasso = Lasso(alpha = lasso_best_alpha, normalize=True, max_iter=10000)
  59. lasso.fit(x_tr, y_tr)
  60. lasso_predict = lasso.predict(x_te) #预测
  61. RMSE = np.sqrt(mean_squared_error(y_te,lasso_predict))

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/330996
推荐阅读
相关标签
  

闽ICP备14008679号