0

All the training tutorials that I see great a graph_viz export on the training data for some reason. I can get that to work and here is my code:

from sklearn.tree import DecisionTreeClassifier
DT = DecisionTreeClassifier(criterion = 'gini', max_depth = 5, 
max_features='sqrt')
Decision_Tree = DT.fit(X_train, y)
Y_pred=Decision_Tree.predict(X_test)

from io import StringIO
dot_data = StringIO()
export_graphviz(Decision_Tree, out_file='tree.dot',  
filled=True, rounded=True,
impurity=False,feature_names=X_test.columns,class_names=['Average','Won- 
Booked'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
from subprocess import call
call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=60'])

This all works and does not give me any error message however it creates a decision tree on training data, instead of test data. I think I need to adjust what I put into export_graphviz instead of my current "Decision_Tree" but I am not quiet sure what?

bernando_vialli
  • 947
  • 4
  • 12
  • 27
  • `export_graphviz` draws the learned model (tree) which contains the conditions on which the tree splits the features. Which obviously be done on the training data. During testing, it just follows those rules to categorise the test data. So what do you want to draw? Do you want to draw the path of a testing sample (what that sample followed to be classified in a class)? – Vivek Kumar Sep 18 '18 at 07:02
  • Maybe you need something like this: http://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html#sphx-glr-auto-examples-tree-plot-unveil-tree-structure-py – Vivek Kumar Sep 18 '18 at 07:07
  • Yes, I want to see the path of the predictions (essentially the same thing as with the data that is trained, but on data that I am predicting it on). I would think it should be a minor change? – bernando_vialli Sep 18 '18 at 12:32
  • The solution is here: https://stackoverflow.com/questions/55878247/how-to-display-the-path-of-a-decision-tree-for-test-samples – Alaa M. Apr 27 '19 at 13:02

0 Answers0