Search code examples
pythonscikit-learngrid-search

Get feature importance from GridSearchCV


Is there a way to get feature importance from a sklearn's GridSearchCV?

For example :

from sklearn.model_selection import GridSearchCV
print("starting grid search ......")
optimized_GBM = GridSearchCV(LGBMRegressor(),
                             params,
                             cv=3,
                             n_jobs=-1)
# 
optimized_GBM.fit(tr, yvar)
preds2 = optimized_GBM.predict(te)

Is there a way I can access feature importance ?

Maybe something like

optimized_GBM.feature_importances_

Solution

  • Got it. It goes something like this :

    optimized_GBM.best_estimator_.feature_importance()
    

    if you happen ran this through a Pipeline and receive object has no attribute 'feature_importance' try optimized_GBM.best_estimator_.named_steps["step_name"].feature_importances_

    where step_name is the corresponding name in your pipeline