title: Random Forest Feature Importance Python
body: <p>I am trying to get the feature importance from my data after performing hyperparameter tuning and getting the best parameters for my classifier. I have also fitted my best parameters to the training set and now I am trying to get the important features but I keep getting errors and have tried every possible solution I found on the internet.</p>
  <p>View my code below;</p>
  <pre><code>enter code here
  # define models and parameters for hyperparametrs
  from sklearn.experimental import enable_halving_search_cv  
  from sklearn.model_selection import HalvingGridSearchCV
  
  # define grid search
  
  from sklearn.model_selection import GridSearchCV
  # Create the parameter grid based on the results of random search 
  param_grid = {
      'bootstrap': [True],
      'max_features': ['auto','sqrt'],
      'n_estimators': [100,1000]
  }
  
  
  # Create a based model
  rf = RandomForestClassifier()
  # Instantiate the grid search model
  grid_search = HalvingGridSearchCV(estimator = rf, param_grid = param_grid, 
                            cv = 3, n_jobs = -1, verbose = 2)
  cv = StratifiedKFold(n_splits=10, shuffle = True, random_state=42)
  
  
  
  steps_3 = [('over', RandomOverSampler()),  ('chi_square', SelectKBest(chi2, k=7000)), ('estimator', grid_search)]
  pipeline_3 = Pipeline(steps=steps_3)
  #fit the model
  rf_hyperparameter = pipeline_3.fit(X_train, y_train)
  print(rf_hyperparameter)
  
  # print('Best parameter set: %s' % grid_search.best_params_)
  print(&quot;Best Score:&quot; + str(grid_search.best_score_))
  print(&quot;Best Parameters: &quot; + str(grid_search.best_params_))
  best_parameters = grid_search.best_params_
  
  #fit the best parameters to the training data
  rf_best = RandomForestClassifier(bootstrap = True, max_features= 'auto', n_estimators = 1000)
  rf_best.fit(X_train, y_train)
  
  feature_importances = pd.DataFrame(rf_best.feature_importances_, 
                                     index=X_train.columns,columns=['importance']).sort_values('importance',ascending = False)
  feature_importances
  </code></pre>
  <p>After running the above code, this is the error I get</p>
  <hr />
  <pre><code>AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-159-563c7c3e7fc5&gt; in &lt;module&gt;
        1 feature_importances = pd.DataFrame(rf_best.feature_importances_, 
  ----&gt; 2                                    index=X_train.columns,columns=['importance']).sort_values('importance',ascending = False)
        3 feature_importances
  
  AttributeError: 'numpy.ndarray' object has no attribute 'columns'
  </code></pre>
  <p>I will really appreciate any input I can get. Thanks!</p>