Skip to content

Instantly share code, notes, and snippets.

@jamespaultg
Created March 16, 2018 07:15
Show Gist options
  • Save jamespaultg/969e353484433af87c43811868ee4098 to your computer and use it in GitHub Desktop.
Save jamespaultg/969e353484433af87c43811868ee4098 to your computer and use it in GitHub Desktop.
Decision tree and feature importance
from sklearn.tree import DecisionTreeClassifier, export_graphviz
tree = DecisionTreeClassifier(max_depth=3,random_state=0)
tree.fit(X_train,y_train)
plt.figure(figsize=(20, 10))
indices = np.argsort(tree.feature_importances_)[::-1]
#indices = np.argsort(tree.feature_importances_)[::1]
# Visualise the importance of the features
# To get your top 10 feature names
features_sorted = []
for i in range(10):
features_sorted.append(features[indices[i]])
# Now plot
plt.figure()
plt.barh(np.arange(1,11,1), tree.feature_importances_[indices[range(10)]], color='blue', align='center')
plt.yticks(np.arange(1,11,1),features_sorted, rotation='horizontal',fontsize=10)
plt.gca().invert_yaxis()
plt.show();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment