Mean Poisson deviance regression loss.
Poisson deviance is equivalent to the Tweedie deviance with the power parameter power=1.
Read more in the User Guide.
Ground truth (correct) target values. Requires y_true >= 0.
Estimated target values. Requires y_pred > 0.
Sample weights.
A non-negative floating point value (the best value is 0.0).
Poisson regression and non-normal loss


Mean squared error regression loss.
Read more in the User Guide.
Ground truth (correct) target values.
Estimated target values.
Sample weights.
Defines aggregating of multiple output values. Array-like value defines weights used to average errors.
Returns a full set of errors in case of multioutput input.
Errors of all outputs are averaged with uniform weight.
If True returns MSE value, if False returns RMSE value.
A non-negative floating point value (the best value is 0.0), or an array of floating point values, one for each individual target.
Gradient Boosting regression
Model Complexity Influence
Plot Ridge coefficients as a function of the L2 regularization
Linear Regression Example
Robust linear estimator fitting
Poisson regression and non-normal loss
Tweedie regression on insurance claims


Mean squared logarithmic error regression loss.
Read more in the User Guide.
Ground truth (correct) target values.
Estimated target values.
Sample weights.
Defines aggregating of multiple output values. Array-like value defines weights used to average errors.
Returns a full set of errors when the input is of multioutput format.
Errors of all outputs are averaged with uniform weight.
A non-negative floating point value (the best value is 0.0), or an array of floating point values, one for each individual target.


Mean Tweedie deviance regression loss.
Read more in the User Guide.
Ground truth (correct) target values.
Estimated target values.
Sample weights.
Tweedie power parameter. Either power <= 0 or power >= 1.
The higher p the less weight is given to extreme deviations between true and predicted targets.
A non-negative floating point value (the best value is 0.0).
Tweedie regression on insurance claims


Median absolute error regression loss.
Median absolute error output is non-negative floating point. The best value is 0.0. Read more in the User Guide.
Ground truth (correct) target values.
Estimated target values.
Defines aggregating of multiple output values. Array-like value defines weights used to average errors.
Returns a full set of errors in case of multioutput input.
Errors of all outputs are averaged with uniform weight.
Sample weights.
New in version 0.24.
If multioutput is ‘raw_values’, then mean absolute error is returned for each output separately. If multioutput is ‘uniform_average’ or an ndarray of weights, then the weighted average of all output errors is returned.
Common pitfalls in interpretation of coefficients of linear models
Effect of transforming the targets in regression model


Compute a confusion matrix for each class or sample.
New in version 0.21.
Compute class-wise (default) or sample-wise (samplewise=True) multilabel confusion matrix to evaluate the accuracy of a classification, and output confusion matrices for each class or sample.
In multilabel confusion matrix \(MCM\), the count of true negatives is \(MCM_{:,0,0}\), false negatives is \(MCM_{:,1,0}\), true positives is \(MCM_{:,1,1}\) and false positives is \(MCM_{:,0,1}\).
Multiclass data will be treated as if binarized under a one-vs-rest transformation. Returned confusion matrices will be in the order of sorted unique labels in the union of (y_true, y_pred).
Read more in the User Guide.
Ground truth (correct) target values.
Estimated targets as returned by a classifier.
Sample weights.
A list of classes or column indices to select some (or to force inclusion of classes absent from the data).
In the multilabel case, this calculates a confusion matrix per sample.
A 2x2 confusion matrix corresponding to each output in the input. When calculating class-wise multi_confusion (default), then n_outputs = n_labels; when calculating sample-wise multi_confusion (samplewise=True), n_outputs = n_samples. If labels is defined, the results will be returned in the order specified in labels, otherwise the results will be returned in sorted order by default.
See also
The multilabel_confusion_matrix calculates class-wise or sample-wise multilabel confusion matrices, and in multiclass tasks, labels are binarized under a one-vs-rest way; while confusion_matrix calculates one confusion matrix for confusion between every two classes.
Multilabel-indicator case:
Multiclass case:


Mutual Information between two clusterings.
The Mutual Information is a measure of the similarity between two labels of the same data. Where \(|U_i|\) is the number of the samples in cluster \(U_i\) and \(|V_j|\) is the number of the samples in cluster \(V_j\), the Mutual Information between clusterings \(U\) and \(V\) is given as:
This metric is independent of the absolute values of the labels: a permutation of the class or cluster label values won’t change the score value in any way.
This metric is furthermore symmetric: switching label_true with label_pred will return the same score value. This can be useful to measure the agreement of two independent label assignments strategies on the same dataset when the real ground truth is not known.
Read more in the User Guide.
A clustering of the data into disjoint subsets.
A clustering of the data into disjoint subsets.
A contingency matrix given by the contingency_matrix function. If value is None, it will be computed, otherwise the given value is used, with labels_true and labels_pred ignored.
Mutual information, a non-negative value
See also
Adjusted against chance Mutual Information.
Normalized Mutual Information.
The logarithm used is the natural logarithm (base-e).
Adjustment for chance in clustering performance evaluation


Compute Normalized Discounted Cumulative Gain.
Sum the true scores ranked in the order induced by the predicted scores, after applying a logarithmic discount. Then divide by the best possible score (Ideal DCG, obtained for a perfect ranking) to obtain a score between 0 and 1.
This ranking metric yields a high value if true labels are ranked high by y_score.
True targets of multilabel classification, or true scores of entities to be ranked.
Target scores, can either be probability estimates, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).
Only consider the highest k scores in the ranking. If None, use all outputs.
Sample weights. If None, all samples are given the same weight.
Assume that there are no ties in y_score (which is likely to be the case if y_score is continuous) for efficiency gains.
The averaged NDCG scores for all samples.
See also
Discounted Cumulative Gain (not normalized).
Wikipedia entry for Discounted Cumulative Gain
Jarvelin, K., & Kekalainen, J. (2002). Cumulated gain-based evaluation of IR techniques. ACM Transactions on Information Systems (TOIS), 20(4), 422-446.
Wang, Y., Wang, L., Li, Y., He, D., Chen, W., & Liu, T. Y. (2013, May). A theoretical analysis of NDCG ranking measures. In Proceedings of the 26th Annual Conference on Learning Theory (COLT 2013)
McSherry, F., & Najork, M. (2008, March). Computing information retrieval performance measures efficiently in the presence of tied scores. In European conference on information retrieval (pp. 414-421). Springer, Berlin, Heidelberg.


Normalized Mutual Information between two clusterings.
Normalized Mutual Information (NMI) is a normalization of the Mutual Information (MI) score to scale the results between 0 (no mutual information) and 1 (perfect correlation). In this function, mutual information is normalized by some generalized mean of H(labels_true) and H(labels_pred)), defined by the average_method.
This measure is not adjusted for chance. Therefore adjusted_mutual_info_score might be preferred.
This metric is independent of the absolute values of the labels: a permutation of the class or cluster label values won’t change the score value in any way.
This metric is furthermore symmetric: switching label_true with label_pred will return the same score value. This can be useful to measure the agreement of two independent label assignments strategies on the same dataset when the real ground truth is not known.
Read more in the User Guide.
A clustering of the data into disjoint subsets.
A clustering of the data into disjoint subsets.
How to compute the normalizer in the denominator. Possible options are ‘min’, ‘geometric’, ‘arithmetic’, and ‘max’.
New in version 0.20.
Changed in version 0.22: The default value of average_method changed from ‘geometric’ to ‘arithmetic’.
score between 0.0 and 1.0. 1.0 stands for perfectly complete labeling
See also
V-Measure (NMI with arithmetic mean option).
Adjusted Rand Index.
Adjusted Mutual Information (adjusted against chance).
Perfect labelings are both homogeneous and complete, hence have score 1.0:
If classes members are completely split across different clusters, the assignment is totally in-complete, hence the NMI is null:


Computes the additive chi-squared kernel between observations in X and Y.
The chi-squared kernel is computed between each pair of rows in X and Y. X and Y have to be non-negative. This kernel is most commonly applied to histograms.
The chi-squared kernel is given by:
It can be interpreted as a weighted difference per entry.
Read more in the User Guide.
See also
The exponentiated version of the kernel, which is usually preferable.
A Fourier approximation to this kernel.
As the negative of a distance, this kernel is only conditionally positive definite.


Computes the exponential chi-squared kernel X and Y.
The chi-squared kernel is computed between each pair of rows in X and Y. X and Y have to be non-negative. This kernel is most commonly applied to histograms.
The chi-squared kernel is given by:
It can be interpreted as a weighted difference per entry.
Read more in the User Guide.
Scaling parameter of the chi2 kernel.
See also
The additive version of this kernel.
A Fourier approximation to the additive version of this kernel.


Compute cosine distance between samples in X and Y.
Cosine distance is defined as 1.0 minus the cosine similarity.
Read more in the User Guide.
Matrix X.
Matrix Y.
See also
Dense matrices only.


Compute cosine similarity between samples in X and Y.
Cosine similarity, or the cosine kernel, computes similarity as the normalized dot product of X and Y:
K(X, Y) = <X, Y> / (||X||*||Y||)
On L2-normalized data, this function is equivalent to linear_kernel.
Read more in the User Guide.
Input data.
Input data. If None, the output will be the pairwise similarities between all samples in X.
Whether to return dense output even when the input is sparse. If False, the output is sparse if both input arrays are sparse.
New in version 0.17: parameter dense_output for dense output.


Valid metrics for pairwise_distances.
This function simply returns the valid pairwise distance metrics. It exists to allow for a description of the mapping for each of the valid strings.
The valid distance metrics, and the function they map to, are:
metric
Function
‘cityblock’
metrics.pairwise.manhattan_distances
‘cosine’
metrics.pairwise.cosine_distances
‘euclidean’
metrics.pairwise.euclidean_distances
‘haversine’
metrics.pairwise.haversine_distances
‘l1’
metrics.pairwise.manhattan_distances
‘l2’
metrics.pairwise.euclidean_distances
‘manhattan’
metrics.pairwise.manhattan_distances
‘nan_euclidean’
metrics.pairwise.nan_euclidean_distances
Read more in the User Guide.


Considering the rows of X (and Y=X) as vectors, compute the distance matrix between each pair of vectors.
For efficiency reasons, the euclidean distance between a pair of row vector x and y is computed as:
This formulation has two advantages over other ways of computing distances. First, it is computationally efficient when dealing with sparse data. Second, if one argument varies but the other remains unchanged, then dot(x, x) and/or dot(y, y) can be pre-computed.
However, this is not the most precise way of doing this computation, because this equation potentially suffers from “catastrophic cancellation”. Also, the distance matrix returned by this function may not be exactly symmetric as required by, e.g., scipy.spatial.distance functions.
Read more in the User Guide.
Pre-computed dot-products of vectors in Y (e.g., (Y**2).sum(axis=1)) May be ignored in some cases, see the note below.
Return squared Euclidean distances.
Pre-computed dot-products of vectors in X (e.g., (X**2).sum(axis=1)) May be ignored in some cases, see the note below.
See also
Distances betweens pairs of elements of X and Y.
To achieve better accuracy, X_norm_squared and Y_norm_squared may be unused if they are passed as float32.


Compute the Haversine distance between samples in X and Y.
The Haversine (or great circle) distance is the angular distance between two points on the surface of a sphere. The first coordinate of each point is assumed to be the latitude, the second is the longitude, given in radians. The dimension of the data must be 2.
As the Earth is nearly spherical, the haversine formula provides a good approximation of the distance between two points of the Earth surface, with a less than 1% error on average.
We want to calculate the distance between the Ezeiza Airport (Buenos Aires, Argentina) and the Charles de Gaulle Airport (Paris, France).


Valid metrics for pairwise_kernels.
This function simply returns the valid pairwise distance metrics. It exists, however, to allow for a verbose description of the mapping for each of the valid strings.
metric
Function
‘additive_chi2’
sklearn.pairwise.additive_chi2_kernel
‘chi2’
sklearn.pairwise.chi2_kernel
‘linear’
sklearn.pairwise.linear_kernel
‘poly’
sklearn.pairwise.polynomial_kernel
‘polynomial’
sklearn.pairwise.polynomial_kernel
‘rbf’
sklearn.pairwise.rbf_kernel
‘laplacian’
sklearn.pairwise.laplacian_kernel
‘sigmoid’
sklearn.pairwise.sigmoid_kernel
‘cosine’
sklearn.pairwise.cosine_similarity
Read more in the User Guide.


Compute the laplacian kernel between X and Y.
The laplacian kernel is defined as:
for each pair of rows x in X and y in Y. Read more in the User Guide.
New in version 0.17.
If None, defaults to 1.0 / n_features.


Compute the linear kernel between X and Y.
Read more in the User Guide.
Whether to return dense output even when the input is sparse. If False, the output is sparse if both input arrays are sparse.
New in version 0.20.


Compute the L1 distances between the vectors in X and Y.
With sum_over_features equal to False it returns the componentwise distances.
Read more in the User Guide.
If True the function returns the pairwise distance matrix else it returns the componentwise L1 pairwise-distances. Not supported for sparse matrix inputs.
If sum_over_features is False shape is (n_samples_X * n_samples_Y, n_features) and D contains the componentwise L1 pairwise-distances (ie. absolute difference), else shape is (n_samples_X, n_samples_Y) and D contains the pairwise L1 distances.
When X and/or Y are CSR sparse matrices and they are not already in canonical format, this function modifies them in-place to make them canonical.


Calculate the euclidean distances in the presence of missing values.
Compute the euclidean distance between each pair of samples in X and Y, where Y=X is assumed if Y=None. When calculating the distance between a pair of samples, this formulation ignores feature coordinates with a missing value in either sample and scales up the weight of the remaining coordinates:
dist(x,y) = sqrt(weight * sq. distance from present coordinates) where, weight = Total # of coordinates / # of present coordinates
For example, the distance between [3, na, na, 6] and [1, na, 4, 5] is:
If all the coordinates are missing or if there are no common present coordinates then NaN is returned for that pair.
Read more in the User Guide.
New in version 0.22.
Return squared Euclidean distances.
Representation of missing value.
Make and use a deep copy of X and Y (if Y exists).
See also
Distances between pairs of elements of X and Y.


Computes the paired cosine distances between X and Y.
Read more in the User Guide.
The cosine distance is equivalent to the half the squared euclidean distance if each sample is normalized to unit norm.


Computes the paired distances between X and Y.
Computes the distances between (X[0], Y[0]), (X[1], Y[1]), etc…
Read more in the User Guide.
Array 1 for distance computation.
Array 2 for distance computation.
The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options specified in PAIRED_DISTANCES, including “euclidean”, “manhattan”, or “cosine”. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them.
See also
Computes the distance between every pair of samples.


Computes the paired euclidean distances between X and Y.
Read more in the User Guide.


Compute the L1 distances between the vectors in X and Y.
Read more in the User Guide.


Compute the kernel between arrays X and optional array Y.
This method takes either a vector array or a kernel matrix, and returns a kernel matrix. If the input is a vector array, the kernels are computed. If the input is a kernel matrix, it is returned instead.
This method provides a safe way to take a kernel matrix as input, while preserving compatibility with many other algorithms that take a vector array.
If Y is given (default is None), then the returned matrix is the pairwise kernel between the arrays from both X and Y.
[‘additive_chi2’, ‘chi2’, ‘linear’, ‘poly’, ‘polynomial’, ‘rbf’, ‘laplacian’, ‘sigmoid’, ‘cosine’]
Read more in the User Guide.
Array of pairwise kernels between samples, or a feature array. The shape of the array should be (n_samples_X, n_samples_X) if metric == “precomputed” and (n_samples_X, n_features) otherwise.
A second feature array only if X has shape (n_samples_X, n_features).
The metric to use when calculating kernel between instances in a feature array. If metric is a string, it must be one of the metrics in pairwise.PAIRWISE_KERNEL_FUNCTIONS. If metric is “precomputed”, X is assumed to be a kernel matrix. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two rows from X as input and return the corresponding kernel value as a single number. This means that callables from sklearn.metrics.pairwise are not allowed, as they operate on matrices, not single samples. Use the string identifying the kernel instead.
Whether to filter invalid parameters or not.
The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel.
None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
Any further parameters are passed directly to the kernel function.
A kernel matrix K such that K_{i, j} is the kernel between the ith and jth vectors of the given matrix X, if Y is None. If Y is not None, then K_{i, j} is the kernel between the ith array from X and the jth array from Y.
If metric is ‘precomputed’, Y is ignored and X is returned.


Compute the polynomial kernel between X and Y:
Read more in the User Guide.
If None, defaults to 1.0 / n_features.


Compute the rbf (gaussian) kernel between X and Y:
for each pair of rows x in X and y in Y.
Read more in the User Guide.
If None, defaults to 1.0 / n_features.


Compute the sigmoid kernel between X and Y:
Read more in the User Guide.
If None, defaults to 1.0 / n_features.


Compute the distance matrix from a vector array X and optional Y.
This method takes either a vector array or a distance matrix, and returns a distance matrix. If the input is a vector array, the distances are computed. If the input is a distances matrix, it is returned instead.
This method provides a safe way to take a distance matrix as input, while preserving compatibility with many other algorithms that take a vector array.
If Y is given (default is None), then the returned matrix is the pairwise distance between the arrays from both X and Y.
Valid values for metric are:
Note that in the case of ‘cityblock’, ‘cosine’ and ‘euclidean’ (which are valid scipy.spatial.distance metrics), the scikit-learn implementation will be used, which is faster and has support for sparse matrices (except for ‘cityblock’). For a verbose description of the metrics from scikit-learn, see the __doc__ of the sklearn.pairwise.distance_metrics function.
Read more in the User Guide.
Array of pairwise distances between samples, or a feature array. The shape of the array should be (n_samples_X, n_samples_X) if metric == “precomputed” and (n_samples_X, n_features) otherwise.
An optional second feature array. Only allowed if metric != “precomputed”.
The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options allowed by scipy.spatial.distance.pdist for its metric parameter, or a metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS. If metric is “precomputed”, X is assumed to be a distance matrix. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them.
The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel.
None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
Whether to raise an error on np.inf, np.nan, pd.NA in array. Ignored for a metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS. The possibilities are:
New in version 0.22: force_all_finite accepts the string 'allow-nan'.
Changed in version 0.23: Accepts pd.NA and converts it into np.nan.
Any further parameters are passed directly to the distance function. If using a scipy.spatial.distance metric, the parameters are still metric dependent. See the scipy docs for usage examples.
A distance matrix D such that D_{i, j} is the distance between the ith and jth vectors of the given matrix X, if Y is None. If Y is not None, then D_{i, j} is the distance between the ith array from X and the jth array from Y.
See also
Performs the same calculation as this function, but returns a generator of chunks of the distance matrix, in order to limit memory usage.
Computes the distances between corresponding elements of two arrays.
Agglomerative clustering with different metrics


Compute minimum distances between one point and a set of points.
This function computes for each row in X, the index of the row of Y which is closest (according to the specified distance).
This is mostly equivalent to calling:
pairwise_distances(X, Y=Y, metric=metric).argmin(axis=axis)
but uses much less memory, and is faster for large arrays.
This function works with dense 2D arrays only.
Array containing points.
Arrays containing points.
Axis along which the argmin and distances are to be computed.
Metric to use for distance computation. Any metric from scikit-learn or scipy.spatial.distance can be used.
If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string.
Distance matrices are not supported.
Valid values for metric are:
See the documentation for scipy.spatial.distance for details on these metrics.
Keyword arguments to pass to specified metric function.
Y[argmin[i], :] is the row in Y that is closest to X[i, :].
See also
Color Quantization using K-Means
Comparison of the K-Means and MiniBatchKMeans clustering algorithms


Compute minimum distances between one point and a set of points.
This function computes for each row in X, the index of the row of Y which is closest (according to the specified distance). The minimal distances are also returned.
This is mostly equivalent to calling:
pairwise_distances(X, Y=Y, metric=metric).min(axis=axis))
but uses much less memory, and is faster for large arrays.
Array containing points.
Array containing points.
Axis along which the argmin and distances are to be computed.
Metric to use for distance computation. Any metric from scikit-learn or scipy.spatial.distance can be used.
If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string.
Distance matrices are not supported.
Valid values for metric are:
See the documentation for scipy.spatial.distance for details on these metrics.
Keyword arguments to pass to specified metric function.
Y[argmin[i], :] is the row in Y that is closest to X[i, :].
distances[i] is the distance between the i-th row in X and the argmin[i]-th row in Y.
See also


Generate a distance matrix chunk by chunk with optional reduction.
In cases where not all of a pairwise distance matrix needs to be stored at once, this is used to calculate pairwise distances in working_memory-sized chunks. If reduce_func is given, it is run on each chunk and its return values are concatenated into lists, arrays or sparse matrices.
Array of pairwise distances between samples, or a feature array. The shape the array should be (n_samples_X, n_samples_X) if metric=’precomputed’ and (n_samples_X, n_features) otherwise.
An optional second feature array. Only allowed if metric != “precomputed”.
The function which is applied on each chunk of the distance matrix, reducing it to needed values. reduce_func(D_chunk, start) is called repeatedly, where D_chunk is a contiguous vertical slice of the pairwise distance matrix, starting at row start. It should return one of: None; an array, a list, or a sparse matrix of length D_chunk.shape[0]; or a tuple of such objects. Returning None is useful for in-place operations, rather than reductions.
If None, pairwise_distances_chunked returns a generator of vertical chunks of the distance matrix.
The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options allowed by scipy.spatial.distance.pdist for its metric parameter, or a metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS. If metric is “precomputed”, X is assumed to be a distance matrix. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them.
The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel.
None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
The sought maximum memory for temporary distance matrix chunks. When None (default), the value of sklearn.get_config()['working_memory'] is used.
Any further parameters are passed directly to the distance function. If using a scipy.spatial.distance metric, the parameters are still metric dependent. See the scipy docs for usage examples.
A contiguous slice of distance matrix, optionally processed by reduce_func.
Without reduce_func:
Retrieve all neighbors and average distance within radius r:
Where r is defined per sample, we need to make use of start:
Force row-by-row generation by reducing working_memory:


Plot Confusion Matrix.
Read more in the User Guide.
Fitted classifier or a fitted Pipeline in which the last estimator is a classifier.
Input values.
Target values.
List of labels to index the matrix. This may be used to reorder or select a subset of labels. If None is given, those that appear at least once in y_true or y_pred are used in sorted order.
Sample weights.
Normalizes confusion matrix over the true (rows), predicted (columns) conditions or all the population. If None, confusion matrix will not be normalized.
Target names used for plotting. By default, labels will be used if it is defined, otherwise the unique labels of y_true and y_pred will be used.
Includes values in confusion matrix.
Rotation of xtick labels.
Format specification for values in confusion matrix. If None, the format specification is ‘d’ or ‘.2g’ whichever is shorter.
Colormap recognized by matplotlib.
Axes object to plot on. If None, a new figure and axes is created.
Whether or not to add a colorbar to the plot.
New in version 0.24.
See also
Compute Confusion Matrix to evaluate the accuracy of a classification.
Confusion Matrix visualization.
Recognizing hand-written digits
Confusion matrix


Plot detection error tradeoff (DET) curve.
Extra keyword arguments will be passed to matplotlib’s plot.
Read more in the User Guide.
New in version 0.24.
Fitted classifier or a fitted Pipeline in which the last estimator is a classifier.
Input values.
Target values.
Sample weights.
Specifies whether to use predict_proba or decision_function as the predicted target response. If set to ‘auto’, predict_proba is tried first and if it does not exist decision_function is tried next.
Name of DET curve for labeling. If None, use the name of the estimator.
Axes object to plot on. If None, a new figure and axes is created.
The label of the positive class. When pos_label=None, if y_true is in {-1, 1} or {0, 1}, pos_label is set to 1, otherwise an error will be raised.
Object that stores computed values.
See also
Compute error rates for different probability thresholds.
DET curve visualization.
Plot Receiver operating characteristic (ROC) curve.
Detection error tradeoff (DET) curve


Plot Precision Recall Curve for binary classifiers.
Extra keyword arguments will be passed to matplotlib’s plot.
Read more in the User Guide.
Fitted classifier or a fitted Pipeline in which the last estimator is a classifier.
Input values.
Binary target values.
Sample weights.
Specifies whether to use predict_proba or decision_function as the target response. If set to ‘auto’, predict_proba is tried first and if it does not exist decision_function is tried next.
Name for labeling curve. If None, the name of the estimator is used.
Axes object to plot on. If None, a new figure and axes is created.
The class considered as the positive class when computing the precision and recall metrics. By default, estimators.classes_[1] is considered as the positive class.
New in version 0.24.
Keyword arguments to be passed to matplotlib’s plot.
Object that stores computed values.
See also
Compute precision-recall pairs for different probability thresholds.
Precision Recall visualization.
Precision-Recall


Plot Receiver operating characteristic (ROC) curve.
Extra keyword arguments will be passed to matplotlib’s plot.
Read more in the User Guide.
Fitted classifier or a fitted Pipeline in which the last estimator is a classifier.
Input values.
Target values.
Sample weights.
Whether to drop some suboptimal thresholds which would not appear on a plotted ROC curve. This is useful in order to create lighter ROC curves.
Specifies whether to use predict_proba or decision_function as the target response. If set to ‘auto’, predict_proba is tried first and if it does not exist decision_function is tried next.
Name of ROC Curve for labeling. If None, use the name of the estimator.
Axes object to plot on. If None, a new figure and axes is created.
The class considered as the positive class when computing the roc auc metrics. By default, estimators.classes_[1] is considered as the positive class.
New in version 0.24.
Object that stores computed values.
See also
Compute Receiver operating characteristic (ROC) curve.
ROC Curve visualization.
Compute the area under the ROC curve.
Release Highlights for scikit-learn 0.22
ROC Curve with Visualization API
Detection error tradeoff (DET) curve
Receiver Operating Characteristic (ROC) with cross validation


Compute precision-recall pairs for different probability thresholds.
Note: this implementation is restricted to the binary classification task.
The precision is the ratio tp / (tp + fp) where tp is the number of true positives and fp the number of false positives. The precision is intuitively the ability of the classifier not to label as positive a sample that is negative.
The recall is the ratio tp / (tp + fn) where tp is the number of true positives and fn the number of false negatives. The recall is intuitively the ability of the classifier to find all the positive samples.
The last precision and recall values are 1. and 0. respectively and do not have a corresponding threshold. This ensures that the graph starts on the y axis.
Read more in the User Guide.
True binary labels. If labels are not either {-1, 1} or {0, 1}, then pos_label should be explicitly given.
Estimated probabilities or output of a decision function.
The label of the positive class. When pos_label=None, if y_true is in {-1, 1} or {0, 1}, pos_label is set to 1, otherwise an error will be raised.
Sample weights.
Precision values such that element i is the precision of predictions with score >= thresholds[i] and the last element is 1.
Decreasing recall values such that element i is the recall of predictions with score >= thresholds[i] and the last element is 0.
Increasing thresholds on the decision function used to compute precision and recall. n_thresholds <= len(np.unique(probas_pred)).
See also
Plot Precision Recall Curve for binary classifiers.
Precision Recall visualization.
Compute average precision from prediction scores.
Compute error rates for different probability thresholds.
Compute Receiver operating characteristic (ROC) curve.
Visualizations with Display Objects
Precision-Recall


Compute precision, recall, F-measure and support for each class.
The precision is the ratio tp / (tp + fp) where tp is the number of true positives and fp the number of false positives. The precision is intuitively the ability of the classifier not to label as positive a sample that is negative.
The recall is the ratio tp / (tp + fn) where tp is the number of true positives and fn the number of false negatives. The recall is intuitively the ability of the classifier to find all the positive samples.
The F-beta score can be interpreted as a weighted harmonic mean of the precision and recall, where an F-beta score reaches its best value at 1 and worst score at 0.
The F-beta score weights recall more than precision by a factor of beta. beta == 1.0 means recall and precision are equally important.
The support is the number of occurrences of each class in y_true.
If pos_label is None and in binary classification, this function returns the average precision, recall and F-measure if average is one of 'micro', 'macro', 'weighted' or 'samples'.
Read more in the User Guide.
Ground truth (correct) target values.
Estimated targets as returned by a classifier.
The strength of recall versus precision in the F-score.
The set of labels to include when average != 'binary', and their order if average is None. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order.
The class to report if average='binary' and the data is binary. If the data are multiclass or multilabel, this will be ignored; setting labels=[pos_label] and average != 'binary' will report scores for that label only.
If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data:
Only report results for the class specified by pos_label. This is applicable only if targets (y_{true,pred}) are binary.
Calculate metrics globally by counting the total true positives, false negatives and false positives.
Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters ‘macro’ to account for label imbalance; it can result in an F-score that is not between precision and recall.
Calculate metrics for each instance, and find their average (only meaningful for multilabel classification where this differs from accuracy_score).
This determines which warnings will be made in the case that this function is being used to return only one of its metrics.
Sample weights.
If set to “warn”, this acts as 0, but warnings are also raised.
The number of occurrences of each label in y_true.
When true positive + false positive == 0, precision is undefined. When true positive + false negative == 0, recall is undefined. In such cases, by default the metric will be set to 0, as will f-score, and UndefinedMetricWarning will be raised. This behavior can be modified with zero_division.
Wikipedia entry for the Precision and recall.
Wikipedia entry for the F1-score.
Discriminative Methods for Multi-labeled Classification Advances in Knowledge Discovery and Data Mining (2004), pp. 22-30 by Shantanu Godbole, Sunita Sarawagi.
It is possible to compute per-label precisions, recalls, F1-scores and supports instead of averaging:


Compute the precision.
The precision is the ratio tp / (tp + fp) where tp is the number of true positives and fp the number of false positives. The precision is intuitively the ability of the classifier not to label as positive a sample that is negative.
The best value is 1 and the worst value is 0.
Read more in the User Guide.
Ground truth (correct) target values.
Estimated targets as returned by a classifier.
The set of labels to include when average != 'binary', and their order if average is None. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order.
Changed in version 0.17: Parameter labels improved for multiclass problem.
The class to report if average='binary' and the data is binary. If the data are multiclass or multilabel, this will be ignored; setting labels=[pos_label] and average != 'binary' will report scores for that label only.
This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data:
Only report results for the class specified by pos_label. This is applicable only if targets (y_{true,pred}) are binary.
Calculate metrics globally by counting the total true positives, false negatives and false positives.
Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters ‘macro’ to account for label imbalance; it can result in an F-score that is not between precision and recall.
Calculate metrics for each instance, and find their average (only meaningful for multilabel classification where this differs from accuracy_score).
Sample weights.
Sets the value to return when there is a zero division. If set to “warn”, this acts as 0, but warnings are also raised.
(n_unique_labels,) Precision of the positive class in binary classification or weighted average of the precision of each class for the multiclass task.
See also
When true positive + false positive == 0, precision returns 0 and raises UndefinedMetricWarning. This behavior can be modified with zero_division.
Probability Calibration curves
Precision-Recall


R^2 (coefficient of determination) regression score function.
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.
Read more in the User Guide.
Ground truth (correct) target values.
Estimated target values.
Sample weights.
Defines aggregating of multiple output scores. Array-like value defines weights used to average scores. Default is “uniform_average”.
Returns a full set of scores in case of multioutput input.
Scores of all outputs are averaged with uniform weight.
Scores of all outputs are averaged, weighted by the variances of each individual output.
Changed in version 0.19: Default value of multioutput is ‘uniform_average’.
The R^2 score or ndarray of scores if ‘multioutput’ is ‘raw_values’.
This is not a symmetric function.
Unlike most other scores, R^2 score may be negative (it need not actually be the square of a quantity R).
This metric is not well-defined for single samples and will return a NaN value if n_samples is less than two.
Wikipedia entry on the Coefficient of determination
Non-negative least squares
Linear Regression Example
Lasso and Elastic Net for Sparse Signals
Effect of transforming the targets in regression model


Rand index.
The Rand Index computes a similarity measure between two clusterings by considering all pairs of samples and counting pairs that are assigned in the same or different clusters in the predicted and true clusterings.
The raw RI score is:
RI = (number of agreeing pairs) / (number of pairs)
Read more in the User Guide.
Ground truth class labels to be used as a reference.
Cluster labels to evaluate.
Similarity score between 0.0 and 1.0, inclusive, 1.0 stands for perfect match.
See also
Adjusted Rand Score
Adjusted Mutual Information
Perfectly matching labelings have a score of 1 even
Labelings that assign all classes members to the same clusters are complete but may not always be pure, hence penalized:


Compute the recall.
The recall is the ratio tp / (tp + fn) where tp is the number of true positives and fn the number of false negatives. The recall is intuitively the ability of the classifier to find all the positive samples.
The best value is 1 and the worst value is 0.
Read more in the User Guide.
Ground truth (correct) target values.
Estimated targets as returned by a classifier.
The set of labels to include when average != 'binary', and their order if average is None. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order.
Changed in version 0.17: Parameter labels improved for multiclass problem.
The class to report if average='binary' and the data is binary. If the data are multiclass or multilabel, this will be ignored; setting labels=[pos_label] and average != 'binary' will report scores for that label only.
This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data:
Only report results for the class specified by pos_label. This is applicable only if targets (y_{true,pred}) are binary.
Calculate metrics globally by counting the total true positives, false negatives and false positives.
Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters ‘macro’ to account for label imbalance; it can result in an F-score that is not between precision and recall.
Calculate metrics for each instance, and find their average (only meaningful for multilabel classification where this differs from accuracy_score).
Sample weights.
Sets the value to return when there is a zero division. If set to “warn”, this acts as 0, but warnings are also raised.
(n_unique_labels,) Recall of the positive class in binary classification or weighted average of the recall of each class for the multiclass task.
See also
When true positive + false negative == 0, recall returns 0 and raises UndefinedMetricWarning. This behavior can be modified with zero_division.
Probability Calibration curves
Precision-Recall


Compute Area Under the Receiver Operating Characteristic Curve (ROC AUC) from prediction scores.
Note: this implementation can be used with binary, multiclass and multilabel classification, but some restrictions apply (see Parameters).
Read more in the User Guide.
True labels or binary label indicators. The binary and multiclass cases expect labels with shape (n_samples,) while the multilabel case expects binary label indicators with shape (n_samples, n_classes).
Target scores.
If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data: Note: multiclass ROC AUC currently only handles the ‘macro’ and ‘weighted’ averages.
Calculate metrics globally by considering each element of the label indicator matrix as a label.
Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
Calculate metrics for each label, and find their average, weighted by support (the number of true instances for each label).
Calculate metrics for each instance, and find their average.
Will be ignored when y_true is binary.
Sample weights.
If not None, the standardized partial AUC [2] over the range [0, max_fpr] is returned. For the multiclass case, max_fpr, should be either equal to None or 1.0 as AUC ROC partial computation currently is not supported for multiclass.
Only used for multiclass targets. Determines the type of configuration to use. The default value raises an error, so either 'ovr' or 'ovo' must be passed explicitly.
Stands for One-vs-rest. Computes the AUC of each class against the rest [3] [4]. This treats the multiclass case in the same way as the multilabel case. Sensitive to class imbalance even when average == 'macro', because class imbalance affects the composition of each of the ‘rest’ groupings.
Stands for One-vs-one. Computes the average AUC of all possible pairwise combinations of classes [5]. Insensitive to class imbalance when average == 'macro'.
Only used for multiclass targets. List of labels that index the classes in y_score. If None, the numerical or lexicographical order of the labels in y_true is used.
See also
Area under the precision-recall curve.
Compute Receiver operating characteristic (ROC) curve.
Plot Receiver operating characteristic (ROC) curve.
Wikipedia entry for the Receiver operating characteristic
Analyzing a portion of the ROC curve. McClish, 1989
Provost, F., Domingos, P. (2000). Well-trained PETs: Improving probability estimation trees (Section 6.2), CeDER Working Paper #IS-00-04, Stern School of Business, New York University.
Fawcett, T. (2006). An introduction to ROC analysis. Pattern Recognition Letters, 27(8), 861-874.
Hand, D.J., Till, R.J. (2001). A Simple Generalisation of the Area Under the ROC Curve for Multiple Class Classification Problems. Machine Learning, 45(2), 171-186.
Binary case:
Multiclass case:
Multilabel case:
Release Highlights for scikit-learn 0.22
Receiver Operating Characteristic (ROC) with cross validation
Receiver Operating Characteristic (ROC)
Statistical comparison of models using grid search


Compute Receiver operating characteristic (ROC).
Note: this implementation is restricted to the binary classification task.
Read more in the User Guide.
True binary labels. If labels are not either {-1, 1} or {0, 1}, then pos_label should be explicitly given.
Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).
The label of the positive class. When pos_label=None, if y_true is in {-1, 1} or {0, 1}, pos_label is set to 1, otherwise an error will be raised.
Sample weights.
Whether to drop some suboptimal thresholds which would not appear on a plotted ROC curve. This is useful in order to create lighter ROC curves.
New in version 0.17: parameter drop_intermediate.
Increasing false positive rates such that element i is the false positive rate of predictions with score >= thresholds[i].
Increasing true positive rates such that element i is the true positive rate of predictions with score >= thresholds[i].
Decreasing thresholds on the decision function used to compute fpr and tpr. thresholds[0] represents no instances being predicted and is arbitrarily set to max(y_score) + 1.
See also
Plot Receiver operating characteristic (ROC) curve.
ROC Curve visualization.
Compute error rates for different probability thresholds.
Compute the area under the ROC curve.
Since the thresholds are sorted from low to high values, they are reversed upon returning them to ensure they correspond to both fpr and tpr, which are sorted in reversed order during their calculation.
Wikipedia entry for the Receiver operating characteristic
Fawcett T. An introduction to ROC analysis[J]. Pattern Recognition Letters, 2006, 27(8):861-874.
Feature transformations with ensembles of trees
Species distribution modeling
Visualizations with Display Objects
Detection error tradeoff (DET) curve
Receiver Operating Characteristic (ROC)


Compute the Silhouette Coefficient for each sample.
The Silhouette Coefficient is a measure of how well samples are clustered with samples that are similar to themselves. Clustering models with a high Silhouette Coefficient are said to be dense, where samples in the same cluster are similar to each other, and well separated, where samples in different clusters are not very similar to each other.
The Silhouette Coefficient is calculated using the mean intra-cluster distance (a) and the mean nearest-cluster distance (b) for each sample. The Silhouette Coefficient for a sample is (b - a) / max(a,
b). Note that Silhouette Coefficient is only defined if number of labels is 2 <= n_labels <= n_samples - 1.
This function returns the Silhouette Coefficient for each sample.
The best value is 1 and the worst value is -1. Values near 0 indicate overlapping clusters.
Read more in the User Guide.
An array of pairwise distances between samples, or a feature array.
Label values for each sample.
The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options allowed by sklearn.metrics.pairwise.pairwise_distances. If X is the distance array itself, use “precomputed” as the metric. Precomputed distance matrices must have 0 along the diagonal.
Any further parameters are passed directly to the distance function. If using a scipy.spatial.distance metric, the parameters are still metric dependent. See the scipy docs for usage examples.
Silhouette Coefficients for each sample.
Peter J. Rousseeuw (1987). “Silhouettes: a Graphical Aid to the Interpretation and Validation of Cluster Analysis”. Computational and Applied Mathematics 20: 53-65.
Wikipedia entry on the Silhouette Coefficient
Selecting the number of clusters with silhouette analysis on KMeans clustering


Compute the mean Silhouette Coefficient of all samples.
The Silhouette Coefficient is calculated using the mean intra-cluster distance (a) and the mean nearest-cluster distance (b) for each sample. The Silhouette Coefficient for a sample is (b - a) / max(a,
b). To clarify, b is the distance between a sample and the nearest cluster that the sample is not a part of. Note that Silhouette Coefficient is only defined if number of labels is 2 <= n_labels <= n_samples - 1.
This function returns the mean Silhouette Coefficient over all samples. To obtain the values for each sample, use silhouette_samples.
The best value is 1 and the worst value is -1. Values near 0 indicate overlapping clusters. Negative values generally indicate that a sample has been assigned to the wrong cluster, as a different cluster is more similar.
Read more in the User Guide.
An array of pairwise distances between samples, or a feature array.
Predicted labels for each sample.
The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options allowed by metrics.pairwise.pairwise_distances. If X is the distance array itself, use metric="precomputed".
The size of the sample to use when computing the Silhouette Coefficient on a random subset of the data. If sample_size is None, no sampling is used.
Determines random number generation for selecting a subset of samples. Used when sample_size is not None. Pass an int for reproducible results across multiple function calls. See Glossary.
Any further parameters are passed directly to the distance function. If using a scipy.spatial.distance metric, the parameters are still metric dependent. See the scipy docs for usage examples.
Mean Silhouette Coefficient for all samples.
Peter J. Rousseeuw (1987). “Silhouettes: a Graphical Aid to the Interpretation and Validation of Cluster Analysis”. Computational and Applied Mathematics 20: 53-65.
Wikipedia entry on the Silhouette Coefficient
Demo of affinity propagation clustering algorithm
Demo of DBSCAN clustering algorithm
A demo of K-Means clustering on the handwritten digits data
Selecting the number of clusters with silhouette analysis on KMeans clustering
Clustering text documents using k-means


Top-k Accuracy classification score.
This metric computes the number of times where the correct label is among the top k labels predicted (ranked by predicted scores). Note that the multilabel case isn’t covered here.
Read more in the User Guide
True labels.
Target scores. These can be either probability estimates or non-thresholded decision values (as returned by decision_function on some classifiers). The binary case expects scores with shape (n_samples,) while the multiclass case expects scores with shape (n_samples, n_classes). In the nulticlass case, the order of the class scores must correspond to the order of labels, if provided, or else to the numerical or lexicographical order of the labels in y_true.
Number of most likely outcomes considered to find the correct label.
If True, return the fraction of correctly classified samples. Otherwise, return the number of correctly classified samples.
Sample weights. If None, all samples are given the same weight.
Multiclass only. List of labels that index the classes in y_score. If None, the numerical or lexicographical order of the labels in y_true is used.
The top-k accuracy score. The best performance is 1 with normalize == True and the number of samples with normalize == False.
See also
In cases where two or more labels are assigned equal predicted scores, the labels with the highest indices will be chosen first. This might impact the result if the correct label falls after the threshold because of that.


V-measure cluster labeling given a ground truth.
This score is identical to normalized_mutual_info_score with the 'arithmetic' option for averaging.
The V-measure is the harmonic mean between homogeneity and completeness:
This metric is independent of the absolute values of the labels: a permutation of the class or cluster label values won’t change the score value in any way.
This metric is furthermore symmetric: switching label_true with label_pred will return the same score value. This can be useful to measure the agreement of two independent label assignments strategies on the same dataset when the real ground truth is not known.
Read more in the User Guide.
ground truth class labels to be used as a reference
cluster labels to evaluate
Ratio of weight attributed to homogeneity vs completeness. If beta is greater than 1, completeness is weighted more strongly in the calculation. If beta is less than 1, homogeneity is weighted more strongly.
score between 0.0 and 1.0. 1.0 stands for perfectly complete labeling
See also
Andrew Rosenberg and Julia Hirschberg, 2007. V-Measure: A conditional entropy-based external cluster evaluation measure
Perfect labelings are both homogeneous and complete, hence have score 1.0:
Labelings that assign all classes members to the same clusters are complete be not homogeneous, hence penalized:
Labelings that have pure clusters with members coming from the same classes are homogeneous but un-necessary splits harms completeness and thus penalize V-measure as well:
If classes members are completely split across different clusters, the assignment is totally incomplete, hence the V-Measure is null:
Clusters that include samples from totally different classes totally destroy the homogeneity of the labeling, hence:
Biclustering documents with the Spectral Co-clustering algorithm
Demo of affinity propagation clustering algorithm
Demo of DBSCAN clustering algorithm
Adjustment for chance in clustering performance evaluation
A demo of K-Means clustering on the handwritten digits data
Clustering text documents using k-means


Zero-one classification loss.
If normalize is True, return the fraction of misclassifications (float), else it returns the number of misclassifications (int). The best performance is 0.
Read more in the User Guide.
Ground truth (correct) labels.
Predicted labels, as returned by a classifier.
If False, return the number of misclassifications. Otherwise, return the fraction of misclassifications.
Sample weights.
If normalize == True, return the fraction of misclassifications (float), else it returns the number of misclassifications (int).
See also
In multilabel classification, the zero_one_loss function corresponds to the subset zero-one loss: for each sample, the entire set of labels must be correctly predicted, otherwise the loss for that sample is equal to one.
In the multilabel case with binary label indicators:
Discrete versus Real AdaBoost


Input checker utility for building a cross-validator
Determines the cross-validation splitting strategy. Possible inputs for cv are: - None, to use the default 5-fold cross validation, - integer, to specify the number of folds. - CV splitter, - An iterable yielding (train, test) splits as arrays of indices.
For integer/None inputs, if classifier is True and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used.
Refer User Guide for the various cross-validation strategies that can be used here.
Changed in version 0.22: cv default value changed from 3-fold to 5-fold.
The target variable for supervised learning problems.
Whether the task is a classification task, in which case stratified KFold will be used.
The return value is a cross-validator which generates the train/test splits via the split method.


Evaluate metric(s) by cross-validation and also record fit/score times.
Read more in the User Guide.
The object to use to fit the data.
The data to fit. Can be for example a list, or an array.
The target variable to try to predict in the case of supervised learning.
Group labels for the samples used while splitting the dataset into train/test set. Only used in conjunction with a “Group” cv instance (e.g., GroupKFold).
Strategy to evaluate the performance of the cross-validated model on the test set.
If scoring represents a single score, one can use:
If scoring reprents multiple scores, one can use:
See Specifying multiple metrics for evaluation for an example.
Determines the cross-validation splitting strategy. Possible inputs for cv are:
For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used.
Refer User Guide for the various cross-validation strategies that can be used here.
Changed in version 0.22: cv default value if None changed from 3-fold to 5-fold.
Number of jobs to run in parallel. Training the estimator and computing the score are parallelized over the cross-validation splits. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
The verbosity level.
Parameters to pass to the fit method of the estimator.
Controls the number of jobs that get dispatched during parallel execution. Reducing this number can be useful to avoid an explosion of memory consumption when more jobs get dispatched than CPUs can process. This parameter can be:
Whether to include train scores. Computing training scores is used to get insights on how different parameter settings impact the overfitting/underfitting trade-off. However computing the scores on the training set can be computationally expensive and is not strictly required to select the parameters that yield the best generalization performance.
New in version 0.19.
Changed in version 0.21: Default value was changed from True to False
Whether to return the estimators fitted on each split.
New in version 0.20.
Value to assign to the score if an error occurs in estimator fitting. If set to ‘raise’, the error is raised. If a numeric value is given, FitFailedWarning is raised.
New in version 0.20.
Array of scores of the estimator for each run of the cross validation.
A dict of arrays containing the score/time arrays for each scorer is returned. The possible keys for this dict are:
The score array for test scores on each cv split. Suffix _score in test_score changes to a specific metric like test_r2 or test_auc if there are multiple scoring metrics in the scoring parameter.
The score array for train scores on each cv split. Suffix _score in train_score changes to a specific metric like train_r2 or train_auc if there are multiple scoring metrics in the scoring parameter. This is available only if return_train_score parameter is True.
The time for fitting the estimator on the train set for each cv split.
The time for scoring the estimator on the test set for each cv split. (Note time for scoring on the train set is not included even if return_train_score is set to True
The estimator objects for each cv split. This is available only if return_estimator parameter is set to True.
See also
Run cross-validation for single metric evaluation.
Get predictions from each split of cross-validation for diagnostic purposes.
Make a scorer from a performance metric or loss function.
Single metric evaluation using cross_validate
Multiple metric evaluation using cross_validate (please refer the scoring parameter doc for more information)
Categorical Feature Support in Gradient Boosting
Combine predictors using stacking
Common pitfalls in interpretation of coefficients of linear models


Generate cross-validated estimates for each input data point
The data is split according to the cv parameter. Each sample belongs to exactly one test set, and its prediction is computed with an estimator fitted on the corresponding training set.
Passing these predictions into an evaluation metric may not be a valid way to measure generalization performance. Results can differ from cross_validate and cross_val_score unless all tests sets have equal size and the metric decomposes over samples.
Read more in the User Guide.
The object to use to fit the data.
The data to fit. Can be, for example a list, or an array at least 2d.
The target variable to try to predict in the case of supervised learning.
Group labels for the samples used while splitting the dataset into train/test set. Only used in conjunction with a “Group” cv instance (e.g., GroupKFold).
Determines the cross-validation splitting strategy. Possible inputs for cv are:
For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used.
Refer User Guide for the various cross-validation strategies that can be used here.
Changed in version 0.22: cv default value if None changed from 3-fold to 5-fold.
Number of jobs to run in parallel. Training the estimator and predicting are parallelized over the cross-validation splits. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
The verbosity level.
Parameters to pass to the fit method of the estimator.
Controls the number of jobs that get dispatched during parallel execution. Reducing this number can be useful to avoid an explosion of memory consumption when more jobs get dispatched than CPUs can process. This parameter can be:
The method to be invoked by estimator.
This is the result of calling method. Shape:
See also
Calculate score for each CV split.
Calculate one or more scores and timings for each CV split.
In the case that one or more classes are absent in a training portion, a default score needs to be assigned to all instances for that class if method produces columns per class, as in {‘decision_function’, ‘predict_proba’, ‘predict_log_proba’}. For predict_proba this value is 0. In order to ensure finite output, we approximate negative infinity by the minimum finite float value for the dtype in other cases.
Combine predictors using stacking
Plotting Cross-Validated Predictions


Evaluate a score by cross-validation
Read more in the User Guide.
The object to use to fit the data.
The data to fit. Can be for example a list, or an array.
The target variable to try to predict in the case of supervised learning.
Group labels for the samples used while splitting the dataset into train/test set. Only used in conjunction with a “Group” cv instance (e.g., GroupKFold).
A str (see model evaluation documentation) or a scorer callable object / function with signature scorer(estimator, X, y) which should return only a single value.
Similar to cross_validate but only a single metric is permitted.
If None, the estimator’s default scorer (if available) is used.
Determines the cross-validation splitting strategy. Possible inputs for cv are:
For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used.
Refer User Guide for the various cross-validation strategies that can be used here.
Changed in version 0.22: cv default value if None changed from 3-fold to 5-fold.
Number of jobs to run in parallel. Training the estimator and computing the score are parallelized over the cross-validation splits. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
The verbosity level.
Parameters to pass to the fit method of the estimator.
Controls the number of jobs that get dispatched during parallel execution. Reducing this number can be useful to avoid an explosion of memory consumption when more jobs get dispatched than CPUs can process. This parameter can be:
Value to assign to the score if an error occurs in estimator fitting. If set to ‘raise’, the error is raised. If a numeric value is given, FitFailedWarning is raised.
New in version 0.20.
Array of scores of the estimator for each run of the cross validation.
See also
To run cross-validation on multiple metrics and also to return train scores, fit times and score times.
Get predictions from each split of cross-validation for diagnostic purposes.
Make a scorer from a performance metric or loss function.
Model selection with Probabilistic PCA and Factor Analysis (FA)
Imputing missing values with variants of IterativeImputer
Imputing missing values before building an estimator
Underfitting vs. Overfitting
Receiver Operating Characteristic (ROC) with cross validation
Nested versus non-nested cross-validation
SVM-Anova: SVM with univariate feature selection
Cross-validation on Digits Dataset Exercise


Learning curve.
Determines cross-validated training and test scores for different training set sizes.
A cross-validation generator splits the whole dataset k times in training and test data. Subsets of the training set with varying sizes will be used to train the estimator and a score for each training subset size and the test set will be computed. Afterwards, the scores will be averaged over all k runs for each training subset size.
Read more in the User Guide.
An object of that type which is cloned for each validation.
Training vector, where n_samples is the number of samples and n_features is the number of features.
Target relative to X for classification or regression; None for unsupervised learning.
Group labels for the samples used while splitting the dataset into train/test set. Only used in conjunction with a “Group” cv instance (e.g., GroupKFold).
Relative or absolute numbers of training examples that will be used to generate the learning curve. If the dtype is float, it is regarded as a fraction of the maximum size of the training set (that is determined by the selected validation method), i.e. it has to be within (0, 1]. Otherwise it is interpreted as absolute sizes of the training sets. Note that for classification the number of samples usually have to be big enough to contain at least one sample from each class.
Determines the cross-validation splitting strategy. Possible inputs for cv are:
For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used.
Refer User Guide for the various cross-validation strategies that can be used here.
Changed in version 0.22: cv default value if None changed from 3-fold to 5-fold.
A str (see model evaluation documentation) or a scorer callable object / function with signature scorer(estimator, X, y).
If the estimator supports incremental learning, this will be used to speed up fitting for different training set sizes.
Number of jobs to run in parallel. Training the estimator and computing the score are parallelized over the different training and test sets. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
Number of predispatched jobs for parallel execution (default is all). The option can reduce the allocated memory. The str can be an expression like ‘2*n_jobs’.
Controls the verbosity: the higher, the more messages.
Whether to shuffle training data before taking prefixes of it based on``train_sizes``.
Used when shuffle is True. Pass an int for reproducible output across multiple function calls. See Glossary.
Value to assign to the score if an error occurs in estimator fitting. If set to ‘raise’, the error is raised. If a numeric value is given, FitFailedWarning is raised.
New in version 0.20.
Whether to return the fit and score times.
Parameters to pass to the fit method of the estimator.
New in version 0.24.
Numbers of training examples that has been used to generate the learning curve. Note that the number of ticks might be less than n_ticks because duplicate entries will be removed.
Scores on training sets.
Scores on test set.
Times spent for fitting in seconds. Only present if return_times is True.
Times spent for scoring in seconds. Only present if return_times is True.
See examples/model_selection/plot_learning_curve.py
Comparison of kernel ridge regression and SVR
Plotting Learning Curves


Evaluate the significance of a cross-validated score with permutations
Permutes targets to generate ‘randomized data’ and compute the empirical p-value against the null hypothesis that features and targets are independent.
The p-value represents the fraction of randomized data sets where the estimator performed as well or better than in the original data. A small p-value suggests that there is a real dependency between features and targets which has been used by the estimator to give good predictions. A large p-value may be due to lack of real dependency between features and targets or the estimator was not able to use the dependency to give good predictions.
Read more in the User Guide.
The object to use to fit the data.
The data to fit.
The target variable to try to predict in the case of supervised learning.
Labels to constrain permutation within groups, i.e. y values are permuted among samples with the same group identifier. When not specified, y values are permuted among all samples.
When a grouped cross-validator is used, the group labels are also passed on to the split method of the cross-validator. The cross-validator uses them for grouping the samples while splitting the dataset into train/test set.
A single str (see The scoring parameter: defining model evaluation rules) or a callable (see Defining your scoring strategy from metric functions) to evaluate the predictions on the test set.
If None the estimator’s score method is used.
Determines the cross-validation splitting strategy. Possible inputs for cv are:
For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used.
Refer User Guide for the various cross-validation strategies that can be used here.
Changed in version 0.22: cv default value if None changed from 3-fold to 5-fold.
Number of times to permute y.
Number of jobs to run in parallel. Training the estimator and computing the cross-validated score are parallelized over the permutations. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
Pass an int for reproducible output for permutation of y values among samples. See Glossary.
The verbosity level.
Parameters to pass to the fit method of the estimator.
New in version 0.24.
The true score without permuting targets.
The scores obtained for each permutations.
The p-value, which approximates the probability that the score would be obtained by chance. This is calculated as:
(C + 1) / (n_permutations + 1)
Where C is the number of permutations whose score >= the true score.
The best possible p-value is 1/(n_permutations + 1), the worst is 1.0.
This function implements Test 1 in:
Ojala and Garriga. Permutation Tests for Studying Classifier Performance. The Journal of Machine Learning Research (2010) vol. 11
Test with permutations the significance of a classification score


Split arrays or matrices into random train and test subsets
Quick utility that wraps input validation and next(ShuffleSplit().split(X, y)) and application to input data into a single call for splitting (and optionally subsampling) data in a oneliner.
Read more in the User Guide.
Allowed inputs are lists, numpy arrays, scipy-sparse matrices or pandas dataframes.
If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test split. If int, represents the absolute number of test samples. If None, the value is set to the complement of the train size. If train_size is also None, it will be set to 0.25.
If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the train split. If int, represents the absolute number of train samples. If None, the value is automatically set to the complement of the test size.
Controls the shuffling applied to the data before applying the split. Pass an int for reproducible output across multiple function calls. See Glossary.
Whether or not to shuffle the data before splitting. If shuffle=False then stratify must be None.
If not None, data is split in a stratified fashion, using this as the class labels. Read more in the User Guide.
List containing train-test split of inputs.
New in version 0.16: If the input is sparse, the output will be a scipy.sparse.csr_matrix. Else, output type is the same as the input type.
Release Highlights for scikit-learn 0.23
Release Highlights for scikit-learn 0.24
Release Highlights for scikit-learn 0.22
Probability Calibration curves
Probability calibration of classifiers
Recognizing hand-written digits
Classifier comparison
Principal Component Regression vs Partial Least Squares Regression
Post pruning decision trees with cost complexity pruning
Understanding the decision tree structure
Comparing random forests and the multi-output meta estimator
Gradient Boosting regression
Early stopping of Gradient Boosting
Feature transformations with ensembles of trees
Gradient Boosting Out-of-Bag estimates
Faces recognition example using eigenfaces and SVMs
Prediction Latency
Pipeline Anova SVM
Univariate Feature Selection
Non-negative least squares
Comparing various online solvers
MNIST classification using multinomial logistic + L1
Multiclass sparse logistic regression on 20newgroups
Early stopping of Stochastic Gradient Descent
Poisson regression and non-normal loss
Tweedie regression on insurance claims
Permutation Importance with Multicollinear or Correlated Features
Permutation Importance vs Random Forest Feature Importance (MDI)
Partial Dependence and Individual Conditional Expectation Plots
Common pitfalls in interpretation of coefficients of linear models
Scalable learning with polynomial kernel aproximation
ROC Curve with Visualization API
Visualizations with Display Objects
Confusion matrix
Detection error tradeoff (DET) curve
Parameter estimation using grid search with cross-validation
Receiver Operating Characteristic (ROC)
Precision-Recall
Classifier Chain
Comparing Nearest Neighbors with and without Neighborhood Components Analysis
Dimensionality Reduction with Neighborhood Components Analysis
Restricted Boltzmann Machine features for digit classification
Varying regularization in Multi-layer Perceptron
Column Transformer with Mixed Types
Effect of transforming the targets in regression model
Importance of Feature Scaling
Map data to a normal distribution
Feature discretization
Semi-supervised Classification on a Text Dataset


Validation curve.
Determine training and test scores for varying parameter values.
Compute scores for an estimator with different values of a specified parameter. This is similar to grid search with one parameter. However, this will also compute training scores and is merely a utility for plotting the results.
Read more in the User Guide.
An object of that type which is cloned for each validation.
Training vector, where n_samples is the number of samples and n_features is the number of features.
Target relative to X for classification or regression; None for unsupervised learning.
Name of the parameter that will be varied.
The values of the parameter that will be evaluated.
Group labels for the samples used while splitting the dataset into train/test set. Only used in conjunction with a “Group” cv instance (e.g., GroupKFold).
Determines the cross-validation splitting strategy. Possible inputs for cv are:
For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used.
Refer User Guide for the various cross-validation strategies that can be used here.
Changed in version 0.22: cv default value if None changed from 3-fold to 5-fold.
A str (see model evaluation documentation) or a scorer callable object / function with signature scorer(estimator, X, y).
Number of jobs to run in parallel. Training the estimator and computing the score are parallelized over the combinations of each parameter value and each cross-validation split. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
Number of predispatched jobs for parallel execution (default is all). The option can reduce the allocated memory. The str can be an expression like ‘2*n_jobs’.
Controls the verbosity: the higher, the more messages.
Parameters to pass to the fit method of the estimator.
New in version 0.24.
Value to assign to the score if an error occurs in estimator fitting. If set to ‘raise’, the error is raised. If a numeric value is given, FitFailedWarning is raised.
New in version 0.20.
Scores on training sets.
Scores on test set.
See Plotting Validation Curves
Plotting Validation Curves


Computes the (weighted) graph of k-Neighbors for points in X
Read more in the User Guide.
Sample data, in the form of a numpy array or a precomputed BallTree.
Number of neighbors for each sample.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, and ‘distance’ will return the distances between neighbors according to the given metric.
The distance metric used to calculate the k-Neighbors for each sample point. The DistanceMetric class gives a list of available metrics. The default distance is ‘euclidean’ (‘minkowski’ metric with the p param equal to 2.)
Power parameter for the Minkowski metric. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.
additional keyword arguments for the metric function.
Whether or not to mark each sample as the first nearest neighbor to itself. If ‘auto’, then True is used for mode=’connectivity’ and False for mode=’distance’.
The number of parallel jobs to run for neighbors search. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
Graph where A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.
See also
Agglomerative clustering with and without structure
Hierarchical clustering: structured vs unstructured ward
Comparing different clustering algorithms on toy datasets


Computes the (weighted) graph of Neighbors for points in X
Neighborhoods are restricted the points at a distance lower than radius.
Read more in the User Guide.
Sample data, in the form of a numpy array or a precomputed BallTree.
Radius of neighborhoods.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, and ‘distance’ will return the distances between neighbors according to the given metric.
The distance metric used to calculate the neighbors within a given radius for each sample point. The DistanceMetric class gives a list of available metrics. The default distance is ‘euclidean’ (‘minkowski’ metric with the param equal to 2.)
Power parameter for the Minkowski metric. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.
additional keyword arguments for the metric function.
Whether or not to mark each sample as the first nearest neighbor to itself. If ‘auto’, then True is used for mode=’connectivity’ and False for mode=’distance’.
The number of parallel jobs to run for neighbors search. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
Graph where A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.
See also


Construct a Pipeline from the given estimators.
This is a shorthand for the Pipeline constructor; it does not require, and does not permit, naming the estimators. Instead, their names will be set to the lowercase of their types automatically.
Used to cache the fitted transformers of the pipeline. By default, no caching is performed. If a string is given, it is the path to the caching directory. Enabling caching triggers a clone of the transformers before fitting. Therefore, the transformer instance given to the pipeline cannot be inspected directly. Use the attribute named_steps or steps to inspect estimators within the pipeline. Caching the transformers is advantageous when fitting is time consuming.
If True, the time elapsed while fitting each step will be printed as it is completed.
See also
Class for creating a pipeline of transforms with a final estimator.
Release Highlights for scikit-learn 0.23
Release Highlights for scikit-learn 0.24
Release Highlights for scikit-learn 0.22
A demo of K-Means clustering on the handwritten digits data
Principal Component Regression vs Partial Least Squares Regression
Feature transformations with ensembles of trees
Categorical Feature Support in Gradient Boosting
Combine predictors using stacking
Pipeline Anova SVM
Univariate Feature Selection
Polynomial interpolation
Robust linear estimator fitting
Poisson regression and non-normal loss
Tweedie regression on insurance claims
Partial Dependence and Individual Conditional Expectation Plots
Common pitfalls in interpretation of coefficients of linear models
Scalable learning with polynomial kernel aproximation
Visualizations with Display Objects
Advanced Plotting With Partial Dependence
Imputing missing values with variants of IterativeImputer
Imputing missing values before building an estimator
Detection error tradeoff (DET) curve
Dimensionality Reduction with Neighborhood Components Analysis
Approximate nearest neighbors in TSNE
Varying regularization in Multi-layer Perceptron
Importance of Feature Scaling
Feature discretization
Clustering text documents using k-means


Construct a FeatureUnion from the given transformers.
This is a shorthand for the FeatureUnion constructor; it does not require, and does not permit, naming the transformers. Instead, they will be given names automatically based on their types. It also does not allow weighting.
Number of jobs to run in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
Changed in version v0.20: n_jobs default changed from 1 to None
If True, the time elapsed while fitting each transformer will be printed as it is completed.
See also
Class for concatenating the results of multiple transformer objects.


Augment dataset with an additional dummy feature.
This is useful for fitting an intercept term with implementations which cannot otherwise fit it directly.
Data.
Value to use for the dummy feature.
Same data with dummy feature added as first column.


Boolean thresholding of array-like or scipy.sparse matrix.
Read more in the User Guide.
The data to binarize, element by element. scipy.sparse matrices should be in CSR or CSC format to avoid an un-necessary copy.
Feature values below or equal to this are replaced by 0, above it by 1. Threshold may not be less than 0 for operations on sparse matrices.
set to False to perform inplace binarization and avoid a copy (if the input is already a numpy array or a scipy.sparse CSR / CSC matrix and if axis is 1).
The transformed data.
See also
Performs binarization using the Transformer API (e.g. as part of a preprocessing Pipeline).


Binarize labels in a one-vs-all fashion.
Several regression and binary classification algorithms are available in scikit-learn. A simple way to extend these algorithms to the multi-class classification case is to use the so-called one-vs-all scheme.
This function makes it possible to compute this transformation for a fixed set of class labels known ahead of time.
Sequence of integer labels or multilabel data to encode.
Uniquely holds the label for each class.
Value with which negative labels must be encoded.
Value with which positive labels must be encoded.
Set to true if output binary array is desired in CSR sparse format.
Shape will be (n_samples, 1) for binary problems. Sparse matrix will be of CSR format.
See also
Class used to wrap the functionality of label_binarize and allow for fitting to classes independently of the transform operation.
The class ordering is preserved:
Binary targets transform to a column vector
Receiver Operating Characteristic (ROC)
Precision-Recall


Scale each feature to the [-1, 1] range without breaking the sparsity.
This estimator scales each feature individually such that the maximal absolute value of each feature in the training set will be 1.0.
This scaler can also be applied to sparse CSR or CSC matrices.
The data.
axis used to scale along. If 0, independently scale each feature, otherwise (if 1) scale each sample.
Set to False to perform inplace scaling and avoid a copy (if the input is already a numpy array).
The transformed data.
Warning
Risk of data leak Do not use maxabs_scale unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using MaxAbsScaler within a Pipeline in order to prevent most risks of data leaking: pipe = make_pipeline(MaxAbsScaler(), LogisticRegression()).
See also
Performs scaling to the [-1, 1] range using the Transformer API (e.g. as part of a preprocessing Pipeline).
NaNs are treated as missing values: disregarded to compute the statistics, and maintained during the data transformation.
For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.


Transform features by scaling each feature to a given range.
This estimator scales and translates each feature individually such that it is in the given range on the training set, i.e. between zero and one.
The transformation is given by (when axis=0):
where min, max = feature_range.
The transformation is calculated as (when axis=0):
This transformation is often used as an alternative to zero mean, unit variance scaling.
Read more in the User Guide.
New in version 0.17: minmax_scale function interface to MinMaxScaler.
The data.
Desired range of transformed data.
Axis used to scale along. If 0, independently scale each feature, otherwise (if 1) scale each sample.
Set to False to perform inplace scaling and avoid a copy (if the input is already a numpy array).
The transformed data.
Warning
Risk of data leak Do not use minmax_scale unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using MinMaxScaler within a Pipeline in order to prevent most risks of data leaking: pipe = make_pipeline(MinMaxScaler(), LogisticRegression()).
See also
Performs scaling to a given range using the Transformer API (e.g. as part of a preprocessing Pipeline).
For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.
Compare the effect of different scalers on data with outliers


Scale input vectors individually to unit norm (vector length).
Read more in the User Guide.
The data to normalize, element by element. scipy.sparse matrices should be in CSR format to avoid an un-necessary copy.
The norm to use to normalize each non zero sample (or each non-zero feature if axis is 0).
axis used to normalize the data along. If 1, independently normalize each sample, otherwise (if 0) normalize each feature.
set to False to perform inplace row normalization and avoid a copy (if the input is already a numpy array or a scipy.sparse CSR matrix and if axis is 1).
whether to return the computed norms
Normalized input X.
An array of norms along given axis for X. When X is sparse, a NotImplementedError will be raised for norm ‘l1’ or ‘l2’.
See also
Performs normalization using the Transformer API (e.g. as part of a preprocessing Pipeline).
For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.


Power transforms are a family of parametric, monotonic transformations that are applied to make data more Gaussian-like. This is useful for modeling issues related to heteroscedasticity (non-constant variance), or other situations where normality is desired.
Currently, power_transform supports the Box-Cox transform and the Yeo-Johnson transform. The optimal parameter for stabilizing variance and minimizing skewness is estimated through maximum likelihood.
Box-Cox requires input data to be strictly positive, while Yeo-Johnson supports both positive or negative data.
By default, zero-mean, unit-variance normalization is applied to the transformed data.
Read more in the User Guide.
The data to be transformed using a power transformation.
The power transform method. Available methods are:
Changed in version 0.23: The default value of the method parameter changed from ‘box-cox’ to ‘yeo-johnson’ in 0.23.
Set to True to apply zero-mean, unit-variance normalization to the transformed output.
Set to False to perform inplace computation during transformation.
The transformed data.
See also
Equivalent transformation with the Transformer API (e.g. as part of a preprocessing Pipeline).
Maps data to a standard normal distribution with the parameter output_distribution='normal'.
NaNs are treated as missing values: disregarded in fit, and maintained in transform.
For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.
I.K. Yeo and R.A. Johnson, “A new family of power transformations to improve normality or symmetry.” Biometrika, 87(4), pp.954-959, (2000).
G.E.P. Box and D.R. Cox, “An Analysis of Transformations”, Journal of the Royal Statistical Society B, 26, 211-252 (1964).
Warning
Risk of data leak. Do not use power_transform unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using PowerTransformer within a Pipeline in order to prevent most risks of data leaking, e.g.: pipe = make_pipeline(PowerTransformer(),
LogisticRegression()).


Transform features using quantiles information.
This method transforms the features to follow a uniform or a normal distribution. Therefore, for a given feature, this transformation tends to spread out the most frequent values. It also reduces the impact of (marginal) outliers: this is therefore a robust preprocessing scheme.
The transformation is applied on each feature independently. First an estimate of the cumulative distribution function of a feature is used to map the original values to a uniform distribution. The obtained values are then mapped to the desired output distribution using the associated quantile function. Features values of new/unseen data that fall below or above the fitted range will be mapped to the bounds of the output distribution. Note that this transform is non-linear. It may distort linear correlations between variables measured at the same scale but renders variables measured at different scales more directly comparable.
Read more in the User Guide.
The data to transform.
Axis used to compute the means and standard deviations along. If 0, transform each feature, otherwise (if 1) transform each sample.
Number of quantiles to be computed. It corresponds to the number of landmarks used to discretize the cumulative distribution function. If n_quantiles is larger than the number of samples, n_quantiles is set to the number of samples as a larger number of quantiles does not give a better approximation of the cumulative distribution function estimator.
Marginal distribution for the transformed data. The choices are ‘uniform’ (default) or ‘normal’.
Only applies to sparse matrices. If True, the sparse entries of the matrix are discarded to compute the quantile statistics. If False, these entries are treated as zeros.
Maximum number of samples used to estimate the quantiles for computational efficiency. Note that the subsampling procedure may differ for value-identical sparse and dense matrices.
Determines random number generation for subsampling and smoothing noise. Please see subsample for more details. Pass an int for reproducible results across multiple function calls. See Glossary
Set to False to perform inplace transformation and avoid a copy (if the input is already a numpy array). If True, a copy of X is transformed, leaving the original X unchanged
The default value of copy changed from False to True in 0.23.
The transformed data.
See also
Performs quantile-based scaling using the Transformer API (e.g. as part of a preprocessing Pipeline).
Maps data to a normal distribution using a power transformation.
Performs standardization that is faster, but less robust to outliers.
Performs robust standardization that removes the influence of outliers but does not put outliers and inliers on the same scale.
NaNs are treated as missing values: disregarded in fit, and maintained in transform.
Warning
Risk of data leak
Do not use quantile_transform unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using QuantileTransformer within a Pipeline in order to prevent most risks of data leaking:pipe = make_pipeline(QuantileTransformer(),
LogisticRegression()).
For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.
Effect of transforming the targets in regression model


Standardize a dataset along any axis
Center to the median and component wise scale according to the interquartile range.
Read more in the User Guide.
The data to center and scale.
axis used to compute the medians and IQR along. If 0, independently scale each feature, otherwise (if 1) scale each sample.
If True, center the data before scaling.
If True, scale the data to unit variance (or equivalently, unit standard deviation).
default=(25.0, 75.0), == (1st quantile, 3rd quantile), == IQR Quantile range used to calculate scale_.
New in version 0.18.
set to False to perform inplace row normalization and avoid a copy (if the input is already a numpy array or a scipy.sparse CSR matrix and if axis is 1).
If True, scale data so that normally distributed features have a variance of 1. In general, if the difference between the x-values of q_max and q_min for a standard normal distribution is greater than 1, the dataset will be scaled down. If less than 1, the dataset will be scaled up.
New in version 0.24.
The transformed data.
See also
Performs centering and scaling using the Transformer API (e.g. as part of a preprocessing Pipeline).
This implementation will refuse to center scipy.sparse matrices since it would make them non-sparse and would potentially crash the program with memory exhaustion problems.
Instead the caller is expected to either set explicitly with_centering=False (in that case, only variance scaling will be performed on the features of the CSR matrix) or to call X.toarray() if he/she expects the materialized dense array to fit in memory.
To avoid memory copy the caller should pass a CSR matrix.
For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.
Warning
Risk of data leak
Do not use robust_scale unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using RobustScaler within a Pipeline in order to prevent most risks of data leaking: pipe = make_pipeline(RobustScaler(), LogisticRegression()).