Feature Importance from OneVsRestClassifier

Question:

As the title suggests, how can I obtain the feature importances from a OneVsRestClassifier model?

I tried using model.feature_importances_ but the error message was

"OneVsRestClassifier’ object has no attribute ‘feature_importances_"

Tried searching from the internet but was not able to find any clue.

Asked By: Chew CS

||

Answers:

OneVsRestClassifier() basically builds as much binary classifiers as there are classes. Each has its own set of importances (assuming the base classifier supports them), showing the importance of features to distinguish a certain class from all others when generalizing on the train set. Those can be accessed with .estimators_[i].feature_importances_.

Alternatively, you may study other sorts of feature importances, like sklearn.inspection.permutation_importance, which are universally applicable.

Answered By: dx2-66

Here’s my solution to get important features for a stacked classification with sklearn below. Here the stacked_model is the model built with OneVsRestClassifier :

# to get permutation: 
results = permutation_importance(stacked_model, x, y, scoring='accuracy')
# get important features:
important_features = results.importances_mean
# list all features:
for i,v in enumerate(important_features):
 print('Feature: %0d, Score: %.5f' % (i,v))
Answered By: Maryam Nasseri
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.