title: Access Random Forest Features Names Attribute in a scikit learn Pipeline after Feature Selection
body: <p>I'm running Random Forest Classifier in a Dataset, as a step of a sklearn pipeline.</p>
  <pre><code># Numerical
  numeric_cols = ['p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7']
  numeric_transformer = Pipeline(
      steps=[(&quot;scaler&quot;, StandardScaler())]
  )
  
  # Categorical
  categ_cols = ['p8', 'p9', 'p10', 'p11', 'p12', 'p13']
  categ_transformer = OneHotEncoder(handle_unknown=&quot;ignore&quot;)
  
  # Preprocessing
  preprocessor = ColumnTransformer(
      transformers=[
          (&quot;num&quot;, numeric_transformer, numeric_cols),
          (&quot;cat&quot;, categ_transformer, categ_cols),
      ]
  )
  
  rf_pipe = Pipeline(
      steps=[(&quot;preprocessor&quot;, preprocessor), 
             (&quot;feature_selection_var&quot;, VarianceThreshold()),
             (&quot;feature_selection_percentile&quot;, SelectPercentile(f_classif, percentile=90)),
             (&quot;classifier&quot;, (RandomForestClassifier(n_jobs=-1, class_weight='balanced',
                                                   criterion='entropy', max_features=10,
                                                   min_samples_leaf=50, n_estimators=1000)))]
  )
  cross_score = cross_val_score(rf_pipe, x_train_up, y_train_up, cv=10, scoring='roc_auc', n_jobs=-1)
  print(f'cross_mean: {cross_score.mean()}, cross_std: {cross_score.std()}')
  rf_pipe.fit(x_train_up, y_train_up)
  </code></pre>
  <p>I want to plot the RFC attribute <code>feature_importances_</code> but because my pipeline does feature selection, I can't identify the feature names used in fit method. So what I know after the <code>One Hot Encoder</code>, is that array X contains 31 features. Then after <code>SelectPercentile</code> array X contains 27 features that are used in RFC.</p>
  <p>How can I identify which features were chosen and fitted in RFC? When I access the RFC attributes, I only can have the numbers about feature importance, the names are not available.</p>
  <pre><code>rf_pipe.named_steps['classifier'].feature_importances_
  
  out: array([8.41159321e-02, 1.23094971e-01, 1.62218154e-02, 3.34926745e-01,
         1.06620128e-01, 1.37351967e-01, 9.39408084e-03, 1.74327442e-02,
         1.62594558e-02, 1.66887184e-04, 1.66724711e-02, 7.06176017e-03,
         6.81514535e-03, 1.11633257e-02, 1.32052716e-02, 3.72520454e-03,
         3.64255314e-03, 1.25925324e-02, 1.12110261e-02, 9.37540757e-04,
         7.53327441e-03, 7.30348346e-03, 1.40424287e-02, 2.04903820e-03,
         1.73613154e-02, 9.33500153e-03, 9.76390164e-03])
  </code></pre>
  <pre><code>rf_pipe.named_steps['classifier'].feature_names_in_
  
  out: 
  ---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  C:\Users\PHELIP~1.SOA\AppData\Local\Temp/ipykernel_10268/205801647.py in &lt;module&gt;
  ----&gt; 1 rf_pipe.named_steps['classifier'].feature_names_in_
  
  AttributeError: 'RandomForestClassifier' object has no attribute 'feature_names_in_'
  </code></pre>