-
Notifications
You must be signed in to change notification settings - Fork 5
/
getFeatureRankings.py
31 lines (25 loc) · 932 Bytes
/
getFeatureRankings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
Compute feature rankings for a trained classifier saved in a joblib
usage: python getFeatureRankings.py pathToClassifier
where pathToClassifier points to the .p file that was created by running the training script
"""
import sys, os
import numpy as np
import random
from sklearn.externals import joblib
from sklearn.ensemble import ExtraTreesClassifier
classifierPickleFileName = sys.argv[1]
statsToUse, header, grid_search = joblib.load(classifierPickleFileName)
importances = grid_search.best_estimator_.feature_importances_
# Print the feature ranking
print("Feature ranking from an ExtraTreesClassifier:")
outLines = []
for f in range(len(importances)):
outLines.append((importances[f], "feature %s; value: %g" % (header[f+1], importances[f])))
outLines.sort()
outLines.reverse()
featureRank = 1
for importance, outLine in outLines:
print("%d. " %(featureRank) + outLine)
featureRank += 1
print("done\n")