0

I would like to ask please how to get the important features in a dataframe

# fit model to training data
xgb_model = XGBClassifier(random_state = 0 )
xgb_model.fit(X_train, y_train)

print("Feature Importances : ", xgb_model.feature_importances_) 

I know how to plot it but I want to know howw to put the 20 most important features in a dataframe or a list

Jocelyn AL
  • 67
  • 1
  • 8
  • Does this answer your question? [How do I get indices of N maximum values in a NumPy array?](https://stackoverflow.com/questions/6910641/how-do-i-get-indices-of-n-maximum-values-in-a-numpy-array) – Arne Jun 23 '21 at 20:05
  • Thanks for commenting on my question. The answer to your question is no. I want to get the features names in a list so I can created a dataframe of only these features. – Jocelyn AL Jun 24 '21 at 10:40
  • My idea was to get the indices of the 20 highest values of `xgb_model.feature_importances_` as shown in the answers to the question linked above. Then you could use these indices to get the names of the most important features from your list of feature names, provided the names in the list are in the same order as the columns of `X_train`. Or you could index the dataframe directly with `df.iloc()`. – Arne Jun 24 '21 at 15:12

0 Answers0