# Visualizing decision tree with feature names

from scipy.sparse import hstack
X_tr1 = hstack((X_train_cc_ohe, X_train_csc_ohe, X_train_grade_ohe,X_train_price_norm, X_train_tnppp_norm, X_train_essay_bow, X_train_pt_bow)).tocsr()
X_te1 = hstack((X_test_cc_ohe, X_test_csc_ohe, X_test_grade_ohe, X_test_price_norm, X_test_tnppp_norm, X_test_essay_bow, X_test_pt_bow)).tocsr()


X_train_cc_ohe and all are vectorized categorical data, and X_train_pt_bow is bag of words vectorized text data.
Now i applied decision tree classifier on this model, i got this.

i took max_depth as 3 just for visualization purpose.

my question is i want to get feature names in my output instead of index as X2599, X4 etc.
i know i can do it by vect.get_feature_names() as input to export_graphviz, vect is object of CountVectorizer(), since i have already merged this vectorized data using hstack, now how to get feature names in this decision tree.

You can use graphviz instead. and use the following code to view the decision tree with feature names.

import pydotplus
import sklearn.tree as tree
from IPython.display import Image

dt_feature_names = list(X.columns)
dt_target_names = [str(s) for s in Y.unique()]
tree.export_graphviz(dt, out_file='tree.dot',
feature_names=dt_feature_names, class_names=dt_target_names,
filled=True)
graph = pydotplus.graph_from_dot_file('tree.dot')
Image(graph.create_png())


This will display feature names with values, gini coefficient, sample, value and class

• This won't work, I have main problem in collecting feature names, feature names are build when the model is fitted, in my case I have total 6689 features. I got to know exact number when it throwed an error. Now I don't know how to extract features – torBhakt Mar 24 '19 at 10:57