I am trying to get a bar plot for feature importance in the XGBoost classifier. It should have worked but it didn't. I tried too many times. Can you check the code below and tell me what is wrong with it?
feat_import=clf.feature_importances_
feat_names=X.columns
sorted_idx=clf.feature_importances_.argsort()[-20:]
plt.barh(feat_names[sorted_idx], clf.feature_importances_[sorted_idx])
It takes the features that the most important ones. However, it plots them unsorted.
When I use just numbers instead of column names I take the sorted bar graph.
plt.barh(range(20),feat_import[sorted_idx])
I couldn't figure out the problem here.