3

This classifies the data as a decision tree. The decision tree is created but I am not able to view the decision tree.

import numpy as np
from sklearn import linear_model, datasets, tree
import matplotlib.pyplot as plt
iris = datasets.load_iris()
f = open('decision_tree_data.txt')
x_train = []
y_train = []
for line in f:
    line = np.asarray(line.split(),dtype = np.float32)
    x_train.append(line[:-1])
    y_train.append(line[:-1])
x_train = np.asmatrix(x_train)
y_train = np.asmatrix(y_train)
model = tree.DecisionTreeClassifier()
model.fit(x_train,y_train)
from sklearn.externals.six import StringIO
import pydot
from IPython.display import Image
dot_data = StringIO()
tree.export_graphviz(model, out_file=dot_data,  
                     feature_names=iris.feature_names,  
                     class_names=iris.target_names,  
                     filled=True, rounded=True,  
                     special_characters=True)  
graph = pydot.graph_from_dot_data(dot_data.getvalue()) 
Image(graph.create_png())
Anurag A S
  • 725
  • 10
  • 23
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Most notably, we need you to include the *entire* error message as well as enough data to get us to the problem point. – Prune Aug 08 '17 at 21:54
  • Overall, it appears that `graph` is a list, not the expected pydot object. Are you missing a `todot` conversion? Sorry; I'm no expert on this package. – Prune Aug 08 '17 at 22:00
  • maybe you should try install pydotplus, and replace pydot in your code with pydotplus. Pydot seems to be inactive for some years. – Metaphox Aug 10 '17 at 07:51
  • What do you mean "not able to view", is it blank, do you get an error message, do you get jumbled text, etc.? – Joshua Drake Aug 10 '17 at 15:28
  • @Prune When i did print the graph i got : [] – Biranchi Aug 11 '17 at 22:30
  • That's the first step to debugging. Now I'm a bit puzzled. Can you please include those lines (the print and the result) in your original post? Also include the entire error message (full text and traceback). – Prune Aug 11 '17 at 22:35

1 Answers1

7

The function pydot.graph_from_dot_data returns a list in pydot >= 1.2.0 (in contrast to earlier versions of pydot).

The reason was to homogenize the output, which in the past was a list if two graphs were returned, but a graph if a single graph was returned. Such branching is a common source of errors in user code (simple is better than complex [PEP 20]).

The change applies to all functions that call the function dot_parser.parse_dot_data, which now returns a list in all cases.

To address the error, you need to unpack the single graph that you expect:

graph, = pydot.graph_from_dot_data(dot_data.getvalue())

This statement also asserts that a single graph is returned. So if this assumption doesn't hold, and more graphs are returned, this unpacking will catch it. In contrast, graph = (...)[0] won't.

Relevant pydot issues:

0 _
  • 10,524
  • 11
  • 77
  • 109