10

I am struggling to pull out the feature importances from my RandomForestRegressor, I get an:

AttributeError: 'GridSearchCV' object has no attribute 'feature_importances_'.

Anyone know why there is no attribute? According to documentation there should exist this attribute?

The full code:

from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV

#Running a RandomForestRegressor GridSearchCV to tune the model.
parameter_candidates = {
    'n_estimators' : [650, 700, 750, 800],
    'min_samples_leaf' : [1, 2, 3],
    'max_depth' : [10, 11, 12],
    'min_samples_split' : [2, 3, 4, 5, 6]
}

RFR_regr = RandomForestRegressor()
CV_RFR_regr = GridSearchCV(estimator=RFR_regr, param_grid=parameter_candidates, n_jobs=5, verbose=2)
CV_RFR_regr.fit(X_train, y_train)

#Predict with testing set
y_pred = CV_RFR_regr.predict(X_test)

#Extract feature importances
importances = CV_RFR_regr.feature_importances_
Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
Svarto
  • 603
  • 2
  • 8
  • 20

2 Answers2

18

You are trying to use the attribute on the GridSearchCV object. Its not present there. What you actually need to do is to access the estimator on which the grid search is done.

Access the attribute by :

importances = CV_RFR_regr.best_estimator_.feature_importances_
Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
-1

///

clf = RandomForestClassifier() clf.fit(df.drop('name', axis=1), df['name'])

plt.figure(figsize=(10,10)) plt.bar(df.drop('name', axis=1).columns, height=clf.feature_importances_, bottom = 0, width=0.8) plt.xticks(rotation=80)

hight_rate_col = df.drop('name', axis=1).columns[clf.feature_importances_ > 0.1] x_train_rate, x_test_rate, y_train_rate, y_test_rate = train_test_split(df[hight_rate_col], df['name'])

///

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 16 '22 at 21:59