Measure the similarity of two clusterings of a set of points.
New in version 0.18.
The Fowlkes-Mallows index (FMI) is defined as the geometric mean between of the precision and recall:
Where TP is the number of True Positive (i.e. the number of pair of points that belongs in the same clusters in both labels_true and labels_pred), FP is the number of False Positive (i.e. the number of pair of points that belongs in the same clusters in labels_true and not in labels_pred) and FN is the number of False Negative (i.e the number of pair of points that belongs in the same clusters in labels_pred and not in labels_True).
The score ranges from 0 to 1. A high value indicates a good similarity between two clusters.
Read more in the User Guide.
A clustering of the data into disjoint subsets.
A clustering of the data into disjoint subsets.
Compute contingency matrix internally with sparse matrix.
The resulting Fowlkes-Mallows score.
E. B. Fowkles and C. L. Mallows, 1983. “A method for comparing two hierarchical clusterings”. Journal of the American Statistical Association
Wikipedia entry for the Fowlkes-Mallows Index
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 random, hence the FMI is null:


Get a scorer from string.
Read more in the User Guide.
Scoring method as string. If callable it is returned as is.
The scorer.


Compute the average Hamming loss.
The Hamming loss is the fraction of labels that are incorrectly predicted.
Read more in the User Guide.
Ground truth (correct) labels.
Predicted labels, as returned by a classifier.
Sample weights.
New in version 0.18.
Return the average Hamming loss between element of y_true and y_pred.
See also
In multiclass classification, the Hamming loss corresponds to the Hamming distance between y_true and y_pred which is equivalent to the subset zero_one_loss function, when normalize parameter is set to True.
In multilabel classification, the Hamming loss is different from the subset zero-one loss. The zero-one loss considers the entire set of labels for a given sample incorrect if it does not entirely match the true set of labels. Hamming loss is more forgiving in that it penalizes only the individual labels.
The Hamming loss is upperbounded by the subset zero-one loss, when normalize parameter is set to True. It is always between 0 and 1, lower being better.
Grigorios Tsoumakas, Ioannis Katakis. Multi-Label Classification: An Overview. International Journal of Data Warehousing & Mining, 3(3), 1-13, July-September 2007.
Wikipedia entry on the Hamming distance.
In the multilabel case with binary label indicators:


Average hinge loss (non-regularized).
In binary class case, assuming labels in y_true are encoded with +1 and -1, when a prediction mistake is made, margin = y_true * pred_decision is always negative (since the signs disagree), implying 1 - margin is always greater than 1. The cumulated hinge loss is therefore an upper bound of the number of mistakes made by the classifier.
In multiclass case, the function expects that either all the labels are included in y_true or an optional labels argument is provided which contains all the labels. The multilabel margin is calculated according to Crammer-Singer’s method. As in the binary case, the cumulated hinge loss is an upper bound of the number of mistakes made by the classifier.
Read more in the User Guide.
True target, consisting of integers of two values. The positive label must be greater than the negative label.
Predicted decisions, as output by decision_function (floats).
Contains all the labels for the problem. Used in multiclass hinge loss.
Sample weights.
Wikipedia entry on the Hinge loss.
Koby Crammer, Yoram Singer. On the Algorithmic Implementation of Multiclass Kernel-based Vector Machines. Journal of Machine Learning Research 2, (2001), 265-292.
L1 AND L2 Regularization for Multiclass Hinge Loss Models by Robert C. Moore, John DeNero.
In the multiclass case:


Compute the homogeneity and completeness and V-Measure scores at once.
Those metrics are based on normalized conditional entropy measures of the clustering labeling to evaluate given the knowledge of a Ground Truth class labels of the same samples.
A clustering result satisfies homogeneity if all of its clusters contain only data points which are members of a single class.
A clustering result satisfies completeness if all the data points that are members of a given class are elements of the same cluster.
Both scores have positive values between 0.0 and 1.0, larger values being desirable.
Those 3 metrics are independent of the absolute values of the labels: a permutation of the class or cluster label values won’t change the score values in any way.
V-Measure is furthermore symmetric: swapping labels_true and label_pred will give the same score. This does not hold for homogeneity and completeness. V-Measure is identical to normalized_mutual_info_score with the arithmetic averaging method.
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 homogeneous labeling
score between 0.0 and 1.0. 1.0 stands for perfectly complete labeling
harmonic mean of the first two
See also


Homogeneity metric of a cluster labeling given a ground truth.
A clustering result satisfies homogeneity if all of its clusters contain only data points which are members of a single class.
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 not symmetric: switching label_true with label_pred will return the completeness_score which will be different in general.
Read more in the User Guide.
ground truth class labels to be used as a reference
cluster labels to evaluate
score between 0.0 and 1.0. 1.0 stands for perfectly homogeneous labeling
See also
Andrew Rosenberg and Julia Hirschberg, 2007. V-Measure: A conditional entropy-based external cluster evaluation measure
Perfect labelings are homogeneous:
Non-perfect labelings that further split classes into more clusters can be perfectly homogeneous:
Clusters that include samples from different classes do not make for an homogeneous labeling:


Jaccard similarity coefficient score.
The Jaccard index [1], or Jaccard similarity coefficient, defined as the size of the intersection divided by the size of the union of two label sets, is used to compare set of predicted labels for a sample to the corresponding set of labels in y_true.
Read more in the User Guide.
Ground truth (correct) labels.
Predicted labels, 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.
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.
Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).
Sample weights.
Sets the value to return when there is a zero division, i.e. when there there are no negative values in predictions and labels. If set to “warn”, this acts like 0, but a warning is also raised.
See also
jaccard_score may be a poor metric if there are no positives for some samples or classes. Jaccard is undefined if there are no true or predicted labels, and our implementation will return a score of 0 with a warning.
Wikipedia entry for the Jaccard index.
In the binary case:
In the multilabel case:
In the multiclass case:


Compute ranking-based average precision.
Label ranking average precision (LRAP) is the average over each ground truth label assigned to each sample, of the ratio of true vs. total labels with lower score.
This metric is used in multilabel ranking problem, where the goal is to give better rank to the labels associated to each sample.
The obtained score is always strictly greater than 0 and the best value is 1.
Read more in the User Guide.
True binary labels in binary indicator format.
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).
Sample weights.
New in version 0.20.


Compute Ranking loss measure.
Compute the average number of label pairs that are incorrectly ordered given y_score weighted by the size of the label set and the number of labels not in the label set.
This is similar to the error set size, but weighted by the number of relevant and irrelevant labels. The best performance is achieved with a ranking loss of zero.
Read more in the User Guide.
New in version 0.17: A function label_ranking_loss
True binary labels in binary indicator format.
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).
Sample weights.
Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010). Mining multi-label data. In Data mining and knowledge discovery handbook (pp. 667-685). Springer US.


Log loss, aka logistic loss or cross-entropy loss.
This is the loss function used in (multinomial) logistic regression and extensions of it such as neural networks, defined as the negative log-likelihood of a logistic model that returns y_pred probabilities for its training data y_true. The log loss is only defined for two or more labels. For a single sample with true label \(y \in \{0,1\}\) and and a probability estimate \(p = \operatorname{Pr}(y = 1)\), the log loss is:
Read more in the User Guide.
Ground truth (correct) labels for n_samples samples.
Predicted probabilities, as returned by a classifier’s predict_proba method. If y_pred.shape = (n_samples,) the probabilities provided are assumed to be that of the positive class. The labels in y_pred are assumed to be ordered alphabetically, as done by preprocessing.LabelBinarizer.
Log loss is undefined for p=0 or p=1, so probabilities are clipped to max(eps, min(1 - eps, p)).
If true, return the mean loss per sample. Otherwise, return the sum of the per-sample losses.
Sample weights.
If not provided, labels will be inferred from y_true. If labels is None and y_pred has shape (n_samples,) the labels are assumed to be binary and are inferred from y_true.
New in version 0.18.
The logarithm used is the natural logarithm (base-e).
C.M. Bishop (2006). Pattern Recognition and Machine Learning. Springer, p. 209.


Make a scorer from a performance metric or loss function.
This factory function wraps scoring functions for use in GridSearchCV and cross_val_score. It takes a score function, such as accuracy_score, mean_squared_error, adjusted_rand_index or average_precision and returns a callable that scores an estimator’s output. The signature of the call is (estimator, X, y) where estimator is the model to be evaluated, X is the data and y is the ground truth labeling (or None in the case of unsupervised models).
Read more in the User Guide.
Score function (or loss function) with signature score_func(y, y_pred, **kwargs).
Whether score_func is a score function (default), meaning high is good, or a loss function, meaning low is good. In the latter case, the scorer object will sign-flip the outcome of the score_func.
Whether score_func requires predict_proba to get probability estimates out of a classifier.
If True, for binary y_true, the score function is supposed to accept a 1D y_pred (i.e., probability of the positive class, shape (n_samples,)).
Whether score_func takes a continuous decision certainty. This only works for binary classification using estimators that have either a decision_function or predict_proba method.
If True, for binary y_true, the score function is supposed to accept a 1D y_pred (i.e., probability of the positive class or the decision function, shape (n_samples,)).
For example average_precision or the area under the roc curve can not be computed using discrete predictions alone.
Additional parameters to be passed to score_func.
Callable object that returns a scalar score; greater is better.
If needs_proba=False and needs_threshold=False, the score function is supposed to accept the output of predict. If needs_proba=True, the score function is supposed to accept the output of predict_proba (For binary y_true, the score function is supposed to accept probability of the positive class). If needs_threshold=True, the score function is supposed to accept the output of decision_function.


Compute the Matthews correlation coefficient (MCC).
The Matthews correlation coefficient is used in machine learning as a measure of the quality of binary and multiclass classifications. It takes into account true and false positives and negatives and is generally regarded as a balanced measure which can be used even if the classes are of very different sizes. The MCC is in essence a correlation coefficient value between -1 and +1. A coefficient of +1 represents a perfect prediction, 0 an average random prediction and -1 an inverse prediction. The statistic is also known as the phi coefficient. [source: Wikipedia]
Binary and multiclass labels are supported. Only in the binary case does this relate to information about true and false positives and negatives. See references below.
Read more in the User Guide.
Ground truth (correct) target values.
Estimated targets as returned by a classifier.
Sample weights.
New in version 0.18.
The Matthews correlation coefficient (+1 represents a perfect prediction, 0 an average random prediction and -1 and inverse prediction).
Baldi, Brunak, Chauvin, Andersen and Nielsen, (2000). Assessing the accuracy of prediction algorithms for classification: an overview.
Wikipedia entry for the Matthews Correlation Coefficient.
Gorodkin, (2004). Comparing two K-category assignments by a K-category correlation coefficient.
Jurman, Riccadonna, Furlanello, (2012). A Comparison of MCC and CEN Error Measures in MultiClass Prediction.


max_error metric calculates the maximum residual error.
Read more in the User Guide.
Ground truth (correct) target values.
Estimated target values.
A positive floating point value (the best value is 0.0).


Mean absolute 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 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.
MAE output is non-negative floating point. The best value is 0.0.


Mean absolute percentage error regression loss.
Note here that we do not represent the output as a percentage in range [0, 100]. Instead, we represent it in range [0, 1/eps]. Read more in the User Guide.
New in version 0.24.
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. If input is list then the shape must be (n_outputs,).
Returns a full set of errors in case of multioutput input.
Errors of all outputs are averaged with uniform weight.
If multioutput is ‘raw_values’, then mean absolute percentage 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.
MAPE output is non-negative floating point. The best value is 0.0. But note the fact that bad predictions can lead to arbitarily large MAPE values, especially if some y_true values are very close to zero. Note that we return a large value instead of inf when y_true is zero.


Mean Gamma deviance regression loss.
Gamma deviance is equivalent to the Tweedie deviance with the power parameter power=2. It is invariant to scaling of the target variable, and measures relative errors.
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).


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).


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.


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).


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.


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).


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.


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


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.


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.


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.


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.


Precision Recall visualization.
It is recommend to use plot_precision_recall_curve to create a visualizer. All parameters are stored as attributes.
Read more in the User Guide.
Precision values.
Recall values.
Average precision. If None, the average precision is not shown.
Name of estimator. If None, then the estimator name is not shown.
The class considered as the positive class. If None, the class will not be shown in the legend.
New in version 0.24.
Precision recall curve.
Axes with precision recall curve.
Figure containing the curve.
See also
Compute precision-recall pairs for different probability thresholds.
Plot Precision Recall Curve for binary classifiers.
plot([ax, name])
Plot visualization.
Plot visualization.
Extra keyword arguments will be passed to matplotlib’s plot.
Axes object to plot on. If None, a new figure and axes is created.
Name of precision recall curve for labeling. If None, use the name of the estimator.
Keyword arguments to be passed to matplotlib’s plot.
Object that stores computed values.


Precision Recall visualization.
It is recommend to use plot_precision_recall_curve to create a visualizer. All parameters are stored as attributes.
Read more in the User Guide.
Precision values.
Recall values.
Average precision. If None, the average precision is not shown.
Name of estimator. If None, then the estimator name is not shown.
The class considered as the positive class. If None, the class will not be shown in the legend.
New in version 0.24.
Precision recall curve.
Axes with precision recall curve.
Figure containing the curve.
See also
Compute precision-recall pairs for different probability thresholds.
Plot Precision Recall Curve for binary classifiers.
plot([ax, name])
Plot visualization.
Plot visualization.
Extra keyword arguments will be passed to matplotlib’s plot.
Axes object to plot on. If None, a new figure and axes is created.
Name of precision recall curve for labeling. If None, use the name of the estimator.
Keyword arguments to be passed to matplotlib’s plot.
Object that stores computed values.
Visualizations with Display Objects


Plot visualization.
Extra keyword arguments will be passed to matplotlib’s plot.
Axes object to plot on. If None, a new figure and axes is created.
Name of precision recall curve for labeling. If None, use the name of the estimator.
Keyword arguments to be passed to matplotlib’s plot.
Object that stores computed values.


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.


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.


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


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.


ROC Curve visualization.
It is recommend to use plot_roc_curve to create a visualizer. All parameters are stored as attributes.
Read more in the User Guide.
False positive rate.
True positive rate.
Area under ROC curve. If None, the roc_auc score is not shown.
Name of estimator. If None, the estimator name is not shown.
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.
ROC Curve.
Axes with ROC Curve.
Figure containing the curve.
See also
Compute Receiver operating characteristic (ROC) curve.
Plot Receiver operating characteristic (ROC) curve.
Compute the area under the ROC curve.
plot([ax, name])
Plot visualization
Plot visualization
Extra keyword arguments will be passed to matplotlib’s plot.
Axes object to plot on. If None, a new figure and axes is created.
Name of ROC Curve for labeling. If None, use the name of the estimator.
Object that stores computed values.


ROC Curve visualization.
It is recommend to use plot_roc_curve to create a visualizer. All parameters are stored as attributes.
Read more in the User Guide.
False positive rate.
True positive rate.
Area under ROC curve. If None, the roc_auc score is not shown.
Name of estimator. If None, the estimator name is not shown.
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.
ROC Curve.
Axes with ROC Curve.
Figure containing the curve.
See also
Compute Receiver operating characteristic (ROC) curve.
Plot Receiver operating characteristic (ROC) curve.
Compute the area under the ROC curve.
plot([ax, name])
Plot visualization
Plot visualization
Extra keyword arguments will be passed to matplotlib’s plot.
Axes object to plot on. If None, a new figure and axes is created.
Name of ROC Curve for labeling. If None, use the name of the estimator.
Object that stores computed values.
Visualizations with Display Objects


Plot visualization
Extra keyword arguments will be passed to matplotlib’s plot.
Axes object to plot on. If None, a new figure and axes is created.
Name of ROC Curve for labeling. If None, use the name of the estimator.
Object that stores computed values.


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:


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.


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


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


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:


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:


Variational Bayesian estimation of a Gaussian mixture.
This class allows to infer an approximate posterior distribution over the parameters of a Gaussian mixture distribution. The effective number of components can be inferred from the data.
This class implements two types of prior for the weights distribution: a finite mixture model with Dirichlet distribution and an infinite mixture model with the Dirichlet Process. In practice Dirichlet Process inference algorithm is approximated and uses a truncated distribution with a fixed maximum number of components (called the Stick-breaking representation). The number of components actually used almost always depends on the data.
New in version 0.18.
Read more in the User Guide.
The number of mixture components. Depending on the data and the value of the weight_concentration_prior the model can decide to not use all the components by setting some component weights_ to values very close to zero. The number of effective components is therefore smaller than n_components.
String describing the type of covariance parameters to use. Must be one of:
The convergence threshold. EM iterations will stop when the lower bound average gain on the likelihood (of the training data with respect to the model) is below this threshold.
Non-negative regularization added to the diagonal of covariance. Allows to assure that the covariance matrices are all positive.
The number of EM iterations to perform.
The number of initializations to perform. The result with the highest lower bound value on the likelihood is kept.
The method used to initialize the weights, the means and the covariances. Must be one of:
String describing the type of the weight concentration prior. Must be one of:
The dirichlet concentration of each component on the weight distribution (Dirichlet). This is commonly called gamma in the literature. The higher concentration puts more mass in the center and will lead to more components being active, while a lower concentration parameter will lead to more mass at the edge of the mixture weights simplex. The value of the parameter must be greater than 0. If it is None, it’s set to 1. / n_components.
The precision prior on the mean distribution (Gaussian). Controls the extent of where means can be placed. Larger values concentrate the cluster means around mean_prior. The value of the parameter must be greater than 0. If it is None, it is set to 1.
The prior on the mean distribution (Gaussian). If it is None, it is set to the mean of X.
The prior of the number of degrees of freedom on the covariance distributions (Wishart). If it is None, it’s set to n_features.
The prior on the covariance distribution (Wishart). If it is None, the emiprical covariance prior is initialized using the covariance of X. The shape depends on covariance_type:
Controls the random seed given to the method chosen to initialize the parameters (see init_params). In addition, it controls the generation of random samples from the fitted distribution (see the method sample). Pass an int for reproducible output across multiple function calls. See Glossary.
If ‘warm_start’ is True, the solution of the last fitting is used as initialization for the next call of fit(). This can speed up convergence when fit is called several times on similar problems. See the Glossary.
Enable verbose output. If 1 then it prints the current initialization and each iteration step. If greater than 1 then it prints also the log probability and the time needed for each step.
Number of iteration done before the next print.
The weights of each mixture components.
The mean of each mixture component.
The covariance of each mixture component. The shape depends on covariance_type:
The precision matrices for each component in the mixture. A precision matrix is the inverse of a covariance matrix. A covariance matrix is symmetric positive definite so the mixture of Gaussian can be equivalently parameterized by the precision matrices. Storing the precision matrices instead of the covariance matrices makes it more efficient to compute the log-likelihood of new samples at test time. The shape depends on covariance_type:
The cholesky decomposition of the precision matrices of each mixture component. A precision matrix is the inverse of a covariance matrix. A covariance matrix is symmetric positive definite so the mixture of Gaussian can be equivalently parameterized by the precision matrices. Storing the precision matrices instead of the covariance matrices makes it more efficient to compute the log-likelihood of new samples at test time. The shape depends on covariance_type:
True when convergence was reached in fit(), False otherwise.
Number of step used by the best fit of inference to reach the convergence.
Lower bound value on the likelihood (of the training data with respect to the model) of the best fit of inference.
The dirichlet concentration of each component on the weight distribution (Dirichlet). The type depends on weight_concentration_prior_type:
The higher concentration puts more mass in the center and will lead to more components being active, while a lower concentration parameter will lead to more mass at the edge of the simplex.
The dirichlet concentration of each component on the weight distribution (Dirichlet).
The precision prior on the mean distribution (Gaussian). Controls the extent of where means can be placed. Larger values concentrate the cluster means around mean_prior. If mean_precision_prior is set to None, mean_precision_prior_ is set to 1.
The precision of each components on the mean distribution (Gaussian).
The prior on the mean distribution (Gaussian).
The prior of the number of degrees of freedom on the covariance distributions (Wishart).
The number of degrees of freedom of each components in the model.
The prior on the covariance distribution (Wishart). The shape depends on covariance_type:
See also
Finite Gaussian mixture fit with EM.
Bishop, Christopher M. (2006). “Pattern recognition and machine learning”. Vol. 4 No. 4. New York: Springer.
Hagai Attias. (2000). “A Variational Bayesian Framework for Graphical Models”. In Advances in Neural Information Processing Systems 12.
Blei, David M. and Michael I. Jordan. (2006). “Variational inference for Dirichlet process mixtures”. Bayesian analysis 1.1
fit(X[, y])
Estimate model parameters with the EM algorithm.
fit_predict(X[, y])
Estimate model parameters using X and predict the labels for X.
get_params([deep])
Get parameters for this estimator.
predict(X)
Predict the labels for the data samples in X using trained model.
predict_proba(X)
Predict posterior probability of each component given the data.
sample([n_samples])
Generate random samples from the fitted Gaussian distribution.
score(X[, y])
Compute the per-sample average log-likelihood of the given data X.
score_samples(X)
Compute the weighted log probabilities for each sample.
set_params(**params)
Set the parameters of this estimator.
Estimate model parameters with the EM algorithm.
The method fits the model n_init times and sets the parameters with which the model has the largest likelihood or lower bound. Within each trial, the method iterates between E-step and M-step for max_iter times until the change of likelihood or lower bound is less than tol, otherwise, a ConvergenceWarning is raised. If warm_start is True, then n_init is ignored and a single initialization is performed upon the first call. Upon consecutive calls, training starts where it left off.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Estimate model parameters using X and predict the labels for X.
The method fits the model n_init times and sets the parameters with which the model has the largest likelihood or lower bound. Within each trial, the method iterates between E-step and M-step for max_iter times until the change of likelihood or lower bound is less than tol, otherwise, a ConvergenceWarning is raised. After fitting, it predicts the most probable label for the input data points.
New in version 0.20.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Component labels.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Predict the labels for the data samples in X using trained model.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Component labels.
Predict posterior probability of each component given the data.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Returns the probability each Gaussian (state) in the model given each sample.
Generate random samples from the fitted Gaussian distribution.
Number of samples to generate.
Randomly generated sample
Component labels
Compute the per-sample average log-likelihood of the given data X.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Log likelihood of the Gaussian mixture given X.
Compute the weighted log probabilities for each sample.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Log probabilities of each data point in X.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.


Variational Bayesian estimation of a Gaussian mixture.
This class allows to infer an approximate posterior distribution over the parameters of a Gaussian mixture distribution. The effective number of components can be inferred from the data.
This class implements two types of prior for the weights distribution: a finite mixture model with Dirichlet distribution and an infinite mixture model with the Dirichlet Process. In practice Dirichlet Process inference algorithm is approximated and uses a truncated distribution with a fixed maximum number of components (called the Stick-breaking representation). The number of components actually used almost always depends on the data.
New in version 0.18.
Read more in the User Guide.
The number of mixture components. Depending on the data and the value of the weight_concentration_prior the model can decide to not use all the components by setting some component weights_ to values very close to zero. The number of effective components is therefore smaller than n_components.
String describing the type of covariance parameters to use. Must be one of:
The convergence threshold. EM iterations will stop when the lower bound average gain on the likelihood (of the training data with respect to the model) is below this threshold.
Non-negative regularization added to the diagonal of covariance. Allows to assure that the covariance matrices are all positive.
The number of EM iterations to perform.
The number of initializations to perform. The result with the highest lower bound value on the likelihood is kept.
The method used to initialize the weights, the means and the covariances. Must be one of:
String describing the type of the weight concentration prior. Must be one of:
The dirichlet concentration of each component on the weight distribution (Dirichlet). This is commonly called gamma in the literature. The higher concentration puts more mass in the center and will lead to more components being active, while a lower concentration parameter will lead to more mass at the edge of the mixture weights simplex. The value of the parameter must be greater than 0. If it is None, it’s set to 1. / n_components.
The precision prior on the mean distribution (Gaussian). Controls the extent of where means can be placed. Larger values concentrate the cluster means around mean_prior. The value of the parameter must be greater than 0. If it is None, it is set to 1.
The prior on the mean distribution (Gaussian). If it is None, it is set to the mean of X.
The prior of the number of degrees of freedom on the covariance distributions (Wishart). If it is None, it’s set to n_features.
The prior on the covariance distribution (Wishart). If it is None, the emiprical covariance prior is initialized using the covariance of X. The shape depends on covariance_type:
Controls the random seed given to the method chosen to initialize the parameters (see init_params). In addition, it controls the generation of random samples from the fitted distribution (see the method sample). Pass an int for reproducible output across multiple function calls. See Glossary.
If ‘warm_start’ is True, the solution of the last fitting is used as initialization for the next call of fit(). This can speed up convergence when fit is called several times on similar problems. See the Glossary.
Enable verbose output. If 1 then it prints the current initialization and each iteration step. If greater than 1 then it prints also the log probability and the time needed for each step.
Number of iteration done before the next print.
The weights of each mixture components.
The mean of each mixture component.
The covariance of each mixture component. The shape depends on covariance_type:
The precision matrices for each component in the mixture. A precision matrix is the inverse of a covariance matrix. A covariance matrix is symmetric positive definite so the mixture of Gaussian can be equivalently parameterized by the precision matrices. Storing the precision matrices instead of the covariance matrices makes it more efficient to compute the log-likelihood of new samples at test time. The shape depends on covariance_type:
The cholesky decomposition of the precision matrices of each mixture component. A precision matrix is the inverse of a covariance matrix. A covariance matrix is symmetric positive definite so the mixture of Gaussian can be equivalently parameterized by the precision matrices. Storing the precision matrices instead of the covariance matrices makes it more efficient to compute the log-likelihood of new samples at test time. The shape depends on covariance_type:
True when convergence was reached in fit(), False otherwise.
Number of step used by the best fit of inference to reach the convergence.
Lower bound value on the likelihood (of the training data with respect to the model) of the best fit of inference.
The dirichlet concentration of each component on the weight distribution (Dirichlet). The type depends on weight_concentration_prior_type:
The higher concentration puts more mass in the center and will lead to more components being active, while a lower concentration parameter will lead to more mass at the edge of the simplex.
The dirichlet concentration of each component on the weight distribution (Dirichlet).
The precision prior on the mean distribution (Gaussian). Controls the extent of where means can be placed. Larger values concentrate the cluster means around mean_prior. If mean_precision_prior is set to None, mean_precision_prior_ is set to 1.
The precision of each components on the mean distribution (Gaussian).
The prior on the mean distribution (Gaussian).
The prior of the number of degrees of freedom on the covariance distributions (Wishart).
The number of degrees of freedom of each components in the model.
The prior on the covariance distribution (Wishart). The shape depends on covariance_type:
See also
Finite Gaussian mixture fit with EM.
Bishop, Christopher M. (2006). “Pattern recognition and machine learning”. Vol. 4 No. 4. New York: Springer.
Hagai Attias. (2000). “A Variational Bayesian Framework for Graphical Models”. In Advances in Neural Information Processing Systems 12.
Blei, David M. and Michael I. Jordan. (2006). “Variational inference for Dirichlet process mixtures”. Bayesian analysis 1.1
fit(X[, y])
Estimate model parameters with the EM algorithm.
fit_predict(X[, y])
Estimate model parameters using X and predict the labels for X.
get_params([deep])
Get parameters for this estimator.
predict(X)
Predict the labels for the data samples in X using trained model.
predict_proba(X)
Predict posterior probability of each component given the data.
sample([n_samples])
Generate random samples from the fitted Gaussian distribution.
score(X[, y])
Compute the per-sample average log-likelihood of the given data X.
score_samples(X)
Compute the weighted log probabilities for each sample.
set_params(**params)
Set the parameters of this estimator.
Estimate model parameters with the EM algorithm.
The method fits the model n_init times and sets the parameters with which the model has the largest likelihood or lower bound. Within each trial, the method iterates between E-step and M-step for max_iter times until the change of likelihood or lower bound is less than tol, otherwise, a ConvergenceWarning is raised. If warm_start is True, then n_init is ignored and a single initialization is performed upon the first call. Upon consecutive calls, training starts where it left off.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Estimate model parameters using X and predict the labels for X.
The method fits the model n_init times and sets the parameters with which the model has the largest likelihood or lower bound. Within each trial, the method iterates between E-step and M-step for max_iter times until the change of likelihood or lower bound is less than tol, otherwise, a ConvergenceWarning is raised. After fitting, it predicts the most probable label for the input data points.
New in version 0.20.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Component labels.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Predict the labels for the data samples in X using trained model.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Component labels.
Predict posterior probability of each component given the data.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Returns the probability each Gaussian (state) in the model given each sample.
Generate random samples from the fitted Gaussian distribution.
Number of samples to generate.
Randomly generated sample
Component labels
Compute the per-sample average log-likelihood of the given data X.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Log likelihood of the Gaussian mixture given X.
Compute the weighted log probabilities for each sample.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Log probabilities of each data point in X.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Gaussian Mixture Model Ellipsoids
Gaussian Mixture Model Sine Curve
Concentration Prior Type Analysis of Variation Bayesian Gaussian Mixture


Estimate model parameters with the EM algorithm.
The method fits the model n_init times and sets the parameters with which the model has the largest likelihood or lower bound. Within each trial, the method iterates between E-step and M-step for max_iter times until the change of likelihood or lower bound is less than tol, otherwise, a ConvergenceWarning is raised. If warm_start is True, then n_init is ignored and a single initialization is performed upon the first call. Upon consecutive calls, training starts where it left off.
List of n_features-dimensional data points. Each row corresponds to a single data point.


Estimate model parameters using X and predict the labels for X.
The method fits the model n_init times and sets the parameters with which the model has the largest likelihood or lower bound. Within each trial, the method iterates between E-step and M-step for max_iter times until the change of likelihood or lower bound is less than tol, otherwise, a ConvergenceWarning is raised. After fitting, it predicts the most probable label for the input data points.
New in version 0.20.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Component labels.


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


Predict the labels for the data samples in X using trained model.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Component labels.


Predict posterior probability of each component given the data.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Returns the probability each Gaussian (state) in the model given each sample.