Fork me on GitHub


sklearn.tree.export_graphviz(decision_tree, out_file='', feature_names=None, max_depth=None, close=True)

Export a decision tree in DOT format.

This function generates a GraphViz representation of the decision tree, which is then written into out_file. Once exported, graphical renderings can be generated using, for example:

$ dot -Tps -o      (PostScript format)
$ dot -Tpng -o tree.png    (PNG format)
Parameters :

decision_tree : decision tree classifier

The decision tree to be exported to GraphViz.

out_file : file object or string, optional (default=””)

Handle or name of the output file.

feature_names : list of strings, optional (default=None)

Names of each of the features.

max_depth : int, optional (default=None)

The maximum depth of the representation. If None, the tree is fully generated.

Returns :

out_file : file object

The file object to which the tree was exported. The user is expected to close() this object when done with it.


>>> import os
>>> from sklearn.datasets import load_iris
>>> from sklearn import tree
>>> clf = tree.DecisionTreeClassifier()
>>> iris = load_iris()
>>> clf =,
>>> export_file = tree.export_graphviz(clf,
...     out_file='')