I am using Python 3.6.5 and 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_
This has successfully provided me the best_params_ and best_score_ values. Meaning .fit
has ran.
refit has not been adjusted, and hence it should be default refit = True
However when it comes to trying to return the coefficients of my ridge regression model:
for coef, col in enumerate(X.columns):
print(f"{col}: {ridge.coef_[coef]}")
It leads me to the error below:
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_
Appreciate any help for this.
ridge
(theRidge
instance) doesn't actually get fitted when fittingridge_regressor
(theGridSearchCV
instance); instead, clones ofridge
are fitted, and one such is saved asridge.best_estimator_
. Soridge.best_estimator_.coef_
will contain the refitted model's coefficients.Note that
GridSearchCV
does provide some convenience functions for accessing thebest_estimator_
; e.g.ridge.score
is just shorthand forridge.best_estimator_.score
, and similarly for,predict
and its variants. But it doesn't provide this kind of passthrough for all of the methods/attributes ofbest_estimator_
, andcoef_
is one of those not available.