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$

2 Answers 2

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$
5
  • $\begingroup$ I already tried that and i get this $\endgroup$
    – ml_learner
    Jan 27, 2020 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, 2020 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$ Jan 27, 2020 at 13:09
  • $\begingroup$ So i just need to sort them to figure out what the most importante score is? $\endgroup$
    – ml_learner
    Jan 27, 2020 at 13:23
  • 1
    $\begingroup$ Yes, that's right! $\endgroup$ Jan 28, 2020 at 9:40
1
$\begingroup$

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))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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