package sklearn

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
val get_py : string -> Py.Object.t

Get an attribute of this module as a Py.Object.t. This is useful to pass a Python function to another function.

module BaseDecisionTree : sig ... end
module DecisionTreeClassifier : sig ... end
module DecisionTreeRegressor : sig ... end
module ExtraTreeClassifier : sig ... end
module ExtraTreeRegressor : sig ... end
val export_graphviz : ?out_file:[ `S of string | `File_object of Py.Object.t ] -> ?max_depth:int -> ?feature_names:string list -> ?class_names:[ `StringList of string list | `Bool of bool ] -> ?label:[ `All | `Root | `None ] -> ?filled:bool -> ?leaves_parallel:bool -> ?impurity:bool -> ?node_ids:bool -> ?proportion:bool -> ?rotate:bool -> ?rounded:bool -> ?special_characters:bool -> ?precision:int -> decision_tree:[> `DecisionTreeClassifier ] Np.Obj.t -> unit -> string option

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 :ref:`User Guide <tree>`.

Parameters ---------- decision_tree : decision tree classifier The decision tree to be exported to GraphViz.

out_file : file object or string, optional (default=None) Handle or name of the output file. If ``None``, the result is returned as a string.

.. versionchanged:: 0.20 Default of out_file changed from 'tree.dot' to None.

max_depth : int, optional (default=None) The maximum depth of the representation. If None, the tree is fully generated.

feature_names : list of strings, optional (default=None) Names of each of the features.

class_names : list of strings, bool or None, optional (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', optional (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, optional (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, optional (default=False) When set to ``True``, draw all leaf nodes at the bottom of the tree.

impurity : bool, optional (default=True) When set to ``True``, show the impurity at each node.

node_ids : bool, optional (default=False) When set to ``True``, show the ID number on each node.

proportion : bool, optional (default=False) When set to ``True``, change the display of 'values' and/or 'samples' to be proportions and percentages respectively.

rotate : bool, optional (default=False) When set to ``True``, orient tree left to right rather than top-down.

rounded : bool, optional (default=False) When set to ``True``, draw node boxes with rounded corners and use Helvetica fonts instead of Times-Roman.

special_characters : bool, optional (default=False) When set to ``False``, ignore special characters for PostScript compatibility.

precision : int, optional (default=3) Number of digits of precision for floating point in the values of impurity, threshold and value attributes of each node.

Returns ------- dot_data : string String representation of the input tree in GraphViz dot format. Only returned if ``out_file`` is None.

.. versionadded:: 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 {...

val export_text : ?feature_names:string list -> ?max_depth:int -> ?spacing:int -> ?decimals:int -> ?show_weights:bool -> decision_tree:[> `BaseDecisionTree ] Np.Obj.t -> unit -> string

Build a text report showing the rules of a decision tree.

Note that backwards compatibility may not be supported.

Parameters ---------- decision_tree : object The decision tree estimator to be exported. It can be an instance of DecisionTreeClassifier or DecisionTreeRegressor.

feature_names : list, optional (default=None) A list of length n_features containing the feature names. If None generic names will be used ('feature_0', 'feature_1', ...).

max_depth : int, optional (default=10) Only the first max_depth levels of the tree are exported. Truncated branches will be marked with '...'.

spacing : int, optional (default=3) Number of spaces between edges. The higher it is, the wider the result.

decimals : int, optional (default=2) Number of decimal digits to display.

show_weights : bool, optional (default=False) If true the classification weights will be exported on each leaf. The classification weights are the number of samples each class.

Returns ------- report : string Text summary of all the rules in the decision tree.

Examples --------

>>> from sklearn.datasets import load_iris >>> from sklearn.tree import DecisionTreeClassifier >>> from sklearn.tree import export_text >>> iris = load_iris() >>> X = iris'data' >>> y = iris'target' >>> decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2) >>> decision_tree = decision_tree.fit(X, y) >>> r = export_text(decision_tree, feature_names=iris'feature_names') >>> print(r) |--- petal width (cm) <= 0.80 | |--- class: 0 |--- petal width (cm) > 0.80 | |--- petal width (cm) <= 1.75 | | |--- class: 1 | |--- petal width (cm) > 1.75 | | |--- class: 2

val plot_tree : ?max_depth:int -> ?feature_names:string list -> ?class_names:[ `StringList of string list | `Bool of bool ] -> ?label:[ `All | `Root | `None ] -> ?filled:bool -> ?impurity:bool -> ?node_ids:bool -> ?proportion:bool -> ?rotate:bool -> ?rounded:bool -> ?precision:int -> ?ax:Py.Object.t -> ?fontsize:int -> decision_tree:[> `BaseDecisionTree ] Np.Obj.t -> unit -> Py.Object.t

Plot a decision tree.

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

The visualization is fit automatically to the size of the axis. Use the ``figsize`` or ``dpi`` arguments of ``plt.figure`` to control the size of the rendering.

Read more in the :ref:`User Guide <tree>`.

.. versionadded:: 0.21

Parameters ---------- decision_tree : decision tree regressor or classifier The decision tree to be plotted.

max_depth : int, optional (default=None) The maximum depth of the representation. If None, the tree is fully generated.

feature_names : list of strings, optional (default=None) Names of each of the features.

class_names : list of strings, bool or None, optional (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', optional (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, optional (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.

impurity : bool, optional (default=True) When set to ``True``, show the impurity at each node.

node_ids : bool, optional (default=False) When set to ``True``, show the ID number on each node.

proportion : bool, optional (default=False) When set to ``True``, change the display of 'values' and/or 'samples' to be proportions and percentages respectively.

rotate : bool, optional (default=False) This parameter has no effect on the matplotlib tree visualisation and it is kept here for backward compatibility.

.. deprecated:: 0.23 ``rotate`` is deprecated in 0.23 and will be removed in 0.25.

rounded : bool, optional (default=False) When set to ``True``, draw node boxes with rounded corners and use Helvetica fonts instead of Times-Roman.

precision : int, optional (default=3) Number of digits of precision for floating point in the values of impurity, threshold and value attributes of each node.

ax : matplotlib axis, optional (default=None) Axes to plot to. If None, use current axis. Any previous content is cleared.

fontsize : int, optional (default=None) Size of text font. If None, determined automatically to fit figure.

Returns ------- annotations : list of artists List containing the artists for the annotation boxes making up the tree.

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

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

>>> clf = clf.fit(iris.data, iris.target) >>> tree.plot_tree(clf) # doctest: +SKIP Text(251.5,345.217,'X[3] <= 0.8...