【发布时间】:2021-01-17 19:03:07
【问题描述】:
我正在使用 Python 3.6.5 和 scikit-learn 0.23.2
from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV, cross_val_score
ridge = Ridge()
r_parameters = {'alpha':[1e-15, 1e-10, 1e-8, 1e-4, 1e-3, 1e-2, 1, 5, 10, 20]} # this is the Ridge regressor penalty, across different values
ridge_regressor = GridSearchCV(ridge, r_parameters, scoring = 'neg_mean_squared_error', cv = 5)
ridge_regressor.fit(X,y)
ridge_best_params_ = ridge_regressor.best_params_
ridge_best_score_ = -ridge_regressor.best_score_
这成功地为我提供了 best_params_ 和 best_score_ 值。意思是.fit 已经跑了。
refit 没有调整,所以应该是默认的refit = True
但是,在尝试返回我的岭回归模型的系数时:
for coef, col in enumerate(X.columns):
print(f"{col}: {ridge.coef_[coef]}")
它导致我出现以下错误:
AttributeError Traceback (most recent call last)
<ipython-input-7-e59d1af522dc> in <module>
2
3 for coef, col in enumerate(X.columns):
----> 4 print(f"{col}: {ridge.coef_[coef]}")
AttributeError: 'Ridge' object has no attribute 'coef_
感谢任何帮助。
【问题讨论】:
-
ridge_regressor已安装,因此它应该具有coef_属性 -
@FBruzzesi 你的意思是如果我使用
for coef, col in enumerate(X.columns): print(f"{col}: {ridge_regressor.coef_[coef]}")那么我应该得到系数吗? -
试试
for coef, col in enumerate(X.columns): print(f"{col}: {ridge_regressor.best_estimator_.coef_[coef]}") -
@FBruzzesi 从使用
ridge.coef_更改为ridge_regressor.coef_后,我得到了一个新的错误代码,而不是TypeError: 'Ridge' object is not subscriptable
标签: python python-3.x scikit-learn grid-search