Python: 'GridSearchCV' object has no attribute 'coef_'

Viewed 2641

I am trying to return the coefficients of my logistic regression model. Here is how I created my model:

logreg = LogisticRegression(solver = 'liblinear')

model = GridSearchCV(logreg, cv = 3, param_grid = {
    'penalty': ('l1', 'l2'),
    'C': [0.5, 0.6, 0.7, 0.8, 0.9],
    'max_iter': [100]
})

model.fit(X_train, y_train)
model.coef_ # here is where I get the error
# Validation (test)
y_pred = model.predict(X_test)

But I get the following error:

AttributeError: 'GridSearchCV' object has no attribute 'coef_'

I even tried the .best_score_ and other functions to see if I could find the coefficients in another way. But no luck. Do you know how I can fix this?

2 Answers

You have to select a specific estimator to access the coef_ property. Try:

model.best_estimator_.coef_

The GridSearchCV object itself doesn't have a coefficient, because it's not an estimator, it's an object that cycles through parameters and trains various estimators.

I think you're looking for the best model provided by GridSearchCV:

...
model.fit(X_train, y_train)
best_model = model.best_estimator_
best_model.coef_ # This should be what you're looking for

y_pred = best_model.predict(X_test)

Your model is simply a GridSearchCV object whereas coef_ is an attribute of a logreg object. The best_estimator_ attribute is the estimator with highest accuracy found during grid search.

Related