0
$\begingroup$

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?

$\endgroup$
3
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ I already tried that and i get this $\endgroup$ – ml_learner Jan 27 '20 at 12:23
  • $\begingroup$ 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 $\endgroup$ – ml_learner Jan 27 '20 at 12:24
  • 1
    $\begingroup$ The order of this values are the same as the order in the dataset. So the column names are just the X.columns $\endgroup$ – Giannis Krilis Jan 27 '20 at 13:09
  • 1
    $\begingroup$ Yes, that's right! $\endgroup$ – Giannis Krilis Jan 28 '20 at 9:40
  • 1
    $\begingroup$ @ml_learner I believe this answers your query. If so, please mark it as an Accepted Answer. That shall help others in future. :) $\endgroup$ – Random Nerd Jan 30 '20 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.