当前位置:   article > 正文

Python实现岭回归_python使用excel进行岭回归

python使用excel进行岭回归
  1. import numpy as np
  2. import pandas as pd
  3. import seaborn as sns
  4. import matplotlib.pyplot as plt
  5. from sklearn.linear_model import RidgeCV
  6. from sklearn.linear_model import Ridge
  7. from statsmodels.stats.outliers_influence import variance_inflation_factor
  8. sns.set_theme()# 设置风格
  9. %config InlineBackend.figure_format = 'retina' # 让图片更清晰
  10. df = pd.read_excel('D:/ml_data/auto-mpg.xlsx')# 导入数据
  11. # print(df)
  12. df_scaled = (df-df.mean())/(df.std())
  13. xs = df_scaled.iloc[:,1:]
  14. ys = df_scaled.iloc[:,0]
  15. X = xs
  16. vif=pd.DataFrame()
  17. vif["VIF Factor"] = [variance_inflation_factor(X.values,i) for i in range(X.shape[1])]
  18. vif["features"]=X.columns
  19. print(vif.round(1))

VIF Factor features

0 10.7 Cylinders

1 21.8 Displacement

2 9.9 Horsepower

3 10.8 Weight

4 2.6 Acceleration

5 1.2 Model Year

6 1.8 Origin

  1. coef=[]
  2. alphas = np.linspace(0.1,300,300)
  3. for a in alphas:
  4. ridgereg=Ridge(alpha=a)
  5. ridgereg.fit(xs,ys)
  6. coef.append(ridgereg.coef_)
  7. fig=plt.figure()
  8. ax=fig.add_subplot(111)
  9. ax.plot(alphas,coef)
  10. ax.set_xlabel('Alpha (Regularization Parameter)')
  11. ax.set_ylabel('Beta (Predictor Coefficients)')
  12. ax.set_title('Ridge Coefficients vs Regularization Parameters')
  13. ax.axis('tight')
  14. ax.legend(list(xs.keys()),loc='best')
  15. plt.savefig("Ridge.png", dpi = 600, bbox_inches = 'tight')
  16. plt.show()
  1. regr_cv=RidgeCV(alphas = alphas)
  2. model_cv=regr_cv.fit(xs,ys)
  3. print(model_cv.alpha_)

2.106020066889632

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号