# Feature Importance from GridSearchCV

I created a GridSearchCV for a Random Forest Regressor. Now I want to check the feature importance. I searched around and I found this:

rf_gridsearch.best_estimator_.named_steps.feature_importances_


This already works, but my training data is huge, 669 attributes. Therefore, I need the attribute names. So I found this code:

rf_gridsearch.best_estimator_.named_steps["step_name"].feature_importances_


But I don't know what the "named_steps["step_name"]" are.

I tried something like this:

named_steps = X_train.columns


But this doesn't work. Could somebody explain me what named_steps["step_name"] is?

I think that you just need:

feature_importances = rf_gridsearch.best_estimator_.feature_importances_


This provides the feature importance for all the attributes in your dataset. For more information on this as well as other options, you may also refer to the Scikit-learn official documentation.

• I already tried that and i get this – ml_learner Jan 27 '20 at 12:23
• array([1.15007706e-02, 1.52749118e-02, 4.92813973e-03, 5.79714037e-03, .... for 669 Attributes. I want also the column name for the results – ml_learner Jan 27 '20 at 12:24
• The order of this values are the same as the order in the dataset. So the column names are just the X.columns – Giannis Krilis Jan 27 '20 at 13:09
• Yes, that's right! – Giannis Krilis Jan 28 '20 at 9:40
• @ml_learner I believe this answers your query. If so, please mark it as an Accepted Answer. That shall help others in future. :) – Random Nerd Jan 30 '20 at 14:28

This I how did to tie the feature importance values to column names

hd = list(XData.columns)
for i, f in zip(hd, best_result.best_estimator_.feature_importances_):
print(i,round(f*100,2))