Toggle Menu
Prev Up Next

scikit-learn 1.3.2
Other versions

Please cite us if you use the software.

# `sklearn.tree`.export_graphviz

sklearn.tree.export_graphviz(decision_tree, out_file=None, *, max_depth=None, feature_names=None, class_names=None, label='all', filled=False, leaves_parallel=False, impurity=True, node_ids=False, proportion=False, rotate=False, rounded=False, special_characters=False, precision=3, fontname='helvetica')

  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 tree.dot -o tree.ps      (PostScript format)
      $ dot -Tpng tree.dot -o tree.png    (PNG format)

  The sample counts that are shown are weighted with any sample_weights that might be present.

  Read more in the User Guide.

  Parameters:
    decision_tree object

      The decision tree estimator to be exported to GraphViz.

    out_file object or str, default=None

      Handle or name of the output file. If `None`, the result is returned as a string.

      Changed in version 0.20: Default of out_file changed from “tree.dot” to None.

    max_depth int, default=None

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

    feature_names array-like of shape (n_features,), default=None

      An array containing the feature names. If None, generic names will be used (“x[0]”, “x[1]”, …).

    class_names array-like of shape (n_classes,) or bool, default=None

      Names of each of the target classes in ascending numerical order. Only relevant for classification and not supported for multi-output. If `True`, shows a symbolic representation of the class name.

    label{‘all’, ‘root’, ‘none’}, default=’all’

      Whether to show informative labels for impurity, etc. Options include ‘all’ to show at every node, ‘root’ to show only at the top root node, or ‘none’ to not show at any node.

    filled bool, default=False

      When set to `True`, paint nodes to indicate majority class for classification, extremity of values for regression, or purity of node for multi-output.

    leaves_parallel bool, default=False

      When set to `True`, draw all leaf nodes at the bottom of the tree.

    impurity bool, default=True

      When set to `True`, show the impurity at each node.

    node_ids bool, default=False

      When set to `True`, show the ID number on each node.

    proportion bool, default=False

      When set to `True`, change the display of ‘values’ and/or ‘samples’ to be proportions and percentages respectively.

    rotate bool, default=False

      When set to `True`, orient tree left to right rather than top-down.

    rounded bool, default=False

      When set to `True`, draw node boxes with rounded corners.

    special_characters bool, default=False

      When set to `False`, ignore special characters for PostScript compatibility.

    precision int, default=3

      Number of digits of precision for floating point in the values of impurity, threshold and value attributes of each node.

    fontname str, default=’helvetica’

      Name of font used to render text.

  Returns:
    dot_data str

      String representation of the input tree in GraphViz dot format. Only returned if `out_file` is None.

      New in version 0.18.

  Examples

      >>> from sklearn.datasets import load_iris
      >>> from sklearn import tree

      >>> clf = tree.DecisionTreeClassifier()
      >>> iris = load_iris()

      >>> clf = clf.fit(iris.data, iris.target)
      >>> tree.export_graphviz(clf)
      'digraph Tree {...