title: XGBoost feature importance: How do I get original variable names after encoding
body: <p>I am using XGBoost classification following the guide from a DataCamp course. The data was treated as follows:</p>
  
  <pre><code>X, y = df.iloc[:,:-1], df.iloc[:,-1]
  
  # Create a boolean mask for categorical columns: check if df.dtypes == object
  categorical_mask = (X.dtypes == object)
  
  # Get list of categorical column names
  categorical_columns = X.columns[categorical_mask].tolist()
  
  # Create LabelEncoder object: le
  le = LabelEncoder()
  
  # Apply LabelEncoder to categorical columns
  X[categorical_columns] = X[categorical_columns].apply(lambda x: le.fit_transform(x))
  
  # Create OneHotEncoder: ohe
  ohe = OneHotEncoder(categorical_features=categorical_mask, sparse=False)
  
  # Apply OneHotEncoder to categorical columns - output is no longer a dataframe: df_encoded is a NumPy array
  X_encoded = ohe.fit_transform(X)
  
  testy = pd.DataFrame(X_encoded)
  
  X_train, X_test, y_train, y_test= train_test_split(testy, y, test_size=0.2, random_state=123)
  
  DM_train = xgb.DMatrix(X_train, label = y_train, )
  DM_test = xgb.DMatrix(X_test, label = y_test)
  </code></pre>
  
  <p>I tuned the hyperparameters with cross-validated grid search and I fit the models with the <code>x_train</code> and <code>y_train</code>.</p>
  
  <p>I fit the model with the tuned parameters and then created the Feature Importance Plot:</p>
  
  <pre><code>model.fit(X_train,y_train)
  
  xgb.plot_importance(model, importance_type = 'gain')
  </code></pre>
  
  <p>This is the output:</p>
  
  <p><a href="https://i.stack.imgur.com/mAdT2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mAdT2.png" alt="Feature Importance Plot"></a></p>
  
  <p>How do I map these features back to the original data? I am confused because I used both <code>LabelEncoder()</code> and <code>OneHotEncoder()</code>.</p>
  
  <p>Any help is much appreciated. </p>