Fork me on GitHub


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

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)

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.


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