sklearn.tree.export_text(decision_tree, *, feature_names=None, max_depth=10, spacing=3, decimals=2, show_weights=False) [source]

Build a text report showing the rules of a decision tree. Note that backwards compatibility may not be supported.  Parameters

decision_treeobject

The decision tree estimator to be exported. It can be an instance of DecisionTreeClassifier or DecisionTreeRegressor.
feature_nameslist of str, default=None

A list of length n_features containing the feature names. If None generic names will be used (“feature_0”, “feature_1”, …).
max_depthint, default=10

Only the first max_depth levels of the tree are exported. Truncated branches will be marked with “…”.
spacingint, default=3

Number of spaces between edges. The higher it is, the wider the result.
decimalsint, default=2

Number of decimal digits to display.
show_weightsbool, default=False

If true the classification weights will be exported on each leaf. The classification weights are the number of samples each class.    Returns

reportstring

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


class sklearn.tree.ExtraTreeClassifier(*, criterion='gini', splitter='random', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, class_weight=None, ccp_alpha=0.0) [source]

An extremely randomized tree classifier. Extra-trees differ from classic decision trees in the way they are built. When looking for the best split to separate the samples of a node into two groups, random splits are drawn for each of the max_features randomly selected features and the best split among those is chosen. When max_features is set 1, this amounts to building a totally random decision tree. Warning: Extra-trees should only be used within ensemble methods. Read more in the User Guide.  Parameters

criterion{“gini”, “entropy”}, default=”gini”

The function to measure the quality of a split. Supported criteria are “gini” for the Gini impurity and “entropy” for the information gain.
splitter{“random”, “best”}, default=”random”

The strategy used to choose the split at each node. Supported strategies are “best” to choose the best split and “random” to choose the best random split.
max_depthint, default=None

The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples.
min_samples_splitint or float, default=2

The minimum number of samples required to split an internal node:  If int, then consider min_samples_split as the minimum number. If float, then min_samples_split is a fraction and ceil(min_samples_split * n_samples) are the minimum number of samples for each split.   Changed in version 0.18: Added float values for fractions.
min_samples_leafint or float, default=1

The minimum number of samples required to be at a leaf node. A split point at any depth will only be considered if it leaves at least min_samples_leaf training samples in each of the left and right branches. This may have the effect of smoothing the model, especially in regression.  If int, then consider min_samples_leaf as the minimum number. If float, then min_samples_leaf is a fraction and ceil(min_samples_leaf * n_samples) are the minimum number of samples for each node.   Changed in version 0.18: Added float values for fractions.
min_weight_fraction_leaffloat, default=0.0

The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided.
max_featuresint, float, {“auto”, “sqrt”, “log2”} or None, default=”auto”

The number of features to consider when looking for the best split:  If int, then consider max_features features at each split. If float, then max_features is a fraction and int(max_features * n_features) features are considered at each split. If “auto”, then max_features=sqrt(n_features). If “sqrt”, then max_features=sqrt(n_features). If “log2”, then max_features=log2(n_features). If None, then max_features=n_features.  Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than max_features features.
random_stateint, RandomState instance or None, default=None

Used to pick randomly the max_features used at each split. See Glossary for details.
max_leaf_nodesint, default=None

Grow a tree with max_leaf_nodes in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes.
min_impurity_decreasefloat, default=0.0

A node will be split if this split induces a decrease of the impurity greater than or equal to this value. The weighted impurity decrease equation is the following: N_t / N * (impurity - N_t_R / N_t * right_impurity
                    - N_t_L / N_t * left_impurity)
 where N is the total number of samples, N_t is the number of samples at the current node, N_t_L is the number of samples in the left child, and N_t_R is the number of samples in the right child. N, N_t, N_t_R and N_t_L all refer to the weighted sum, if sample_weight is passed.  New in version 0.19.
min_impurity_splitfloat, default=None

Threshold for early stopping in tree growth. A node will split if its impurity is above the threshold, otherwise it is a leaf.  Deprecated since version 0.19: min_impurity_split has been deprecated in favor of min_impurity_decrease in 0.19. The default value of min_impurity_split has changed from 1e-7 to 0 in 0.23 and it will be removed in 1.0 (renaming of 0.25). Use min_impurity_decrease instead.
class_weightdict, list of dict or “balanced”, default=None

Weights associated with classes in the form {class_label: weight}. If None, all classes are supposed to have weight one. For multi-output problems, a list of dicts can be provided in the same order as the columns of y. Note that for multioutput (including multilabel) weights should be defined for each class of every column in its own dict. For example, for four-class multilabel classification weights should be [{0: 1, 1: 1}, {0: 1, 1: 5}, {0: 1, 1: 1}, {0: 1, 1: 1}] instead of [{1:1}, {2:5}, {3:1}, {4:1}]. The “balanced” mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as n_samples / (n_classes * np.bincount(y)) For multi-output, the weights of each column of y will be multiplied. Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified.
ccp_alphanon-negative float, default=0.0

Complexity parameter used for Minimal Cost-Complexity Pruning. The subtree with the largest cost complexity that is smaller than ccp_alpha will be chosen. By default, no pruning is performed. See Minimal Cost-Complexity Pruning for details.  New in version 0.22.     Attributes

classes_ndarray of shape (n_classes,) or list of ndarray

The classes labels (single output problem), or a list of arrays of class labels (multi-output problem).
max_features_int

The inferred value of max_features.
n_classes_int or list of int

The number of classes (for single output problems), or a list containing the number of classes for each output (for multi-output problems).
feature_importances_ndarray of shape (n_features,)

Return the feature importances.
n_features_int

The number of features when fit is performed.
n_outputs_int

The number of outputs when fit is performed.
tree_Tree instance

The underlying Tree object. Please refer to help(sklearn.tree._tree.Tree) for attributes of Tree object and Understanding the decision tree structure for basic usage of these attributes.      See also
ExtraTreeRegressor

An extremely randomized tree regressor.
sklearn.ensemble.ExtraTreesClassifier

An extra-trees classifier.
sklearn.ensemble.ExtraTreesRegressor

An extra-trees regressor.    Notes The default values for the parameters controlling the size of the trees (e.g. max_depth, min_samples_leaf, etc.) lead to fully grown and unpruned trees which can potentially be very large on some data sets. To reduce memory consumption, the complexity and size of the trees should be controlled by setting those parameter values. References
1
P. Geurts, D. Ernst., and L. Wehenkel, “Extremely randomized trees”, Machine Learning, 63(1), 3-42, 2006.   Examples >>> from sklearn.datasets import load_iris
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.ensemble import BaggingClassifier
>>> from sklearn.tree import ExtraTreeClassifier
>>> X, y = load_iris(return_X_y=True)
>>> X_train, X_test, y_train, y_test = train_test_split(
...    X, y, random_state=0)
>>> extra_tree = ExtraTreeClassifier(random_state=0)
>>> cls = BaggingClassifier(extra_tree, random_state=0).fit(
...    X_train, y_train)
>>> cls.score(X_test, y_test)
0.8947...
 Methods
apply(X[, check_input]) Return the index of the leaf that each sample is predicted as.
cost_complexity_pruning_path(X, y[, …]) Compute the pruning path during Minimal Cost-Complexity Pruning.
decision_path(X[, check_input]) Return the decision path in the tree.
fit(X, y[, sample_weight, check_input, …]) Build a decision tree classifier from the training set (X, y).
get_depth() Return the depth of the decision tree.
get_n_leaves() Return the number of leaves of the decision tree.
get_params([deep]) Get parameters for this estimator.
predict(X[, check_input]) Predict class or regression value for X.
predict_log_proba(X) Predict class log-probabilities of the input samples X.
predict_proba(X[, check_input]) Predict class probabilities of the input samples X.
score(X, y[, sample_weight]) Return the mean accuracy on the given test data and labels.
set_params(**params) Set the parameters of this estimator.
apply(X, check_input=True) [source]

Return the index of the leaf that each sample is predicted as.  New in version 0.17.   Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

X_leavesarray-like of shape (n_samples,)

For each datapoint x in X, return the index of the leaf x ends up in. Leaves are numbered within [0; self.tree_.node_count), possibly with gaps in the numbering.

cost_complexity_pruning_path(X, y, sample_weight=None) [source]

Compute the pruning path during Minimal Cost-Complexity Pruning. See Minimal Cost-Complexity Pruning for details on the pruning process.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The target values (class labels) as integers or strings.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. Splits are also ignored if they would result in any single class carrying a negative weight in either child node.    Returns

ccp_pathBunch

Dictionary-like object, with the following attributes.
ccp_alphasndarray

Effective alphas of subtree during pruning.
impuritiesndarray

Sum of the impurities of the subtree leaves for the corresponding alpha value in ccp_alphas.

decision_path(X, check_input=True) [source]

Return the decision path in the tree.  New in version 0.18.   Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

indicatorsparse matrix of shape (n_samples, n_nodes)

Return a node indicator CSR matrix where non zero elements indicates that the samples goes through the nodes.

property feature_importances_
Return the feature importances. The importance of a feature is computed as the (normalized) total reduction of the criterion brought by that feature. It is also known as the Gini importance. Warning: impurity-based feature importances can be misleading for high cardinality features (many unique values). See sklearn.inspection.permutation_importance as an alternative.  Returns

feature_importances_ndarray of shape (n_features,)

Normalized total reduction of criteria by feature (Gini importance).

fit(X, y, sample_weight=None, check_input=True, X_idx_sorted='deprecated') [source]

Build a decision tree classifier from the training set (X, y).  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The target values (class labels) as integers or strings.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. Splits are also ignored if they would result in any single class carrying a negative weight in either child node.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
X_idx_sorteddeprecated, default=”deprecated”

This parameter is deprecated and has no effect. It will be removed in 1.1 (renaming of 0.26).  Deprecated since version 0.24.     Returns

selfDecisionTreeClassifier

Fitted estimator.

get_depth() [source]

Return the depth of the decision tree. The depth of a tree is the maximum distance between the root and any leaf.  Returns

self.tree_.max_depthint

The maximum depth of the tree.

get_n_leaves() [source]

Return the number of leaves of the decision tree.  Returns

self.tree_.n_leavesint

Number of leaves.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

predict(X, check_input=True) [source]

Predict class or regression value for X. For a classification model, the predicted class for each sample in X is returned. For a regression model, the predicted value based on X is returned.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The predicted classes, or the predict values.

predict_log_proba(X) [source]

Predict class log-probabilities of the input samples X.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.    Returns

probandarray of shape (n_samples, n_classes) or list of n_outputs such arrays if n_outputs > 1

The class log-probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.

predict_proba(X, check_input=True) [source]

Predict class probabilities of the input samples X. The predicted class probability is the fraction of samples of the same class in a leaf.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

probandarray of shape (n_samples, n_classes) or list of n_outputs such arrays if n_outputs > 1

The class probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.

score(X, y, sample_weight=None) [source]

Return the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

Mean accuracy of self.predict(X) wrt. y.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


sklearn.tree.ExtraTreeClassifier
class sklearn.tree.ExtraTreeClassifier(*, criterion='gini', splitter='random', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, class_weight=None, ccp_alpha=0.0) [source]

An extremely randomized tree classifier. Extra-trees differ from classic decision trees in the way they are built. When looking for the best split to separate the samples of a node into two groups, random splits are drawn for each of the max_features randomly selected features and the best split among those is chosen. When max_features is set 1, this amounts to building a totally random decision tree. Warning: Extra-trees should only be used within ensemble methods. Read more in the User Guide.  Parameters

criterion{“gini”, “entropy”}, default=”gini”

The function to measure the quality of a split. Supported criteria are “gini” for the Gini impurity and “entropy” for the information gain.
splitter{“random”, “best”}, default=”random”

The strategy used to choose the split at each node. Supported strategies are “best” to choose the best split and “random” to choose the best random split.
max_depthint, default=None

The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples.
min_samples_splitint or float, default=2

The minimum number of samples required to split an internal node:  If int, then consider min_samples_split as the minimum number. If float, then min_samples_split is a fraction and ceil(min_samples_split * n_samples) are the minimum number of samples for each split.   Changed in version 0.18: Added float values for fractions.
min_samples_leafint or float, default=1

The minimum number of samples required to be at a leaf node. A split point at any depth will only be considered if it leaves at least min_samples_leaf training samples in each of the left and right branches. This may have the effect of smoothing the model, especially in regression.  If int, then consider min_samples_leaf as the minimum number. If float, then min_samples_leaf is a fraction and ceil(min_samples_leaf * n_samples) are the minimum number of samples for each node.   Changed in version 0.18: Added float values for fractions.
min_weight_fraction_leaffloat, default=0.0

The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided.
max_featuresint, float, {“auto”, “sqrt”, “log2”} or None, default=”auto”

The number of features to consider when looking for the best split:  If int, then consider max_features features at each split. If float, then max_features is a fraction and int(max_features * n_features) features are considered at each split. If “auto”, then max_features=sqrt(n_features). If “sqrt”, then max_features=sqrt(n_features). If “log2”, then max_features=log2(n_features). If None, then max_features=n_features.  Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than max_features features.
random_stateint, RandomState instance or None, default=None

Used to pick randomly the max_features used at each split. See Glossary for details.
max_leaf_nodesint, default=None

Grow a tree with max_leaf_nodes in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes.
min_impurity_decreasefloat, default=0.0

A node will be split if this split induces a decrease of the impurity greater than or equal to this value. The weighted impurity decrease equation is the following: N_t / N * (impurity - N_t_R / N_t * right_impurity
                    - N_t_L / N_t * left_impurity)
 where N is the total number of samples, N_t is the number of samples at the current node, N_t_L is the number of samples in the left child, and N_t_R is the number of samples in the right child. N, N_t, N_t_R and N_t_L all refer to the weighted sum, if sample_weight is passed.  New in version 0.19.
min_impurity_splitfloat, default=None

Threshold for early stopping in tree growth. A node will split if its impurity is above the threshold, otherwise it is a leaf.  Deprecated since version 0.19: min_impurity_split has been deprecated in favor of min_impurity_decrease in 0.19. The default value of min_impurity_split has changed from 1e-7 to 0 in 0.23 and it will be removed in 1.0 (renaming of 0.25). Use min_impurity_decrease instead.
class_weightdict, list of dict or “balanced”, default=None

Weights associated with classes in the form {class_label: weight}. If None, all classes are supposed to have weight one. For multi-output problems, a list of dicts can be provided in the same order as the columns of y. Note that for multioutput (including multilabel) weights should be defined for each class of every column in its own dict. For example, for four-class multilabel classification weights should be [{0: 1, 1: 1}, {0: 1, 1: 5}, {0: 1, 1: 1}, {0: 1, 1: 1}] instead of [{1:1}, {2:5}, {3:1}, {4:1}]. The “balanced” mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as n_samples / (n_classes * np.bincount(y)) For multi-output, the weights of each column of y will be multiplied. Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified.
ccp_alphanon-negative float, default=0.0

Complexity parameter used for Minimal Cost-Complexity Pruning. The subtree with the largest cost complexity that is smaller than ccp_alpha will be chosen. By default, no pruning is performed. See Minimal Cost-Complexity Pruning for details.  New in version 0.22.     Attributes

classes_ndarray of shape (n_classes,) or list of ndarray

The classes labels (single output problem), or a list of arrays of class labels (multi-output problem).
max_features_int

The inferred value of max_features.
n_classes_int or list of int

The number of classes (for single output problems), or a list containing the number of classes for each output (for multi-output problems).
feature_importances_ndarray of shape (n_features,)

Return the feature importances.
n_features_int

The number of features when fit is performed.
n_outputs_int

The number of outputs when fit is performed.
tree_Tree instance

The underlying Tree object. Please refer to help(sklearn.tree._tree.Tree) for attributes of Tree object and Understanding the decision tree structure for basic usage of these attributes.      See also
ExtraTreeRegressor

An extremely randomized tree regressor.
sklearn.ensemble.ExtraTreesClassifier

An extra-trees classifier.
sklearn.ensemble.ExtraTreesRegressor

An extra-trees regressor.    Notes The default values for the parameters controlling the size of the trees (e.g. max_depth, min_samples_leaf, etc.) lead to fully grown and unpruned trees which can potentially be very large on some data sets. To reduce memory consumption, the complexity and size of the trees should be controlled by setting those parameter values. References
1
P. Geurts, D. Ernst., and L. Wehenkel, “Extremely randomized trees”, Machine Learning, 63(1), 3-42, 2006.   Examples >>> from sklearn.datasets import load_iris
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.ensemble import BaggingClassifier
>>> from sklearn.tree import ExtraTreeClassifier
>>> X, y = load_iris(return_X_y=True)
>>> X_train, X_test, y_train, y_test = train_test_split(
...    X, y, random_state=0)
>>> extra_tree = ExtraTreeClassifier(random_state=0)
>>> cls = BaggingClassifier(extra_tree, random_state=0).fit(
...    X_train, y_train)
>>> cls.score(X_test, y_test)
0.8947...
 Methods
apply(X[, check_input]) Return the index of the leaf that each sample is predicted as.
cost_complexity_pruning_path(X, y[, …]) Compute the pruning path during Minimal Cost-Complexity Pruning.
decision_path(X[, check_input]) Return the decision path in the tree.
fit(X, y[, sample_weight, check_input, …]) Build a decision tree classifier from the training set (X, y).
get_depth() Return the depth of the decision tree.
get_n_leaves() Return the number of leaves of the decision tree.
get_params([deep]) Get parameters for this estimator.
predict(X[, check_input]) Predict class or regression value for X.
predict_log_proba(X) Predict class log-probabilities of the input samples X.
predict_proba(X[, check_input]) Predict class probabilities of the input samples X.
score(X, y[, sample_weight]) Return the mean accuracy on the given test data and labels.
set_params(**params) Set the parameters of this estimator.
apply(X, check_input=True) [source]

Return the index of the leaf that each sample is predicted as.  New in version 0.17.   Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

X_leavesarray-like of shape (n_samples,)

For each datapoint x in X, return the index of the leaf x ends up in. Leaves are numbered within [0; self.tree_.node_count), possibly with gaps in the numbering.

cost_complexity_pruning_path(X, y, sample_weight=None) [source]

Compute the pruning path during Minimal Cost-Complexity Pruning. See Minimal Cost-Complexity Pruning for details on the pruning process.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The target values (class labels) as integers or strings.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. Splits are also ignored if they would result in any single class carrying a negative weight in either child node.    Returns

ccp_pathBunch

Dictionary-like object, with the following attributes.
ccp_alphasndarray

Effective alphas of subtree during pruning.
impuritiesndarray

Sum of the impurities of the subtree leaves for the corresponding alpha value in ccp_alphas.

decision_path(X, check_input=True) [source]

Return the decision path in the tree.  New in version 0.18.   Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

indicatorsparse matrix of shape (n_samples, n_nodes)

Return a node indicator CSR matrix where non zero elements indicates that the samples goes through the nodes.

property feature_importances_
Return the feature importances. The importance of a feature is computed as the (normalized) total reduction of the criterion brought by that feature. It is also known as the Gini importance. Warning: impurity-based feature importances can be misleading for high cardinality features (many unique values). See sklearn.inspection.permutation_importance as an alternative.  Returns

feature_importances_ndarray of shape (n_features,)

Normalized total reduction of criteria by feature (Gini importance).

fit(X, y, sample_weight=None, check_input=True, X_idx_sorted='deprecated') [source]

Build a decision tree classifier from the training set (X, y).  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The target values (class labels) as integers or strings.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. Splits are also ignored if they would result in any single class carrying a negative weight in either child node.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
X_idx_sorteddeprecated, default=”deprecated”

This parameter is deprecated and has no effect. It will be removed in 1.1 (renaming of 0.26).  Deprecated since version 0.24.     Returns

selfDecisionTreeClassifier

Fitted estimator.

get_depth() [source]

Return the depth of the decision tree. The depth of a tree is the maximum distance between the root and any leaf.  Returns

self.tree_.max_depthint

The maximum depth of the tree.

get_n_leaves() [source]

Return the number of leaves of the decision tree.  Returns

self.tree_.n_leavesint

Number of leaves.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

predict(X, check_input=True) [source]

Predict class or regression value for X. For a classification model, the predicted class for each sample in X is returned. For a regression model, the predicted value based on X is returned.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The predicted classes, or the predict values.

predict_log_proba(X) [source]

Predict class log-probabilities of the input samples X.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.    Returns

probandarray of shape (n_samples, n_classes) or list of n_outputs such arrays if n_outputs > 1

The class log-probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.

predict_proba(X, check_input=True) [source]

Predict class probabilities of the input samples X. The predicted class probability is the fraction of samples of the same class in a leaf.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

probandarray of shape (n_samples, n_classes) or list of n_outputs such arrays if n_outputs > 1

The class probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.

score(X, y, sample_weight=None) [source]

Return the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

Mean accuracy of self.predict(X) wrt. y.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


apply(X, check_input=True) [source]

Return the index of the leaf that each sample is predicted as.  New in version 0.17.   Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

X_leavesarray-like of shape (n_samples,)

For each datapoint x in X, return the index of the leaf x ends up in. Leaves are numbered within [0; self.tree_.node_count), possibly with gaps in the numbering.


cost_complexity_pruning_path(X, y, sample_weight=None) [source]

Compute the pruning path during Minimal Cost-Complexity Pruning. See Minimal Cost-Complexity Pruning for details on the pruning process.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The target values (class labels) as integers or strings.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. Splits are also ignored if they would result in any single class carrying a negative weight in either child node.    Returns

ccp_pathBunch

Dictionary-like object, with the following attributes.
ccp_alphasndarray

Effective alphas of subtree during pruning.
impuritiesndarray

Sum of the impurities of the subtree leaves for the corresponding alpha value in ccp_alphas.


decision_path(X, check_input=True) [source]

Return the decision path in the tree.  New in version 0.18.   Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

indicatorsparse matrix of shape (n_samples, n_nodes)

Return a node indicator CSR matrix where non zero elements indicates that the samples goes through the nodes.


property feature_importances_
Return the feature importances. The importance of a feature is computed as the (normalized) total reduction of the criterion brought by that feature. It is also known as the Gini importance. Warning: impurity-based feature importances can be misleading for high cardinality features (many unique values). See sklearn.inspection.permutation_importance as an alternative.  Returns

feature_importances_ndarray of shape (n_features,)

Normalized total reduction of criteria by feature (Gini importance).


fit(X, y, sample_weight=None, check_input=True, X_idx_sorted='deprecated') [source]

Build a decision tree classifier from the training set (X, y).  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The target values (class labels) as integers or strings.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. Splits are also ignored if they would result in any single class carrying a negative weight in either child node.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
X_idx_sorteddeprecated, default=”deprecated”

This parameter is deprecated and has no effect. It will be removed in 1.1 (renaming of 0.26).  Deprecated since version 0.24.     Returns

selfDecisionTreeClassifier

Fitted estimator.


get_depth() [source]

Return the depth of the decision tree. The depth of a tree is the maximum distance between the root and any leaf.  Returns

self.tree_.max_depthint

The maximum depth of the tree.


get_n_leaves() [source]

Return the number of leaves of the decision tree.  Returns

self.tree_.n_leavesint

Number of leaves.


get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.


predict(X, check_input=True) [source]

Predict class or regression value for X. For a classification model, the predicted class for each sample in X is returned. For a regression model, the predicted value based on X is returned.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The predicted classes, or the predict values.


predict_log_proba(X) [source]

Predict class log-probabilities of the input samples X.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.    Returns

probandarray of shape (n_samples, n_classes) or list of n_outputs such arrays if n_outputs > 1

The class log-probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.


predict_proba(X, check_input=True) [source]

Predict class probabilities of the input samples X. The predicted class probability is the fraction of samples of the same class in a leaf.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

probandarray of shape (n_samples, n_classes) or list of n_outputs such arrays if n_outputs > 1

The class probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.


score(X, y, sample_weight=None) [source]

Return the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

Mean accuracy of self.predict(X) wrt. y.


set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


class sklearn.tree.ExtraTreeRegressor(*, criterion='mse', splitter='random', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', random_state=None, min_impurity_decrease=0.0, min_impurity_split=None, max_leaf_nodes=None, ccp_alpha=0.0) [source]

An extremely randomized tree regressor. Extra-trees differ from classic decision trees in the way they are built. When looking for the best split to separate the samples of a node into two groups, random splits are drawn for each of the max_features randomly selected features and the best split among those is chosen. When max_features is set 1, this amounts to building a totally random decision tree. Warning: Extra-trees should only be used within ensemble methods. Read more in the User Guide.  Parameters

criterion{“mse”, “friedman_mse”, “mae”}, default=”mse”

The function to measure the quality of a split. Supported criteria are “mse” for the mean squared error, which is equal to variance reduction as feature selection criterion and “mae” for the mean absolute error.  New in version 0.18: Mean Absolute Error (MAE) criterion.   New in version 0.24: Poisson deviance criterion.
splitter{“random”, “best”}, default=”random”

The strategy used to choose the split at each node. Supported strategies are “best” to choose the best split and “random” to choose the best random split.
max_depthint, default=None

The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples.
min_samples_splitint or float, default=2

The minimum number of samples required to split an internal node:  If int, then consider min_samples_split as the minimum number. If float, then min_samples_split is a fraction and ceil(min_samples_split * n_samples) are the minimum number of samples for each split.   Changed in version 0.18: Added float values for fractions.
min_samples_leafint or float, default=1

The minimum number of samples required to be at a leaf node. A split point at any depth will only be considered if it leaves at least min_samples_leaf training samples in each of the left and right branches. This may have the effect of smoothing the model, especially in regression.  If int, then consider min_samples_leaf as the minimum number. If float, then min_samples_leaf is a fraction and ceil(min_samples_leaf * n_samples) are the minimum number of samples for each node.   Changed in version 0.18: Added float values for fractions.
min_weight_fraction_leaffloat, default=0.0

The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided.
max_featuresint, float, {“auto”, “sqrt”, “log2”} or None, default=”auto”

The number of features to consider when looking for the best split:  If int, then consider max_features features at each split. If float, then max_features is a fraction and int(max_features * n_features) features are considered at each split. If “auto”, then max_features=n_features. If “sqrt”, then max_features=sqrt(n_features). If “log2”, then max_features=log2(n_features). If None, then max_features=n_features.  Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than max_features features.
random_stateint, RandomState instance or None, default=None

Used to pick randomly the max_features used at each split. See Glossary for details.
min_impurity_decreasefloat, default=0.0

A node will be split if this split induces a decrease of the impurity greater than or equal to this value. The weighted impurity decrease equation is the following: N_t / N * (impurity - N_t_R / N_t * right_impurity
                    - N_t_L / N_t * left_impurity)
 where N is the total number of samples, N_t is the number of samples at the current node, N_t_L is the number of samples in the left child, and N_t_R is the number of samples in the right child. N, N_t, N_t_R and N_t_L all refer to the weighted sum, if sample_weight is passed.  New in version 0.19.
min_impurity_splitfloat, default=None

Threshold for early stopping in tree growth. A node will split if its impurity is above the threshold, otherwise it is a leaf.  Deprecated since version 0.19: min_impurity_split has been deprecated in favor of min_impurity_decrease in 0.19. The default value of min_impurity_split has changed from 1e-7 to 0 in 0.23 and it will be removed in 1.0 (renaming of 0.25). Use min_impurity_decrease instead.
max_leaf_nodesint, default=None

Grow a tree with max_leaf_nodes in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes.
ccp_alphanon-negative float, default=0.0

Complexity parameter used for Minimal Cost-Complexity Pruning. The subtree with the largest cost complexity that is smaller than ccp_alpha will be chosen. By default, no pruning is performed. See Minimal Cost-Complexity Pruning for details.  New in version 0.22.     Attributes

max_features_int

The inferred value of max_features.
n_features_int

The number of features when fit is performed.
feature_importances_ndarray of shape (n_features,)

Return the feature importances.
n_outputs_int

The number of outputs when fit is performed.
tree_Tree instance

The underlying Tree object. Please refer to help(sklearn.tree._tree.Tree) for attributes of Tree object and Understanding the decision tree structure for basic usage of these attributes.      See also
ExtraTreeClassifier

An extremely randomized tree classifier.
sklearn.ensemble.ExtraTreesClassifier

An extra-trees classifier.
sklearn.ensemble.ExtraTreesRegressor

An extra-trees regressor.    Notes The default values for the parameters controlling the size of the trees (e.g. max_depth, min_samples_leaf, etc.) lead to fully grown and unpruned trees which can potentially be very large on some data sets. To reduce memory consumption, the complexity and size of the trees should be controlled by setting those parameter values. References
1
P. Geurts, D. Ernst., and L. Wehenkel, “Extremely randomized trees”, Machine Learning, 63(1), 3-42, 2006.   Examples >>> from sklearn.datasets import load_diabetes
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.ensemble import BaggingRegressor
>>> from sklearn.tree import ExtraTreeRegressor
>>> X, y = load_diabetes(return_X_y=True)
>>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, random_state=0)
>>> extra_tree = ExtraTreeRegressor(random_state=0)
>>> reg = BaggingRegressor(extra_tree, random_state=0).fit(
...     X_train, y_train)
>>> reg.score(X_test, y_test)
0.33...
 Methods
apply(X[, check_input]) Return the index of the leaf that each sample is predicted as.
cost_complexity_pruning_path(X, y[, …]) Compute the pruning path during Minimal Cost-Complexity Pruning.
decision_path(X[, check_input]) Return the decision path in the tree.
fit(X, y[, sample_weight, check_input, …]) Build a decision tree regressor from the training set (X, y).
get_depth() Return the depth of the decision tree.
get_n_leaves() Return the number of leaves of the decision tree.
get_params([deep]) Get parameters for this estimator.
predict(X[, check_input]) Predict class or regression value for X.
score(X, y[, sample_weight]) Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params) Set the parameters of this estimator.
apply(X, check_input=True) [source]

Return the index of the leaf that each sample is predicted as.  New in version 0.17.   Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

X_leavesarray-like of shape (n_samples,)

For each datapoint x in X, return the index of the leaf x ends up in. Leaves are numbered within [0; self.tree_.node_count), possibly with gaps in the numbering.

cost_complexity_pruning_path(X, y, sample_weight=None) [source]

Compute the pruning path during Minimal Cost-Complexity Pruning. See Minimal Cost-Complexity Pruning for details on the pruning process.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The target values (class labels) as integers or strings.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. Splits are also ignored if they would result in any single class carrying a negative weight in either child node.    Returns

ccp_pathBunch

Dictionary-like object, with the following attributes.
ccp_alphasndarray

Effective alphas of subtree during pruning.
impuritiesndarray

Sum of the impurities of the subtree leaves for the corresponding alpha value in ccp_alphas.

decision_path(X, check_input=True) [source]

Return the decision path in the tree.  New in version 0.18.   Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

indicatorsparse matrix of shape (n_samples, n_nodes)

Return a node indicator CSR matrix where non zero elements indicates that the samples goes through the nodes.

property feature_importances_
Return the feature importances. The importance of a feature is computed as the (normalized) total reduction of the criterion brought by that feature. It is also known as the Gini importance. Warning: impurity-based feature importances can be misleading for high cardinality features (many unique values). See sklearn.inspection.permutation_importance as an alternative.  Returns

feature_importances_ndarray of shape (n_features,)

Normalized total reduction of criteria by feature (Gini importance).

fit(X, y, sample_weight=None, check_input=True, X_idx_sorted='deprecated') [source]

Build a decision tree regressor from the training set (X, y).  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The target values (real numbers). Use dtype=np.float64 and order='C' for maximum efficiency.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
X_idx_sorteddeprecated, default=”deprecated”

This parameter is deprecated and has no effect. It will be removed in 1.1 (renaming of 0.26).  Deprecated since version 0.24.     Returns

selfDecisionTreeRegressor

Fitted estimator.

get_depth() [source]

Return the depth of the decision tree. The depth of a tree is the maximum distance between the root and any leaf.  Returns

self.tree_.max_depthint

The maximum depth of the tree.

get_n_leaves() [source]

Return the number of leaves of the decision tree.  Returns

self.tree_.n_leavesint

Number of leaves.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

predict(X, check_input=True) [source]

Predict class or regression value for X. For a classification model, the predicted class for each sample in X is returned. For a regression model, the predicted value based on X is returned.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The predicted classes, or the predict values.

score(X, y, sample_weight=None) [source]

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

\(R^2\) of self.predict(X) wrt. y.     Notes The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


sklearn.tree.ExtraTreeRegressor
class sklearn.tree.ExtraTreeRegressor(*, criterion='mse', splitter='random', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', random_state=None, min_impurity_decrease=0.0, min_impurity_split=None, max_leaf_nodes=None, ccp_alpha=0.0) [source]

An extremely randomized tree regressor. Extra-trees differ from classic decision trees in the way they are built. When looking for the best split to separate the samples of a node into two groups, random splits are drawn for each of the max_features randomly selected features and the best split among those is chosen. When max_features is set 1, this amounts to building a totally random decision tree. Warning: Extra-trees should only be used within ensemble methods. Read more in the User Guide.  Parameters

criterion{“mse”, “friedman_mse”, “mae”}, default=”mse”

The function to measure the quality of a split. Supported criteria are “mse” for the mean squared error, which is equal to variance reduction as feature selection criterion and “mae” for the mean absolute error.  New in version 0.18: Mean Absolute Error (MAE) criterion.   New in version 0.24: Poisson deviance criterion.
splitter{“random”, “best”}, default=”random”

The strategy used to choose the split at each node. Supported strategies are “best” to choose the best split and “random” to choose the best random split.
max_depthint, default=None

The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples.
min_samples_splitint or float, default=2

The minimum number of samples required to split an internal node:  If int, then consider min_samples_split as the minimum number. If float, then min_samples_split is a fraction and ceil(min_samples_split * n_samples) are the minimum number of samples for each split.   Changed in version 0.18: Added float values for fractions.
min_samples_leafint or float, default=1

The minimum number of samples required to be at a leaf node. A split point at any depth will only be considered if it leaves at least min_samples_leaf training samples in each of the left and right branches. This may have the effect of smoothing the model, especially in regression.  If int, then consider min_samples_leaf as the minimum number. If float, then min_samples_leaf is a fraction and ceil(min_samples_leaf * n_samples) are the minimum number of samples for each node.   Changed in version 0.18: Added float values for fractions.
min_weight_fraction_leaffloat, default=0.0

The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided.
max_featuresint, float, {“auto”, “sqrt”, “log2”} or None, default=”auto”

The number of features to consider when looking for the best split:  If int, then consider max_features features at each split. If float, then max_features is a fraction and int(max_features * n_features) features are considered at each split. If “auto”, then max_features=n_features. If “sqrt”, then max_features=sqrt(n_features). If “log2”, then max_features=log2(n_features). If None, then max_features=n_features.  Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than max_features features.
random_stateint, RandomState instance or None, default=None

Used to pick randomly the max_features used at each split. See Glossary for details.
min_impurity_decreasefloat, default=0.0

A node will be split if this split induces a decrease of the impurity greater than or equal to this value. The weighted impurity decrease equation is the following: N_t / N * (impurity - N_t_R / N_t * right_impurity
                    - N_t_L / N_t * left_impurity)
 where N is the total number of samples, N_t is the number of samples at the current node, N_t_L is the number of samples in the left child, and N_t_R is the number of samples in the right child. N, N_t, N_t_R and N_t_L all refer to the weighted sum, if sample_weight is passed.  New in version 0.19.
min_impurity_splitfloat, default=None

Threshold for early stopping in tree growth. A node will split if its impurity is above the threshold, otherwise it is a leaf.  Deprecated since version 0.19: min_impurity_split has been deprecated in favor of min_impurity_decrease in 0.19. The default value of min_impurity_split has changed from 1e-7 to 0 in 0.23 and it will be removed in 1.0 (renaming of 0.25). Use min_impurity_decrease instead.
max_leaf_nodesint, default=None

Grow a tree with max_leaf_nodes in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes.
ccp_alphanon-negative float, default=0.0

Complexity parameter used for Minimal Cost-Complexity Pruning. The subtree with the largest cost complexity that is smaller than ccp_alpha will be chosen. By default, no pruning is performed. See Minimal Cost-Complexity Pruning for details.  New in version 0.22.     Attributes

max_features_int

The inferred value of max_features.
n_features_int

The number of features when fit is performed.
feature_importances_ndarray of shape (n_features,)

Return the feature importances.
n_outputs_int

The number of outputs when fit is performed.
tree_Tree instance

The underlying Tree object. Please refer to help(sklearn.tree._tree.Tree) for attributes of Tree object and Understanding the decision tree structure for basic usage of these attributes.      See also
ExtraTreeClassifier

An extremely randomized tree classifier.
sklearn.ensemble.ExtraTreesClassifier

An extra-trees classifier.
sklearn.ensemble.ExtraTreesRegressor

An extra-trees regressor.    Notes The default values for the parameters controlling the size of the trees (e.g. max_depth, min_samples_leaf, etc.) lead to fully grown and unpruned trees which can potentially be very large on some data sets. To reduce memory consumption, the complexity and size of the trees should be controlled by setting those parameter values. References
1
P. Geurts, D. Ernst., and L. Wehenkel, “Extremely randomized trees”, Machine Learning, 63(1), 3-42, 2006.   Examples >>> from sklearn.datasets import load_diabetes
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.ensemble import BaggingRegressor
>>> from sklearn.tree import ExtraTreeRegressor
>>> X, y = load_diabetes(return_X_y=True)
>>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, random_state=0)
>>> extra_tree = ExtraTreeRegressor(random_state=0)
>>> reg = BaggingRegressor(extra_tree, random_state=0).fit(
...     X_train, y_train)
>>> reg.score(X_test, y_test)
0.33...
 Methods
apply(X[, check_input]) Return the index of the leaf that each sample is predicted as.
cost_complexity_pruning_path(X, y[, …]) Compute the pruning path during Minimal Cost-Complexity Pruning.
decision_path(X[, check_input]) Return the decision path in the tree.
fit(X, y[, sample_weight, check_input, …]) Build a decision tree regressor from the training set (X, y).
get_depth() Return the depth of the decision tree.
get_n_leaves() Return the number of leaves of the decision tree.
get_params([deep]) Get parameters for this estimator.
predict(X[, check_input]) Predict class or regression value for X.
score(X, y[, sample_weight]) Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params) Set the parameters of this estimator.
apply(X, check_input=True) [source]

Return the index of the leaf that each sample is predicted as.  New in version 0.17.   Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

X_leavesarray-like of shape (n_samples,)

For each datapoint x in X, return the index of the leaf x ends up in. Leaves are numbered within [0; self.tree_.node_count), possibly with gaps in the numbering.

cost_complexity_pruning_path(X, y, sample_weight=None) [source]

Compute the pruning path during Minimal Cost-Complexity Pruning. See Minimal Cost-Complexity Pruning for details on the pruning process.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The target values (class labels) as integers or strings.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. Splits are also ignored if they would result in any single class carrying a negative weight in either child node.    Returns

ccp_pathBunch

Dictionary-like object, with the following attributes.
ccp_alphasndarray

Effective alphas of subtree during pruning.
impuritiesndarray

Sum of the impurities of the subtree leaves for the corresponding alpha value in ccp_alphas.

decision_path(X, check_input=True) [source]

Return the decision path in the tree.  New in version 0.18.   Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

indicatorsparse matrix of shape (n_samples, n_nodes)

Return a node indicator CSR matrix where non zero elements indicates that the samples goes through the nodes.

property feature_importances_
Return the feature importances. The importance of a feature is computed as the (normalized) total reduction of the criterion brought by that feature. It is also known as the Gini importance. Warning: impurity-based feature importances can be misleading for high cardinality features (many unique values). See sklearn.inspection.permutation_importance as an alternative.  Returns

feature_importances_ndarray of shape (n_features,)

Normalized total reduction of criteria by feature (Gini importance).

fit(X, y, sample_weight=None, check_input=True, X_idx_sorted='deprecated') [source]

Build a decision tree regressor from the training set (X, y).  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The target values (real numbers). Use dtype=np.float64 and order='C' for maximum efficiency.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
X_idx_sorteddeprecated, default=”deprecated”

This parameter is deprecated and has no effect. It will be removed in 1.1 (renaming of 0.26).  Deprecated since version 0.24.     Returns

selfDecisionTreeRegressor

Fitted estimator.

get_depth() [source]

Return the depth of the decision tree. The depth of a tree is the maximum distance between the root and any leaf.  Returns

self.tree_.max_depthint

The maximum depth of the tree.

get_n_leaves() [source]

Return the number of leaves of the decision tree.  Returns

self.tree_.n_leavesint

Number of leaves.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

predict(X, check_input=True) [source]

Predict class or regression value for X. For a classification model, the predicted class for each sample in X is returned. For a regression model, the predicted value based on X is returned.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.    Returns

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The predicted classes, or the predict values.

score(X, y, sample_weight=None) [source]

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

\(R^2\) of self.predict(X) wrt. y.     Notes The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


fit(X, y, sample_weight=None, check_input=True, X_idx_sorted='deprecated') [source]

Build a decision tree regressor from the training set (X, y).  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

The target values (real numbers). Use dtype=np.float64 and order='C' for maximum efficiency.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node.
check_inputbool, default=True

Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
X_idx_sorteddeprecated, default=”deprecated”

This parameter is deprecated and has no effect. It will be removed in 1.1 (renaming of 0.26).  Deprecated since version 0.24.     Returns

selfDecisionTreeRegressor

Fitted estimator.


score(X, y, sample_weight=None) [source]

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

\(R^2\) of self.predict(X) wrt. y.     Notes The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).


sklearn.tree.plot_tree(decision_tree, *, max_depth=None, feature_names=None, class_names=None, label='all', filled=False, impurity=True, node_ids=False, proportion=False, rotate='deprecated', rounded=False, precision=3, ax=None, fontsize=None) [source]

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 User Guide.  New in version 0.21.   Parameters

decision_treedecision tree regressor or classifier

The decision tree to be plotted.
max_depthint, default=None

The maximum depth of the representation. If None, the tree is fully generated.
feature_nameslist of strings, default=None

Names of each of the features. If None, generic names will be used (“X[0]”, “X[1]”, …).
class_nameslist of str 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.
filledbool, 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.
impuritybool, default=True

When set to True, show the impurity at each node.
node_idsbool, default=False

When set to True, show the ID number on each node.
proportionbool, default=False

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

This parameter has no effect on the matplotlib tree visualisation and it is kept here for backward compatibility.  Deprecated since version 0.23: rotate is deprecated in 0.23 and will be removed in 1.0 (renaming of 0.25).
roundedbool, default=False

When set to True, draw node boxes with rounded corners and use Helvetica fonts instead of Times-Roman.
precisionint, default=3

Number of digits of precision for floating point in the values of impurity, threshold and value attributes of each node.
axmatplotlib axis, default=None

Axes to plot to. If None, use current axis. Any previous content is cleared.
fontsizeint, default=None

Size of text font. If None, determined automatically to fit figure.    Returns

annotationslist 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)
[Text(251.5,345.217,'X[3] <= 0.8...


sklearn.utils.all_estimators(type_filter=None) [source]

Get a list of all estimators from sklearn. This function crawls the module and gets all classes that inherit from BaseEstimator. Classes that are defined in test-modules are not included.  Parameters

type_filter{“classifier”, “regressor”, “cluster”, “transformer”} or list of such str, default=None

Which kind of estimators should be returned. If None, no filter is applied and all estimators are returned. Possible values are ‘classifier’, ‘regressor’, ‘cluster’ and ‘transformer’ to get estimators only of these specific types, or a list of these to get the estimators that fit at least one of the types.    Returns

estimatorslist of tuples

List of (name, class), where name is the class name as string and class is the actuall type of the class.


sklearn.utils.arrayfuncs.min_pos()
Find the minimum value of an array over positive values Returns a huge value if none of the values are positive


sklearn.utils.assert_all_finite(X, *, allow_nan=False) [source]

Throw a ValueError if X contains NaN or infinity.  Parameters

X{ndarray, sparse matrix}

allow_nanbool, default=False


sklearn.utils.as_float_array(X, *, copy=True, force_all_finite=True) [source]

Converts an array-like to an array of floats. The new dtype will be np.float32 or np.float64, depending on the original type. The function can create a copy or modify the argument depending on the argument copy.  Parameters

X{array-like, sparse matrix}

copybool, default=True

If True, a copy of X will be created. If False, a copy may still be returned if X’s dtype is not a floating point type.
force_all_finitebool or ‘allow-nan’, default=True

Whether to raise an error on np.inf, np.nan, pd.NA in X. The possibilities are:  True: Force all values of X to be finite. False: accepts np.inf, np.nan, pd.NA in X. ‘allow-nan’: accepts only np.nan and pd.NA values in X. Values cannot be infinite.   New in version 0.20: force_all_finite accepts the string 'allow-nan'.   Changed in version 0.23: Accepts pd.NA and converts it into np.nan     Returns

XT{ndarray, sparse matrix}

An array of type float.


sklearn.utils.Bunch(**kwargs) [source]

Container object exposing keys as attributes. Bunch objects are sometimes used as an output for functions and methods. They extend dictionaries by enabling values to be accessed by key, bunch["value_key"], or by an attribute, bunch.value_key. Examples >>> b = Bunch(a=1, b=2)
>>> b['b']
2
>>> b.b
2
>>> b.a = 3
>>> b['a']
3
>>> b.c = 6
>>> b['c']
6


sklearn.utils.check_array(array, accept_sparse=False, *, accept_large_sparse=True, dtype='numeric', order=None, copy=False, force_all_finite=True, ensure_2d=True, allow_nd=False, ensure_min_samples=1, ensure_min_features=1, estimator=None) [source]

Input validation on an array, list, sparse matrix or similar. By default, the input is checked to be a non-empty 2D array containing only finite values. If the dtype of the array is object, attempt converting to float, raising on failure.  Parameters

arrayobject

Input object to check / convert.
accept_sparsestr, bool or list/tuple of str, default=False

String[s] representing allowed sparse matrix formats, such as ‘csc’, ‘csr’, etc. If the input is sparse but not in the allowed format, it will be converted to the first listed format. True allows the input to be any format. False means that a sparse matrix input will raise an error.
accept_large_sparsebool, default=True

If a CSR, CSC, COO or BSR sparse matrix is supplied and accepted by accept_sparse, accept_large_sparse=False will cause it to be accepted only if its indices are stored with a 32-bit dtype.  New in version 0.20.
dtype‘numeric’, type, list of type or None, default=’numeric’

Data type of result. If None, the dtype of the input is preserved. If “numeric”, dtype is preserved unless array.dtype is object. If dtype is a list of types, conversion on the first type is only performed if the dtype of the input is not in the list.
order{‘F’, ‘C’} or None, default=None

Whether an array will be forced to be fortran or c-style. When order is None (default), then if copy=False, nothing is ensured about the memory layout of the output array; otherwise (copy=True) the memory layout of the returned array is kept as close as possible to the original array.
copybool, default=False

Whether a forced copy will be triggered. If copy=False, a copy might be triggered by a conversion.
force_all_finitebool or ‘allow-nan’, default=True

Whether to raise an error on np.inf, np.nan, pd.NA in array. The possibilities are:  True: Force all values of array to be finite. False: accepts np.inf, np.nan, pd.NA in array. ‘allow-nan’: accepts only np.nan and pd.NA values in array. Values cannot be infinite.   New in version 0.20: force_all_finite accepts the string 'allow-nan'.   Changed in version 0.23: Accepts pd.NA and converts it into np.nan
ensure_2dbool, default=True

Whether to raise a value error if array is not 2D.
allow_ndbool, default=False

Whether to allow array.ndim > 2.
ensure_min_samplesint, default=1

Make sure that the array has a minimum number of samples in its first axis (rows for a 2D array). Setting to 0 disables this check.
ensure_min_featuresint, default=1

Make sure that the 2D array has some minimum number of features (columns). The default value of 1 rejects empty datasets. This check is only enforced when the input data has effectively 2 dimensions or is originally 1D and ensure_2d is True. Setting to 0 disables this check.
estimatorstr or estimator instance, default=None

If passed, include the name of the estimator in warning messages.    Returns

array_convertedobject

The converted and validated array.


sklearn.utils.check_consistent_length(*arrays) [source]

Check that all arrays have consistent first dimensions. Checks whether all objects in arrays have the same shape or length.  Parameters

*arrayslist or tuple of input objects.

Objects that will be checked for consistent length.


sklearn.utils.check_random_state(seed) [source]

Turn seed into a np.random.RandomState instance  Parameters

seedNone, int or instance of RandomState

If seed is None, return the RandomState singleton used by np.random. If seed is an int, return a new RandomState instance seeded with seed. If seed is already a RandomState instance, return it. Otherwise raise ValueError.


sklearn.utils.check_scalar(x, name, target_type, *, min_val=None, max_val=None) [source]

Validate scalar parameters type and value.  Parameters

xobject

The scalar parameter to validate.
namestr

The name of the parameter to be printed in error messages.
target_typetype or tuple

Acceptable data types for the parameter.
min_valfloat or int, default=None

The minimum valid value the parameter can take. If None (default) it is implied that the parameter does not have a lower bound.
max_valfloat or int, default=None

The maximum valid value the parameter can take. If None (default) it is implied that the parameter does not have an upper bound.    Raises
 TypeError

If the parameter’s type does not match the desired type.  ValueError

If the parameter’s value violates the given bounds.


sklearn.utils.check_X_y(X, y, accept_sparse=False, *, accept_large_sparse=True, dtype='numeric', order=None, copy=False, force_all_finite=True, ensure_2d=True, allow_nd=False, multi_output=False, ensure_min_samples=1, ensure_min_features=1, y_numeric=False, estimator=None) [source]

Input validation for standard estimators. Checks X and y for consistent length, enforces X to be 2D and y 1D. By default, X is checked to be non-empty and containing only finite values. Standard input checks are also applied to y, such as checking that y does not have np.nan or np.inf targets. For multi-label y, set multi_output=True to allow 2D and sparse y. If the dtype of X is object, attempt converting to float, raising on failure.  Parameters

X{ndarray, list, sparse matrix}

Input data.
y{ndarray, list, sparse matrix}

Labels.
accept_sparsestr, bool or list of str, default=False

String[s] representing allowed sparse matrix formats, such as ‘csc’, ‘csr’, etc. If the input is sparse but not in the allowed format, it will be converted to the first listed format. True allows the input to be any format. False means that a sparse matrix input will raise an error.
accept_large_sparsebool, default=True

If a CSR, CSC, COO or BSR sparse matrix is supplied and accepted by accept_sparse, accept_large_sparse will cause it to be accepted only if its indices are stored with a 32-bit dtype.  New in version 0.20.
dtype‘numeric’, type, list of type or None, default=’numeric’

Data type of result. If None, the dtype of the input is preserved. If “numeric”, dtype is preserved unless array.dtype is object. If dtype is a list of types, conversion on the first type is only performed if the dtype of the input is not in the list.
order{‘F’, ‘C’}, default=None

Whether an array will be forced to be fortran or c-style.
copybool, default=False

Whether a forced copy will be triggered. If copy=False, a copy might be triggered by a conversion.
force_all_finitebool or ‘allow-nan’, default=True

Whether to raise an error on np.inf, np.nan, pd.NA in X. This parameter does not influence whether y can have np.inf, np.nan, pd.NA values. The possibilities are:  True: Force all values of X to be finite. False: accepts np.inf, np.nan, pd.NA in X. ‘allow-nan’: accepts only np.nan or pd.NA values in X. Values cannot be infinite.   New in version 0.20: force_all_finite accepts the string 'allow-nan'.   Changed in version 0.23: Accepts pd.NA and converts it into np.nan
ensure_2dbool, default=True

Whether to raise a value error if X is not 2D.
allow_ndbool, default=False

Whether to allow X.ndim > 2.
multi_outputbool, default=False

Whether to allow 2D y (array or sparse matrix). If false, y will be validated as a vector. y cannot have np.nan or np.inf values if multi_output=True.
ensure_min_samplesint, default=1

Make sure that X has a minimum number of samples in its first axis (rows for a 2D array).
ensure_min_featuresint, default=1

Make sure that the 2D array has some minimum number of features (columns). The default value of 1 rejects empty datasets. This check is only enforced when X has effectively 2 dimensions or is originally 1D and ensure_2d is True. Setting to 0 disables this check.
y_numericbool, default=False

Whether to ensure that y has a numeric type. If dtype of y is object, it is converted to float64. Should only be used for regression algorithms.
estimatorstr or estimator instance, default=None

If passed, include the name of the estimator in warning messages.    Returns

X_convertedobject

The converted and validated X.
y_convertedobject

The converted and validated y.


sklearn.utils.class_weight.compute_class_weight(class_weight, *, classes, y) [source]

Estimate class weights for unbalanced datasets.  Parameters

class_weightdict, ‘balanced’ or None

If ‘balanced’, class weights will be given by n_samples / (n_classes * np.bincount(y)). If a dictionary is given, keys are classes and values are corresponding class weights. If None is given, the class weights will be uniform.
classesndarray

Array of the classes occurring in the data, as given by np.unique(y_org) with y_org the original class labels.
yarray-like of shape (n_samples,)

Array of original class labels per sample.    Returns

class_weight_vectndarray of shape (n_classes,)

Array with class_weight_vect[i] the weight for i-th class.     References The “balanced” heuristic is inspired by Logistic Regression in Rare Events Data, King, Zen, 2001.


sklearn.utils.class_weight.compute_sample_weight(class_weight, y, *, indices=None) [source]

Estimate sample weights by class for unbalanced datasets.  Parameters

class_weightdict, list of dicts, “balanced”, or None

Weights associated with classes in the form {class_label: weight}. If not given, all classes are supposed to have weight one. For multi-output problems, a list of dicts can be provided in the same order as the columns of y. Note that for multioutput (including multilabel) weights should be defined for each class of every column in its own dict. For example, for four-class multilabel classification weights should be [{0: 1, 1: 1}, {0: 1, 1: 5}, {0: 1, 1: 1}, {0: 1, 1: 1}] instead of [{1:1}, {2:5}, {3:1}, {4:1}]. The “balanced” mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data: n_samples / (n_classes * np.bincount(y)). For multi-output, the weights of each column of y will be multiplied.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

Array of original class labels per sample.
indicesarray-like of shape (n_subsample,), default=None

Array of indices to be used in a subsample. Can be of length less than n_samples in the case of a subsample, or equal to n_samples in the case of a bootstrap subsample with repeated indices. If None, the sample weight will be calculated over the full sample. Only “balanced” is supported for class_weight if this is provided.    Returns

sample_weight_vectndarray of shape (n_samples,)

Array with sample weights as applied to the original y.


sklearn.utils.deprecated(extra='') [source]

Decorator to mark a function or class as deprecated. Issue a warning when the function is called/the class is instantiated and adds a warning to the docstring. The optional extra argument will be appended to the deprecation message and the docstring. Note: to use this with the default value for extra, put in an empty of parentheses: >>> from sklearn.utils import deprecated
>>> deprecated()
<sklearn.utils.deprecation.deprecated object at ...>
 >>> @deprecated()
... def some_function(): pass
  Parameters

extrastr, default=’’

To be added to the deprecation messages.


sklearn.utils.estimator_checks.check_estimator(Estimator, generate_only=False) [source]

Check if estimator adheres to scikit-learn conventions. This estimator will run an extensive test-suite for input validation, shapes, etc, making sure that the estimator complies with scikit-learn conventions as detailed in Rolling your own estimator. Additional tests for classifiers, regressors, clustering or transformers will be run if the Estimator class inherits from the corresponding mixin from sklearn.base. Setting generate_only=True returns a generator that yields (estimator, check) tuples where the check can be called independently from each other, i.e. check(estimator). This allows all checks to be run independently and report the checks that are failing. scikit-learn provides a pytest specific decorator, parametrize_with_checks, making it easier to test multiple estimators.  Parameters

Estimatorestimator object

Estimator instance to check.  Changed in version 0.24: Passing a class was deprecated in version 0.23, and support for classes was removed in 0.24.
generate_onlybool, default=False

When False, checks are evaluated when check_estimator is called. When True, check_estimator returns a generator that yields (estimator, check) tuples. The check is run by calling check(estimator).  New in version 0.22.     Returns

checks_generatorgenerator

Generator that yields (estimator, check) tuples. Returned when generate_only=True.


sklearn.utils.estimator_checks.parametrize_with_checks(estimators) [source]

Pytest specific decorator for parametrizing estimator checks. The id of each check is set to be a pprint version of the estimator and the name of the check with its keyword arguments. This allows to use pytest -k to specify which tests to run: pytest test_check_estimators.py -k check_estimators_fit_returns_self
  Parameters

estimatorslist of estimators instances

Estimators to generated checks for.  Changed in version 0.24: Passing a class was deprecated in version 0.23, and support for classes was removed in 0.24. Pass an instance instead.   New in version 0.24.     Returns

decoratorpytest.mark.parametrize
   Examples >>> from sklearn.utils.estimator_checks import parametrize_with_checks
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.tree import DecisionTreeRegressor
 >>> @parametrize_with_checks([LogisticRegression(),
...                           DecisionTreeRegressor()])
... def test_sklearn_compatible_estimator(estimator, check):
...     check(estimator)


sklearn.utils.estimator_html_repr(estimator) [source]

Build a HTML representation of an estimator. Read more in the User Guide.  Parameters

estimatorestimator object

The estimator to visualize.    Returns
 html: str

HTML representation of estimator.


sklearn.utils.extmath.density(w, **kwargs) [source]

Compute density of a sparse vector.  Parameters

warray-like

The sparse vector.    Returns
 float

The density of w, between 0 and 1.


sklearn.utils.extmath.fast_logdet(A) [source]

Compute log(det(A)) for A symmetric. Equivalent to : np.log(nl.det(A)) but more robust. It returns -Inf if det(A) is non positive or is not defined.  Parameters

Aarray-like

The matrix.


sklearn.utils.extmath.randomized_range_finder(A, *, size, n_iter, power_iteration_normalizer='auto', random_state=None) [source]

Computes an orthonormal matrix whose range approximates the range of A.  Parameters

A2D array

The input data matrix.
sizeint

Size of the return array.
n_iterint

Number of power iterations used to stabilize the result.
power_iteration_normalizer{‘auto’, ‘QR’, ‘LU’, ‘none’}, default=’auto’

Whether the power iterations are normalized with step-by-step QR factorization (the slowest but most accurate), ‘none’ (the fastest but numerically unstable when n_iter is large, e.g. typically 5 or larger), or ‘LU’ factorization (numerically stable but can lose slightly in accuracy). The ‘auto’ mode applies no normalization if n_iter <= 2 and switches to LU otherwise.  New in version 0.18.
random_stateint, RandomState instance or None, default=None

The seed of the pseudo random number generator to use when shuffling the data, i.e. getting the random vectors to initialize the algorithm. Pass an int for reproducible results across multiple function calls. See Glossary.    Returns

Qndarray

A (size x size) projection matrix, the range of which approximates well the range of the input matrix A.     Notes Follows Algorithm 4.3 of Finding structure with randomness: Stochastic algorithms for constructing approximate matrix decompositions Halko, et al., 2009 (arXiv:909) https://arxiv.org/pdf/0909.4061.pdf An implementation of a randomized algorithm for principal component analysis A. Szlam et al. 2014


sklearn.utils.extmath.randomized_svd(M, n_components, *, n_oversamples=10, n_iter='auto', power_iteration_normalizer='auto', transpose='auto', flip_sign=True, random_state=0) [source]

Computes a truncated randomized SVD.  Parameters

M{ndarray, sparse matrix}

Matrix to decompose.
n_componentsint

Number of singular values and vectors to extract.
n_oversamplesint, default=10

Additional number of random vectors to sample the range of M so as to ensure proper conditioning. The total number of random vectors used to find the range of M is n_components + n_oversamples. Smaller number can improve speed but can negatively impact the quality of approximation of singular vectors and singular values.
n_iterint or ‘auto’, default=’auto’

Number of power iterations. It can be used to deal with very noisy problems. When ‘auto’, it is set to 4, unless n_components is small (< .1 * min(X.shape)) n_iter in which case is set to 7. This improves precision with few components.  Changed in version 0.18.
power_iteration_normalizer{‘auto’, ‘QR’, ‘LU’, ‘none’}, default=’auto’

Whether the power iterations are normalized with step-by-step QR factorization (the slowest but most accurate), ‘none’ (the fastest but numerically unstable when n_iter is large, e.g. typically 5 or larger), or ‘LU’ factorization (numerically stable but can lose slightly in accuracy). The ‘auto’ mode applies no normalization if n_iter <= 2 and switches to LU otherwise.  New in version 0.18.
transposebool or ‘auto’, default=’auto’

Whether the algorithm should be applied to M.T instead of M. The result should approximately be the same. The ‘auto’ mode will trigger the transposition if M.shape[1] > M.shape[0] since this implementation of randomized SVD tend to be a little faster in that case.  Changed in version 0.18.
flip_signbool, default=True

The output of a singular value decomposition is only unique up to a permutation of the signs of the singular vectors. If flip_sign is set to True, the sign ambiguity is resolved by making the largest loadings for each component in the left singular vectors positive.
random_stateint, RandomState instance or None, default=0

The seed of the pseudo random number generator to use when shuffling the data, i.e. getting the random vectors to initialize the algorithm. Pass an int for reproducible results across multiple function calls. See Glossary.     Notes This algorithm finds a (usually very good) approximate truncated singular value decomposition using randomization to speed up the computations. It is particularly fast on large matrices on which you wish to extract only a small number of components. In order to obtain further speed up, n_iter can be set <=2 (at the cost of loss of precision). References  Finding structure with randomness: Stochastic algorithms for constructing approximate matrix decompositions Halko, et al., 2009 https://arxiv.org/abs/0909.4061
 A randomized algorithm for the decomposition of matrices Per-Gunnar Martinsson, Vladimir Rokhlin and Mark Tygert An implementation of a randomized algorithm for principal component analysis A. Szlam et al. 2014


sklearn.utils.extmath.safe_sparse_dot(a, b, *, dense_output=False) [source]

Dot product that handle the sparse matrix case correctly.  Parameters

a{ndarray, sparse matrix}

b{ndarray, sparse matrix}

dense_outputbool, default=False

When False, a and b both being sparse will yield sparse output. When True, output will always be a dense array.    Returns

dot_product{ndarray, sparse matrix}

Sparse if a and b are sparse and dense_output=False.