Unsupervised Outlier Detection.
Estimate the support of a high-dimensional distribution.
The implementation is based on libsvm.
Read more in the User Guide.
Specifies the kernel type to be used in the algorithm. It must be one of ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ or a callable. If none is given, ‘rbf’ will be used. If a callable is given it is used to precompute the kernel matrix.
Degree of the polynomial kernel function (‘poly’). Ignored by all other kernels.
Kernel coefficient for ‘rbf’, ‘poly’ and ‘sigmoid’.
Changed in version 0.22: The default value of gamma changed from ‘auto’ to ‘scale’.
Independent term in kernel function. It is only significant in ‘poly’ and ‘sigmoid’.
Tolerance for stopping criterion.
An upper bound on the fraction of training errors and a lower bound of the fraction of support vectors. Should be in the interval (0, 1]. By default 0.5 will be taken.
Whether to use the shrinking heuristic. See the User Guide.
Specify the size of the kernel cache (in MB).
Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in libsvm that, if enabled, may not work properly in a multithreaded context.
Hard limit on iterations within solver, or -1 for no limit.
Multipliers of parameter C for each class. Computed based on the class_weight parameter.
Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel.
coef_ is readonly property derived from dual_coef_ and support_vectors_.
Coefficients of the support vectors in the decision function.
0 if correctly fitted, 1 otherwise (will raise warning)
Constant in the decision function.
Number of support vectors for each class.
Offset used to define the decision function from the raw scores. We have the relation: decision_function = score_samples - offset_. The offset is the opposite of intercept_ and is provided for consistency with other outlier detection algorithms.
New in version 0.20.
Array dimensions of training vector X.
Indices of support vectors.
Support vectors.
decision_function(X)
Signed distance to the separating hyperplane.
fit(X[, y, sample_weight])
Detects the soft boundary of the set of samples X.
fit_predict(X[, y])
Perform fit on X and returns labels for X.
get_params([deep])
Get parameters for this estimator.
predict(X)
Perform classification on samples in X.
score_samples(X)
Raw scoring function of the samples.
set_params(**params)
Set the parameters of this estimator.
Signed distance to the separating hyperplane.
Signed distance is positive for an inlier and negative for an outlier.
The data matrix.
Returns the decision function of the samples.
Detects the soft boundary of the set of samples X.
Set of samples, where n_samples is the number of samples and n_features is the number of features.
Per-sample weights. Rescale C per sample. Higher weights force the classifier to put more emphasis on these points.
not used, present for API consistency by convention.
If X is not a C-ordered contiguous array it is copied.
Perform fit on X and returns labels for X.
Returns -1 for outliers and 1 for inliers.
Not used, present for API consistency by convention.
1 for inliers, -1 for outliers.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Perform classification on samples in X.
For a one-class model, +1 or -1 is returned.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
Class labels for samples in X.
Raw scoring function of the samples.
The data matrix.
Returns the (unshifted) scoring function of the samples.
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.
Estimator parameters.
Estimator instance.


Unsupervised Outlier Detection.
Estimate the support of a high-dimensional distribution.
The implementation is based on libsvm.
Read more in the User Guide.
Specifies the kernel type to be used in the algorithm. It must be one of ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ or a callable. If none is given, ‘rbf’ will be used. If a callable is given it is used to precompute the kernel matrix.
Degree of the polynomial kernel function (‘poly’). Ignored by all other kernels.
Kernel coefficient for ‘rbf’, ‘poly’ and ‘sigmoid’.
Changed in version 0.22: The default value of gamma changed from ‘auto’ to ‘scale’.
Independent term in kernel function. It is only significant in ‘poly’ and ‘sigmoid’.
Tolerance for stopping criterion.
An upper bound on the fraction of training errors and a lower bound of the fraction of support vectors. Should be in the interval (0, 1]. By default 0.5 will be taken.
Whether to use the shrinking heuristic. See the User Guide.
Specify the size of the kernel cache (in MB).
Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in libsvm that, if enabled, may not work properly in a multithreaded context.
Hard limit on iterations within solver, or -1 for no limit.
Multipliers of parameter C for each class. Computed based on the class_weight parameter.
Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel.
coef_ is readonly property derived from dual_coef_ and support_vectors_.
Coefficients of the support vectors in the decision function.
0 if correctly fitted, 1 otherwise (will raise warning)
Constant in the decision function.
Number of support vectors for each class.
Offset used to define the decision function from the raw scores. We have the relation: decision_function = score_samples - offset_. The offset is the opposite of intercept_ and is provided for consistency with other outlier detection algorithms.
New in version 0.20.
Array dimensions of training vector X.
Indices of support vectors.
Support vectors.
decision_function(X)
Signed distance to the separating hyperplane.
fit(X[, y, sample_weight])
Detects the soft boundary of the set of samples X.
fit_predict(X[, y])
Perform fit on X and returns labels for X.
get_params([deep])
Get parameters for this estimator.
predict(X)
Perform classification on samples in X.
score_samples(X)
Raw scoring function of the samples.
set_params(**params)
Set the parameters of this estimator.
Signed distance to the separating hyperplane.
Signed distance is positive for an inlier and negative for an outlier.
The data matrix.
Returns the decision function of the samples.
Detects the soft boundary of the set of samples X.
Set of samples, where n_samples is the number of samples and n_features is the number of features.
Per-sample weights. Rescale C per sample. Higher weights force the classifier to put more emphasis on these points.
not used, present for API consistency by convention.
If X is not a C-ordered contiguous array it is copied.
Perform fit on X and returns labels for X.
Returns -1 for outliers and 1 for inliers.
Not used, present for API consistency by convention.
1 for inliers, -1 for outliers.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Perform classification on samples in X.
For a one-class model, +1 or -1 is returned.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
Class labels for samples in X.
Raw scoring function of the samples.
The data matrix.
Returns the (unshifted) scoring function of the samples.
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.
Estimator parameters.
Estimator instance.
Outlier detection on a real data set
Species distribution modeling
Libsvm GUI
Comparing anomaly detection algorithms for outlier detection on toy datasets
One-class SVM with non-linear kernel (RBF)


Signed distance to the separating hyperplane.
Signed distance is positive for an inlier and negative for an outlier.
The data matrix.
Returns the decision function of the samples.


Detects the soft boundary of the set of samples X.
Set of samples, where n_samples is the number of samples and n_features is the number of features.
Per-sample weights. Rescale C per sample. Higher weights force the classifier to put more emphasis on these points.
not used, present for API consistency by convention.
If X is not a C-ordered contiguous array it is copied.


Perform fit on X and returns labels for X.
Returns -1 for outliers and 1 for inliers.
Not used, present for API consistency by convention.
1 for inliers, -1 for outliers.


Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.


Perform classification on samples in X.
For a one-class model, +1 or -1 is returned.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
Class labels for samples in X.


Raw scoring function of the samples.
The data matrix.
Returns the (unshifted) scoring function of the samples.


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.
Estimator parameters.
Estimator instance.


C-Support Vector Classification.
The implementation is based on libsvm. The fit time scales at least quadratically with the number of samples and may be impractical beyond tens of thousands of samples. For large datasets consider using LinearSVC or SGDClassifier instead, possibly after a Nystroem transformer.
The multiclass support is handled according to a one-vs-one scheme.
For details on the precise mathematical formulation of the provided kernel functions and how gamma, coef0 and degree affect each other, see the corresponding section in the narrative documentation: Kernel functions.
Read more in the User Guide.
Regularization parameter. The strength of the regularization is inversely proportional to C. Must be strictly positive. The penalty is a squared l2 penalty.
Specifies the kernel type to be used in the algorithm. It must be one of ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ or a callable. If none is given, ‘rbf’ will be used. If a callable is given it is used to pre-compute the kernel matrix from data matrices; that matrix should be an array of shape (n_samples, n_samples).
Degree of the polynomial kernel function (‘poly’). Ignored by all other kernels.
Kernel coefficient for ‘rbf’, ‘poly’ and ‘sigmoid’.
Changed in version 0.22: The default value of gamma changed from ‘auto’ to ‘scale’.
Independent term in kernel function. It is only significant in ‘poly’ and ‘sigmoid’.
Whether to use the shrinking heuristic. See the User Guide.
Whether to enable probability estimates. This must be enabled prior to calling fit, will slow down that method as it internally uses 5-fold cross-validation, and predict_proba may be inconsistent with predict. Read more in the User Guide.
Tolerance for stopping criterion.
Specify the size of the kernel cache (in MB).
Set the parameter C of class i to class_weight[i]*C for SVC. If not given, all classes are supposed to have weight one. 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))
Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in libsvm that, if enabled, may not work properly in a multithreaded context.
Hard limit on iterations within solver, or -1 for no limit.
Whether to return a one-vs-rest (‘ovr’) decision function of shape (n_samples, n_classes) as all other classifiers, or the original one-vs-one (‘ovo’) decision function of libsvm which has shape (n_samples, n_classes * (n_classes - 1) / 2). However, one-vs-one (‘ovo’) is always used as multi-class strategy. The parameter is ignored for binary classification.
Changed in version 0.19: decision_function_shape is ‘ovr’ by default.
New in version 0.17: decision_function_shape=’ovr’ is recommended.
Changed in version 0.17: Deprecated decision_function_shape=’ovo’ and None.
If true, decision_function_shape='ovr', and number of classes > 2, predict will break ties according to the confidence values of decision_function; otherwise the first class among the tied classes is returned. Please note that breaking ties comes at a relatively high computational cost compared to a simple predict.
New in version 0.22.
Controls the pseudo random number generation for shuffling the data for probability estimates. Ignored when probability is False. Pass an int for reproducible output across multiple function calls. See Glossary.
Multipliers of parameter C for each class. Computed based on the class_weight parameter.
The classes labels.
Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel.
coef_ is a readonly property derived from dual_coef_ and support_vectors_.
Dual coefficients of the support vector in the decision function (see Mathematical formulation), multiplied by their targets. For multiclass, coefficient for all 1-vs-1 classifiers. The layout of the coefficients in the multiclass case is somewhat non-trivial. See the multi-class section of the User Guide for details.
0 if correctly fitted, 1 otherwise (will raise warning)
Constants in decision function.
Indices of support vectors.
Support vectors.
Number of support vectors for each class.
If probability=True, it corresponds to the parameters learned in Platt scaling to produce probability estimates from decision values. If probability=False, it’s an empty array. Platt scaling uses the logistic function 1 / (1 + exp(decision_value * probA_ + probB_)) where probA_ and probB_ are learned from the dataset [2]. For more information on the multiclass case and training procedure see section 8 of [1].
Array dimensions of training vector X.
See also
Support Vector Machine for Regression implemented using libsvm.
Scalable Linear Support Vector Machine for classification implemented using liblinear. Check the See Also section of LinearSVC for more comparison element.
LIBSVM: A Library for Support Vector Machines
Platt, John (1999). “Probabilistic outputs for support vector machines and comparison to regularizedlikelihood methods.”
decision_function(X)
Evaluates the decision function for the samples in X.
fit(X, y[, sample_weight])
Fit the SVM model according to the given training data.
get_params([deep])
Get parameters for this estimator.
predict(X)
Perform classification on samples in 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.
Evaluates the decision function for the samples in X.
Returns the decision function of the sample for each class in the model. If decision_function_shape=’ovr’, the shape is (n_samples, n_classes).
If decision_function_shape=’ovo’, the function values are proportional to the distance of the samples X to the separating hyperplane. If the exact distances are required, divide the function values by the norm of the weight vector (coef_). See also this question for further details. If decision_function_shape=’ovr’, the decision function is a monotonic transformation of ovo decision function.
Fit the SVM model according to the given training data.
Training vectors, where n_samples is the number of samples and n_features is the number of features. For kernel=”precomputed”, the expected shape of X is (n_samples, n_samples).
Target values (class labels in classification, real numbers in regression).
Per-sample weights. Rescale C per sample. Higher weights force the classifier to put more emphasis on these points.
If X and y are not C-ordered and contiguous arrays of np.float64 and X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
If X is a dense array, then the other methods will not support sparse matrices as input.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Perform classification on samples in X.
For an one-class model, +1 or -1 is returned.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
Class labels for samples in X.
Compute log probabilities of possible outcomes for samples in X.
The model need to have probability information computed at training time: fit with attribute probability set to True.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
Returns the log-probabilities of the sample for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.
The probability model is created using cross validation, so the results can be slightly different than those obtained by predict. Also, it will produce meaningless results on very small datasets.
Compute probabilities of possible outcomes for samples in X.
The model need to have probability information computed at training time: fit with attribute probability set to True.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
Returns the probability of the sample for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.
The probability model is created using cross validation, so the results can be slightly different than those obtained by predict. Also, it will produce meaningless results on very small datasets.
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.
Test samples.
True labels for X.
Sample weights.
Mean accuracy of self.predict(X) wrt. y.
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.
Estimator parameters.
Estimator instance.


C-Support Vector Classification.
The implementation is based on libsvm. The fit time scales at least quadratically with the number of samples and may be impractical beyond tens of thousands of samples. For large datasets consider using LinearSVC or SGDClassifier instead, possibly after a Nystroem transformer.
The multiclass support is handled according to a one-vs-one scheme.
For details on the precise mathematical formulation of the provided kernel functions and how gamma, coef0 and degree affect each other, see the corresponding section in the narrative documentation: Kernel functions.
Read more in the User Guide.
Regularization parameter. The strength of the regularization is inversely proportional to C. Must be strictly positive. The penalty is a squared l2 penalty.
Specifies the kernel type to be used in the algorithm. It must be one of ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ or a callable. If none is given, ‘rbf’ will be used. If a callable is given it is used to pre-compute the kernel matrix from data matrices; that matrix should be an array of shape (n_samples, n_samples).
Degree of the polynomial kernel function (‘poly’). Ignored by all other kernels.
Kernel coefficient for ‘rbf’, ‘poly’ and ‘sigmoid’.
Changed in version 0.22: The default value of gamma changed from ‘auto’ to ‘scale’.
Independent term in kernel function. It is only significant in ‘poly’ and ‘sigmoid’.
Whether to use the shrinking heuristic. See the User Guide.
Whether to enable probability estimates. This must be enabled prior to calling fit, will slow down that method as it internally uses 5-fold cross-validation, and predict_proba may be inconsistent with predict. Read more in the User Guide.
Tolerance for stopping criterion.
Specify the size of the kernel cache (in MB).
Set the parameter C of class i to class_weight[i]*C for SVC. If not given, all classes are supposed to have weight one. 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))
Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in libsvm that, if enabled, may not work properly in a multithreaded context.
Hard limit on iterations within solver, or -1 for no limit.
Whether to return a one-vs-rest (‘ovr’) decision function of shape (n_samples, n_classes) as all other classifiers, or the original one-vs-one (‘ovo’) decision function of libsvm which has shape (n_samples, n_classes * (n_classes - 1) / 2). However, one-vs-one (‘ovo’) is always used as multi-class strategy. The parameter is ignored for binary classification.
Changed in version 0.19: decision_function_shape is ‘ovr’ by default.
New in version 0.17: decision_function_shape=’ovr’ is recommended.
Changed in version 0.17: Deprecated decision_function_shape=’ovo’ and None.
If true, decision_function_shape='ovr', and number of classes > 2, predict will break ties according to the confidence values of decision_function; otherwise the first class among the tied classes is returned. Please note that breaking ties comes at a relatively high computational cost compared to a simple predict.
New in version 0.22.
Controls the pseudo random number generation for shuffling the data for probability estimates. Ignored when probability is False. Pass an int for reproducible output across multiple function calls. See Glossary.
Multipliers of parameter C for each class. Computed based on the class_weight parameter.
The classes labels.
Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel.
coef_ is a readonly property derived from dual_coef_ and support_vectors_.
Dual coefficients of the support vector in the decision function (see Mathematical formulation), multiplied by their targets. For multiclass, coefficient for all 1-vs-1 classifiers. The layout of the coefficients in the multiclass case is somewhat non-trivial. See the multi-class section of the User Guide for details.
0 if correctly fitted, 1 otherwise (will raise warning)
Constants in decision function.
Indices of support vectors.
Support vectors.
Number of support vectors for each class.
If probability=True, it corresponds to the parameters learned in Platt scaling to produce probability estimates from decision values. If probability=False, it’s an empty array. Platt scaling uses the logistic function 1 / (1 + exp(decision_value * probA_ + probB_)) where probA_ and probB_ are learned from the dataset [2]. For more information on the multiclass case and training procedure see section 8 of [1].
Array dimensions of training vector X.
See also
Support Vector Machine for Regression implemented using libsvm.
Scalable Linear Support Vector Machine for classification implemented using liblinear. Check the See Also section of LinearSVC for more comparison element.
LIBSVM: A Library for Support Vector Machines
Platt, John (1999). “Probabilistic outputs for support vector machines and comparison to regularizedlikelihood methods.”
decision_function(X)
Evaluates the decision function for the samples in X.
fit(X, y[, sample_weight])
Fit the SVM model according to the given training data.
get_params([deep])
Get parameters for this estimator.
predict(X)
Perform classification on samples in 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.
Evaluates the decision function for the samples in X.
Returns the decision function of the sample for each class in the model. If decision_function_shape=’ovr’, the shape is (n_samples, n_classes).
If decision_function_shape=’ovo’, the function values are proportional to the distance of the samples X to the separating hyperplane. If the exact distances are required, divide the function values by the norm of the weight vector (coef_). See also this question for further details. If decision_function_shape=’ovr’, the decision function is a monotonic transformation of ovo decision function.
Fit the SVM model according to the given training data.
Training vectors, where n_samples is the number of samples and n_features is the number of features. For kernel=”precomputed”, the expected shape of X is (n_samples, n_samples).
Target values (class labels in classification, real numbers in regression).
Per-sample weights. Rescale C per sample. Higher weights force the classifier to put more emphasis on these points.
If X and y are not C-ordered and contiguous arrays of np.float64 and X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
If X is a dense array, then the other methods will not support sparse matrices as input.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Perform classification on samples in X.
For an one-class model, +1 or -1 is returned.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
Class labels for samples in X.
Compute log probabilities of possible outcomes for samples in X.
The model need to have probability information computed at training time: fit with attribute probability set to True.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
Returns the log-probabilities of the sample for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.
The probability model is created using cross validation, so the results can be slightly different than those obtained by predict. Also, it will produce meaningless results on very small datasets.
Compute probabilities of possible outcomes for samples in X.
The model need to have probability information computed at training time: fit with attribute probability set to True.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
Returns the probability of the sample for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.
The probability model is created using cross validation, so the results can be slightly different than those obtained by predict. Also, it will produce meaningless results on very small datasets.
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.
Test samples.
True labels for X.
Sample weights.
Mean accuracy of self.predict(X) wrt. y.
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.
Estimator parameters.
Estimator instance.
Release Highlights for scikit-learn 0.24
Release Highlights for scikit-learn 0.22
Recognizing hand-written digits
Plot classification probability
Classifier comparison
Plot the decision boundaries of a VotingClassifier
Faces recognition example using eigenfaces and SVMs
Libsvm GUI
Recursive feature elimination
Recursive feature elimination with cross-validation
Test with permutations the significance of a classification score
Scalable learning with polynomial kernel aproximation
ROC Curve with Visualization API
Multilabel classification
Explicit feature map approximation for RBF kernels
Confusion matrix
Plotting Validation Curves
Parameter estimation using grid search with cross-validation
Receiver Operating Characteristic (ROC) with cross validation
Nested versus non-nested cross-validation
Comparison between grid search and successive halving
Receiver Operating Characteristic (ROC)
Plotting Learning Curves
Statistical comparison of models using grid search
Concatenating multiple feature extraction methods
Feature discretization
Decision boundary of semi-supervised classifiers versus SVM on the Iris dataset
Effect of varying threshold for self-training
SVM: Maximum margin separating hyperplane
SVM with custom kernel
SVM Tie Breaking Example
SVM: Weighted samples
SVM: Separating hyperplane for unbalanced classes
SVM-Kernels
SVM-Anova: SVM with univariate feature selection
SVM Margins Example
Plot different SVM classifiers in the iris dataset
RBF SVM parameters
Cross-validation on Digits Dataset Exercise
SVM Exercise


Evaluates the decision function for the samples in X.
Returns the decision function of the sample for each class in the model. If decision_function_shape=’ovr’, the shape is (n_samples, n_classes).
If decision_function_shape=’ovo’, the function values are proportional to the distance of the samples X to the separating hyperplane. If the exact distances are required, divide the function values by the norm of the weight vector (coef_). See also this question for further details. If decision_function_shape=’ovr’, the decision function is a monotonic transformation of ovo decision function.


Fit the SVM model according to the given training data.
Training vectors, where n_samples is the number of samples and n_features is the number of features. For kernel=”precomputed”, the expected shape of X is (n_samples, n_samples).
Target values (class labels in classification, real numbers in regression).
Per-sample weights. Rescale C per sample. Higher weights force the classifier to put more emphasis on these points.
If X and y are not C-ordered and contiguous arrays of np.float64 and X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
If X is a dense array, then the other methods will not support sparse matrices as input.


Perform classification on samples in X.
For an one-class model, +1 or -1 is returned.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
Class labels for samples in X.


Compute log probabilities of possible outcomes for samples in X.
The model need to have probability information computed at training time: fit with attribute probability set to True.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
Returns the log-probabilities of the sample for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.
The probability model is created using cross validation, so the results can be slightly different than those obtained by predict. Also, it will produce meaningless results on very small datasets.


Compute probabilities of possible outcomes for samples in X.
The model need to have probability information computed at training time: fit with attribute probability set to True.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
Returns the probability of the sample for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.
The probability model is created using cross validation, so the results can be slightly different than those obtained by predict. Also, it will produce meaningless results on very small datasets.


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.
Test samples.
True labels for X.
Sample weights.
Mean accuracy of self.predict(X) wrt. y.


Epsilon-Support Vector Regression.
The free parameters in the model are C and epsilon.
The implementation is based on libsvm. The fit time complexity is more than quadratic with the number of samples which makes it hard to scale to datasets with more than a couple of 10000 samples. For large datasets consider using LinearSVR or SGDRegressor instead, possibly after a Nystroem transformer.
Read more in the User Guide.
Specifies the kernel type to be used in the algorithm. It must be one of ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ or a callable. If none is given, ‘rbf’ will be used. If a callable is given it is used to precompute the kernel matrix.
Degree of the polynomial kernel function (‘poly’). Ignored by all other kernels.
Kernel coefficient for ‘rbf’, ‘poly’ and ‘sigmoid’.
Changed in version 0.22: The default value of gamma changed from ‘auto’ to ‘scale’.
Independent term in kernel function. It is only significant in ‘poly’ and ‘sigmoid’.
Tolerance for stopping criterion.
Regularization parameter. The strength of the regularization is inversely proportional to C. Must be strictly positive. The penalty is a squared l2 penalty.
Epsilon in the epsilon-SVR model. It specifies the epsilon-tube within which no penalty is associated in the training loss function with points predicted within a distance epsilon from the actual value.
Whether to use the shrinking heuristic. See the User Guide.
Specify the size of the kernel cache (in MB).
Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in libsvm that, if enabled, may not work properly in a multithreaded context.
Hard limit on iterations within solver, or -1 for no limit.
Multipliers of parameter C for each class. Computed based on the class_weight parameter.
Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel.
coef_ is readonly property derived from dual_coef_ and support_vectors_.
Coefficients of the support vector in the decision function.
0 if correctly fitted, 1 otherwise (will raise warning)
Constants in decision function.
Number of support vectors for each class.
Array dimensions of training vector X.
Indices of support vectors.
Support vectors.
See also
Support Vector Machine for regression implemented using libsvm using a parameter to control the number of support vectors.
Scalable Linear Support Vector Machine for regression implemented using liblinear.
LIBSVM: A Library for Support Vector Machines
Platt, John (1999). “Probabilistic outputs for support vector machines and comparison to regularizedlikelihood methods.”
fit(X, y[, sample_weight])
Fit the SVM model according to the given training data.
get_params([deep])
Get parameters for this estimator.
predict(X)
Perform regression on samples in 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.
Fit the SVM model according to the given training data.
Training vectors, where n_samples is the number of samples and n_features is the number of features. For kernel=”precomputed”, the expected shape of X is (n_samples, n_samples).
Target values (class labels in classification, real numbers in regression).
Per-sample weights. Rescale C per sample. Higher weights force the classifier to put more emphasis on these points.
If X and y are not C-ordered and contiguous arrays of np.float64 and X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
If X is a dense array, then the other methods will not support sparse matrices as input.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Perform regression on samples in X.
For an one-class model, +1 (inlier) or -1 (outlier) is returned.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
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.
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.
True values for X.
Sample weights.
\(R^2\) of self.predict(X) wrt. y.
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 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.
Estimator parameters.
Estimator instance.


Epsilon-Support Vector Regression.
The free parameters in the model are C and epsilon.
The implementation is based on libsvm. The fit time complexity is more than quadratic with the number of samples which makes it hard to scale to datasets with more than a couple of 10000 samples. For large datasets consider using LinearSVR or SGDRegressor instead, possibly after a Nystroem transformer.
Read more in the User Guide.
Specifies the kernel type to be used in the algorithm. It must be one of ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ or a callable. If none is given, ‘rbf’ will be used. If a callable is given it is used to precompute the kernel matrix.
Degree of the polynomial kernel function (‘poly’). Ignored by all other kernels.
Kernel coefficient for ‘rbf’, ‘poly’ and ‘sigmoid’.
Changed in version 0.22: The default value of gamma changed from ‘auto’ to ‘scale’.
Independent term in kernel function. It is only significant in ‘poly’ and ‘sigmoid’.
Tolerance for stopping criterion.
Regularization parameter. The strength of the regularization is inversely proportional to C. Must be strictly positive. The penalty is a squared l2 penalty.
Epsilon in the epsilon-SVR model. It specifies the epsilon-tube within which no penalty is associated in the training loss function with points predicted within a distance epsilon from the actual value.
Whether to use the shrinking heuristic. See the User Guide.
Specify the size of the kernel cache (in MB).
Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in libsvm that, if enabled, may not work properly in a multithreaded context.
Hard limit on iterations within solver, or -1 for no limit.
Multipliers of parameter C for each class. Computed based on the class_weight parameter.
Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel.
coef_ is readonly property derived from dual_coef_ and support_vectors_.
Coefficients of the support vector in the decision function.
0 if correctly fitted, 1 otherwise (will raise warning)
Constants in decision function.
Number of support vectors for each class.
Array dimensions of training vector X.
Indices of support vectors.
Support vectors.
See also
Support Vector Machine for regression implemented using libsvm using a parameter to control the number of support vectors.
Scalable Linear Support Vector Machine for regression implemented using liblinear.
LIBSVM: A Library for Support Vector Machines
Platt, John (1999). “Probabilistic outputs for support vector machines and comparison to regularizedlikelihood methods.”
fit(X, y[, sample_weight])
Fit the SVM model according to the given training data.
get_params([deep])
Get parameters for this estimator.
predict(X)
Perform regression on samples in 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.
Fit the SVM model according to the given training data.
Training vectors, where n_samples is the number of samples and n_features is the number of features. For kernel=”precomputed”, the expected shape of X is (n_samples, n_samples).
Target values (class labels in classification, real numbers in regression).
Per-sample weights. Rescale C per sample. Higher weights force the classifier to put more emphasis on these points.
If X and y are not C-ordered and contiguous arrays of np.float64 and X is not a scipy.sparse.csr_matrix, X and/or y may be copied.
If X is a dense array, then the other methods will not support sparse matrices as input.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Perform regression on samples in X.
For an one-class model, +1 (inlier) or -1 (outlier) is returned.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).
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.
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.
True values for X.
Sample weights.
\(R^2\) of self.predict(X) wrt. y.
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 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.
Estimator parameters.
Estimator instance.
Prediction Latency
Comparison of kernel ridge regression and SVR
Support Vector Regression (SVR) using linear and non-linear kernels


Perform regression on samples in X.
For an one-class model, +1 (inlier) or -1 (outlier) is returned.
For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).


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.
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.
True values for X.
Sample weights.
\(R^2\) of self.predict(X) wrt. y.
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).


A decision tree classifier.
Read more in the User Guide.
The function to measure the quality of a split. Supported criteria are “gini” for the Gini impurity and “entropy” for the information gain.
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.
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.
The minimum number of samples required to split an internal node:
Changed in version 0.18: Added float values for fractions.
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.
Changed in version 0.18: Added float values for fractions.
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.
The number of features to consider when looking for the best split:
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.
Controls the randomness of the estimator. The features are always randomly permuted at each split, even if splitter is set to "best". When max_features < n_features, the algorithm will select max_features at random at each split before finding the best split among them. But the best found split may vary across different runs, even if max_features=n_features. That is the case, if the improvement of the criterion is identical for several splits and one split has to be selected at random. To obtain a deterministic behaviour during fitting, random_state has to be fixed to an integer. See Glossary for details.
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.
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:
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.
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.
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.
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.
The classes labels (single output problem), or a list of arrays of class labels (multi-output problem).
Return the feature importances.
The inferred value of max_features.
The number of classes (for single output problems), or a list containing the number of classes for each output (for multi-output problems).
The number of features when fit is performed.
The number of outputs when fit is performed.
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
A decision tree regressor.
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.
The predict method operates using the numpy.argmax function on the outputs of predict_proba. This means that in case the highest predicted probabilities are tied, the classifier will predict the tied class with the lowest index in classes_.
https://en.wikipedia.org/wiki/Decision_tree_learning
L. Breiman, J. Friedman, R. Olshen, and C. Stone, “Classification and Regression Trees”, Wadsworth, Belmont, CA, 1984.
T. Hastie, R. Tibshirani and J. Friedman. “Elements of Statistical Learning”, Springer, 2009.
L. Breiman, and A. Cutler, “Random Forests”, https://www.stat.berkeley.edu/~breiman/RandomForests/cc_home.htm
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.
Return the index of the leaf that each sample is predicted as.
New in version 0.17.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
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.
Compute the pruning path during Minimal Cost-Complexity Pruning.
See Minimal Cost-Complexity Pruning for details on the pruning process.
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.
The target values (class labels) as integers or strings.
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.
Dictionary-like object, with the following attributes.
Effective alphas of subtree during pruning.
Sum of the impurities of the subtree leaves for the corresponding alpha value in ccp_alphas.
Return the decision path in the tree.
New in version 0.18.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
Return a node indicator CSR matrix where non zero elements indicates that the samples goes through the nodes.
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.
Normalized total reduction of criteria by feature (Gini importance).
Build a decision tree classifier from the training set (X, y).
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.
The target values (class labels) as integers or strings.
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.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
This parameter is deprecated and has no effect. It will be removed in 1.1 (renaming of 0.26).
Deprecated since version 0.24.
Fitted estimator.
Return the depth of the decision tree.
The depth of a tree is the maximum distance between the root and any leaf.
The maximum depth of the tree.
Return the number of leaves of the decision tree.
Number of leaves.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
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.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
The predicted classes, or the predict values.
Predict class log-probabilities of the input samples X.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
The class log-probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.
Predict class probabilities of the input samples X.
The predicted class probability is the fraction of samples of the same class in a leaf.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
The class probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.
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.
Test samples.
True labels for X.
Sample weights.
Mean accuracy of self.predict(X) wrt. y.
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.
Estimator parameters.
Estimator instance.


A decision tree classifier.
Read more in the User Guide.
The function to measure the quality of a split. Supported criteria are “gini” for the Gini impurity and “entropy” for the information gain.
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.
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.
The minimum number of samples required to split an internal node:
Changed in version 0.18: Added float values for fractions.
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.
Changed in version 0.18: Added float values for fractions.
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.
The number of features to consider when looking for the best split:
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.
Controls the randomness of the estimator. The features are always randomly permuted at each split, even if splitter is set to "best". When max_features < n_features, the algorithm will select max_features at random at each split before finding the best split among them. But the best found split may vary across different runs, even if max_features=n_features. That is the case, if the improvement of the criterion is identical for several splits and one split has to be selected at random. To obtain a deterministic behaviour during fitting, random_state has to be fixed to an integer. See Glossary for details.
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.
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:
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.
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.
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.
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.
The classes labels (single output problem), or a list of arrays of class labels (multi-output problem).
Return the feature importances.
The inferred value of max_features.
The number of classes (for single output problems), or a list containing the number of classes for each output (for multi-output problems).
The number of features when fit is performed.
The number of outputs when fit is performed.
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
A decision tree regressor.
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.
The predict method operates using the numpy.argmax function on the outputs of predict_proba. This means that in case the highest predicted probabilities are tied, the classifier will predict the tied class with the lowest index in classes_.
https://en.wikipedia.org/wiki/Decision_tree_learning
L. Breiman, J. Friedman, R. Olshen, and C. Stone, “Classification and Regression Trees”, Wadsworth, Belmont, CA, 1984.
T. Hastie, R. Tibshirani and J. Friedman. “Elements of Statistical Learning”, Springer, 2009.
L. Breiman, and A. Cutler, “Random Forests”, https://www.stat.berkeley.edu/~breiman/RandomForests/cc_home.htm
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.
Return the index of the leaf that each sample is predicted as.
New in version 0.17.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
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.
Compute the pruning path during Minimal Cost-Complexity Pruning.
See Minimal Cost-Complexity Pruning for details on the pruning process.
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.
The target values (class labels) as integers or strings.
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.
Dictionary-like object, with the following attributes.
Effective alphas of subtree during pruning.
Sum of the impurities of the subtree leaves for the corresponding alpha value in ccp_alphas.
Return the decision path in the tree.
New in version 0.18.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
Return a node indicator CSR matrix where non zero elements indicates that the samples goes through the nodes.
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.
Normalized total reduction of criteria by feature (Gini importance).
Build a decision tree classifier from the training set (X, y).
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.
The target values (class labels) as integers or strings.
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.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
This parameter is deprecated and has no effect. It will be removed in 1.1 (renaming of 0.26).
Deprecated since version 0.24.
Fitted estimator.
Return the depth of the decision tree.
The depth of a tree is the maximum distance between the root and any leaf.
The maximum depth of the tree.
Return the number of leaves of the decision tree.
Number of leaves.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
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.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
The predicted classes, or the predict values.
Predict class log-probabilities of the input samples X.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
The class log-probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.
Predict class probabilities of the input samples X.
The predicted class probability is the fraction of samples of the same class in a leaf.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
The class probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.
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.
Test samples.
True labels for X.
Sample weights.
Mean accuracy of self.predict(X) wrt. y.
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.
Estimator parameters.
Estimator instance.
Classifier comparison
Plot the decision surface of a decision tree on the iris dataset
Post pruning decision trees with cost complexity pruning
Understanding the decision tree structure
Plot the decision boundaries of a VotingClassifier
Two-class AdaBoost
Multi-class AdaBoosted Decision Trees
Discrete versus Real AdaBoost
Plot the decision surfaces of ensembles of trees on the iris dataset
Demonstration of multi-metric evaluation on cross_val_score and GridSearchCV


Return the index of the leaf that each sample is predicted as.
New in version 0.17.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
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.


Compute the pruning path during Minimal Cost-Complexity Pruning.
See Minimal Cost-Complexity Pruning for details on the pruning process.
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.
The target values (class labels) as integers or strings.
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.
Dictionary-like object, with the following attributes.
Effective alphas of subtree during pruning.
Sum of the impurities of the subtree leaves for the corresponding alpha value in ccp_alphas.


Return the decision path in the tree.
New in version 0.18.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
Return a node indicator CSR matrix where non zero elements indicates that the samples goes through the nodes.


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.
Normalized total reduction of criteria by feature (Gini importance).


Build a decision tree classifier from the training set (X, y).
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.
The target values (class labels) as integers or strings.
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.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
This parameter is deprecated and has no effect. It will be removed in 1.1 (renaming of 0.26).
Deprecated since version 0.24.
Fitted estimator.


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


Return the number of leaves of the decision tree.
Number of leaves.


Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.


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.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
The predicted classes, or the predict values.


Predict class log-probabilities of the input samples X.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
The class log-probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.


Predict class probabilities of the input samples X.
The predicted class probability is the fraction of samples of the same class in a leaf.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
The class probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.


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.
Test samples.
True labels for X.
Sample weights.
Mean accuracy of self.predict(X) wrt. y.


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.
Estimator parameters.
Estimator instance.


A decision tree regressor.
Read more in the User Guide.
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 minimizes the L2 loss using the mean of each terminal node, “friedman_mse”, which uses mean squared error with Friedman’s improvement score for potential splits, “mae” for the mean absolute error, which minimizes the L1 loss using the median of each terminal node, and “poisson” which uses reduction in Poisson deviance to find splits.
New in version 0.18: Mean Absolute Error (MAE) criterion.
New in version 0.24: Poisson deviance criterion.
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.
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.
The minimum number of samples required to split an internal node:
Changed in version 0.18: Added float values for fractions.
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.
Changed in version 0.18: Added float values for fractions.
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.
The number of features to consider when looking for the best split:
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.
Controls the randomness of the estimator. The features are always randomly permuted at each split, even if splitter is set to "best". When max_features < n_features, the algorithm will select max_features at random at each split before finding the best split among them. But the best found split may vary across different runs, even if max_features=n_features. That is the case, if the improvement of the criterion is identical for several splits and one split has to be selected at random. To obtain a deterministic behaviour during fitting, random_state has to be fixed to an integer. See Glossary for details.
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.
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:
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.
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.
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.
Return the feature importances.
The inferred value of max_features.
The number of features when fit is performed.
The number of outputs when fit is performed.
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
A decision tree classifier.
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.
https://en.wikipedia.org/wiki/Decision_tree_learning
L. Breiman, J. Friedman, R. Olshen, and C. Stone, “Classification and Regression Trees”, Wadsworth, Belmont, CA, 1984.
T. Hastie, R. Tibshirani and J. Friedman. “Elements of Statistical Learning”, Springer, 2009.
L. Breiman, and A. Cutler, “Random Forests”, https://www.stat.berkeley.edu/~breiman/RandomForests/cc_home.htm
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.
Return the index of the leaf that each sample is predicted as.
New in version 0.17.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
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.
Compute the pruning path during Minimal Cost-Complexity Pruning.
See Minimal Cost-Complexity Pruning for details on the pruning process.
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.
The target values (class labels) as integers or strings.
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.
Dictionary-like object, with the following attributes.
Effective alphas of subtree during pruning.
Sum of the impurities of the subtree leaves for the corresponding alpha value in ccp_alphas.
Return the decision path in the tree.
New in version 0.18.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
Return a node indicator CSR matrix where non zero elements indicates that the samples goes through the nodes.
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.
Normalized total reduction of criteria by feature (Gini importance).
Build a decision tree regressor from the training set (X, y).
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.
The target values (real numbers). Use dtype=np.float64 and order='C' for maximum efficiency.
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.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
This parameter is deprecated and has no effect. It will be removed in 1.1 (renaming of 0.26).
Deprecated since version 0.24.
Fitted estimator.
Return the depth of the decision tree.
The depth of a tree is the maximum distance between the root and any leaf.
The maximum depth of the tree.
Return the number of leaves of the decision tree.
Number of leaves.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
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.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
The predicted classes, or the predict values.
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.
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.
True values for X.
Sample weights.
\(R^2\) of self.predict(X) wrt. y.
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 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.
Estimator parameters.
Estimator instance.


A decision tree regressor.
Read more in the User Guide.
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 minimizes the L2 loss using the mean of each terminal node, “friedman_mse”, which uses mean squared error with Friedman’s improvement score for potential splits, “mae” for the mean absolute error, which minimizes the L1 loss using the median of each terminal node, and “poisson” which uses reduction in Poisson deviance to find splits.
New in version 0.18: Mean Absolute Error (MAE) criterion.
New in version 0.24: Poisson deviance criterion.
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.
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.
The minimum number of samples required to split an internal node:
Changed in version 0.18: Added float values for fractions.
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.
Changed in version 0.18: Added float values for fractions.
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.
The number of features to consider when looking for the best split:
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.
Controls the randomness of the estimator. The features are always randomly permuted at each split, even if splitter is set to "best". When max_features < n_features, the algorithm will select max_features at random at each split before finding the best split among them. But the best found split may vary across different runs, even if max_features=n_features. That is the case, if the improvement of the criterion is identical for several splits and one split has to be selected at random. To obtain a deterministic behaviour during fitting, random_state has to be fixed to an integer. See Glossary for details.
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.
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:
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.
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.
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.
Return the feature importances.
The inferred value of max_features.
The number of features when fit is performed.
The number of outputs when fit is performed.
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
A decision tree classifier.
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.
https://en.wikipedia.org/wiki/Decision_tree_learning
L. Breiman, J. Friedman, R. Olshen, and C. Stone, “Classification and Regression Trees”, Wadsworth, Belmont, CA, 1984.
T. Hastie, R. Tibshirani and J. Friedman. “Elements of Statistical Learning”, Springer, 2009.
L. Breiman, and A. Cutler, “Random Forests”, https://www.stat.berkeley.edu/~breiman/RandomForests/cc_home.htm
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.
Return the index of the leaf that each sample is predicted as.
New in version 0.17.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
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.
Compute the pruning path during Minimal Cost-Complexity Pruning.
See Minimal Cost-Complexity Pruning for details on the pruning process.
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.
The target values (class labels) as integers or strings.
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.
Dictionary-like object, with the following attributes.
Effective alphas of subtree during pruning.
Sum of the impurities of the subtree leaves for the corresponding alpha value in ccp_alphas.
Return the decision path in the tree.
New in version 0.18.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
Return a node indicator CSR matrix where non zero elements indicates that the samples goes through the nodes.
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.
Normalized total reduction of criteria by feature (Gini importance).
Build a decision tree regressor from the training set (X, y).
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.
The target values (real numbers). Use dtype=np.float64 and order='C' for maximum efficiency.
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.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
This parameter is deprecated and has no effect. It will be removed in 1.1 (renaming of 0.26).
Deprecated since version 0.24.
Fitted estimator.
Return the depth of the decision tree.
The depth of a tree is the maximum distance between the root and any leaf.
The maximum depth of the tree.
Return the number of leaves of the decision tree.
Number of leaves.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
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.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
The predicted classes, or the predict values.
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.
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.
True values for X.
Sample weights.
\(R^2\) of self.predict(X) wrt. y.
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 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.
Estimator parameters.
Estimator instance.
Release Highlights for scikit-learn 0.24
Release Highlights for scikit-learn 0.22
Decision Tree Regression
Multi-output Decision Tree Regression
Decision Tree Regression with AdaBoost
Single estimator versus bagging: bias-variance decomposition
Advanced Plotting With Partial Dependence
Imputing missing values with variants of IterativeImputer
Using KBinsDiscretizer to discretize continuous features


Build a decision tree regressor from the training set (X, y).
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.
The target values (real numbers). Use dtype=np.float64 and order='C' for maximum efficiency.
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.
Allow to bypass several input checking. Don’t use this parameter unless you know what you do.
This parameter is deprecated and has no effect. It will be removed in 1.1 (renaming of 0.26).
Deprecated since version 0.24.
Fitted estimator.


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.
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.
True values for X.
Sample weights.
\(R^2\) of self.predict(X) wrt. y.
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).


Export a decision tree in DOT format.
This function generates a GraphViz representation of the decision tree, which is then written into out_file. Once exported, graphical renderings can be generated using, for example:
The sample counts that are shown are weighted with any sample_weights that might be present.
Read more in the User Guide.
The decision tree to be exported to GraphViz.
Handle or name of the output file. If None, the result is returned as a string.
Changed in version 0.20: Default of out_file changed from “tree.dot” to None.
The maximum depth of the representation. If None, the tree is fully generated.
Names of each of the features. If None generic names will be used (“feature_0”, “feature_1”, …).
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.
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.
When set to True, paint nodes to indicate majority class for classification, extremity of values for regression, or purity of node for multi-output.
When set to True, draw all leaf nodes at the bottom of the tree.
When set to True, show the impurity at each node.
When set to True, show the ID number on each node.
When set to True, change the display of ‘values’ and/or ‘samples’ to be proportions and percentages respectively.
When set to True, orient tree left to right rather than top-down.
When set to True, draw node boxes with rounded corners and use Helvetica fonts instead of Times-Roman.
When set to False, ignore special characters for PostScript compatibility.
Number of digits of precision for floating point in the values of impurity, threshold and value attributes of each node.
String representation of the input tree in GraphViz dot format. Only returned if out_file is None.
New in version 0.18.