5

I built a decision tree and it also gives me the feature importance for my classification. But how can I tell my program to give me the feature importance for each of my classes? To give me the overall feature importance I have this code:

importances = tree.feature_importances_
#std = np.std([tree.feature_importances_ for tree in forest.estimators_],
            # axis=0)
indices = np.argsort(importances)[::-1]

# Print the feature ranking
print("Feature ranking:")

for f in range(X.shape[1]):
    print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))

# Plot the feature importances of the forest
plt.figure()
plt.title("Feature importances")
plt.bar(range(X.shape[1]), importances[indices],
       color="r", yerr=std[indices], align="center")
plt.xticks(range(X.shape[1]), [feature_cols[i] for i in indices])
plt.xlim([-1, X.shape[1]])
plt.show()

I have four classes - 0, 1, 2, 3. Does someone know a solution?

maybeyourneighour
  • 494
  • 2
  • 4
  • 13
  • I'm not sure I understand the issue, isn't feature importance giving you an array for each feature? As said by the scikit-learn docs: `Returns: feature_importances_ : array, shape = [n_features]` – Celius Stingher Aug 14 '19 at 14:53
  • 1
    Yes, for each feature. But I want the feature importance for each class. So, I want the information for which class which feature is the most important one – maybeyourneighour Aug 14 '19 at 15:00
  • @maybeyourneighour I was looking for the same thing and it seems that LIME package is the best solution https://github.com/marcotcr/lime – Anna Yashina Jul 13 '20 at 08:56

0 Answers0