sklearn.metrics.homogeneity_score
sklearn.metrics.homogeneity_score(labels_true, labels_pred) [source]

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

labels_trueint array, shape = [n_samples]

ground truth class labels to be used as a reference
labels_predarray-like of shape (n_samples,)

cluster labels to evaluate    Returns

homogeneityfloat

score between 0.0 and 1.0. 1.0 stands for perfectly homogeneous labeling      See also
completeness_score

v_measure_score

  References
1
Andrew Rosenberg and Julia Hirschberg, 2007. V-Measure: A conditional entropy-based external cluster evaluation measure   Examples Perfect labelings are homogeneous: >>> from sklearn.metrics.cluster import homogeneity_score
>>> homogeneity_score([0, 0, 1, 1], [1, 1, 0, 0])
1.0
 Non-perfect labelings that further split classes into more clusters can be perfectly homogeneous: >>> print("%.6f" % homogeneity_score([0, 0, 1, 1], [0, 0, 1, 2]))
1.000000
>>> print("%.6f" % homogeneity_score([0, 0, 1, 1], [0, 1, 2, 3]))
1.000000
 Clusters that include samples from different classes do not make for an homogeneous labeling: >>> print("%.6f" % homogeneity_score([0, 0, 1, 1], [0, 1, 0, 1]))
0.0...
>>> print("%.6f" % homogeneity_score([0, 0, 1, 1], [0, 0, 0, 0]))
0.0...

 Examples using sklearn.metrics.homogeneity_score

  Demo of affinity propagation clustering algorithm

  Demo of DBSCAN clustering algorithm

  A demo of K-Means clustering on the handwritten digits data

  Clustering text documents using k-means


sklearn.metrics.jaccard_score
sklearn.metrics.jaccard_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn') [source]

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

y_true1d array-like, or label indicator array / sparse matrix

Ground truth (correct) labels.
y_pred1d array-like, or label indicator array / sparse matrix

Predicted labels, as returned by a classifier.
labelsarray-like of shape (n_classes,), default=None

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.
pos_labelstr or int, default=1

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.
average{None, ‘micro’, ‘macro’, ‘samples’, ‘weighted’, ‘binary’}, default=’binary’

If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data:
'binary':

Only report results for the class specified by pos_label. This is applicable only if targets (y_{true,pred}) are binary.
'micro':

Calculate metrics globally by counting the total true positives, false negatives and false positives.
'macro':

Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
'weighted':

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.
'samples':

Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
zero_division“warn”, {0.0, 1.0}, default=”warn”

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

scorefloat (if average is not None) or array of floats, shape = [n_unique_labels]
    See also
accuracy_score, f_score, multilabel_confusion_matrix

  Notes 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. References
1
Wikipedia entry for the Jaccard index.   Examples >>> import numpy as np
>>> from sklearn.metrics import jaccard_score
>>> y_true = np.array([[0, 1, 1],
...                    [1, 1, 0]])
>>> y_pred = np.array([[1, 1, 1],
...                    [1, 0, 0]])
 In the binary case: >>> jaccard_score(y_true[0], y_pred[0])
0.6666...
 In the multilabel case: >>> jaccard_score(y_true, y_pred, average='samples')
0.5833...
>>> jaccard_score(y_true, y_pred, average='macro')
0.6666...
>>> jaccard_score(y_true, y_pred, average=None)
array([0.5, 0.5, 1. ])
 In the multiclass case: >>> y_pred = [0, 2, 1, 2]
>>> y_true = [0, 1, 2, 2]
>>> jaccard_score(y_true, y_pred, average=None)
array([1. , 0. , 0.33...])

 Examples using sklearn.metrics.jaccard_score

  Classifier Chain


sklearn.metrics.label_ranking_average_precision_score
sklearn.metrics.label_ranking_average_precision_score(y_true, y_score, *, sample_weight=None) [source]

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

y_true{ndarray, sparse matrix} of shape (n_samples, n_labels)

True binary labels in binary indicator format.
y_scorendarray of shape (n_samples, n_labels)

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_weightarray-like of shape (n_samples,), default=None

Sample weights.  New in version 0.20.     Returns

scorefloat
   Examples >>> import numpy as np
>>> from sklearn.metrics import label_ranking_average_precision_score
>>> y_true = np.array([[1, 0, 0], [0, 0, 1]])
>>> y_score = np.array([[0.75, 0.5, 1], [1, 0.2, 0.1]])
>>> label_ranking_average_precision_score(y_true, y_score)
0.416...


sklearn.metrics.label_ranking_loss
sklearn.metrics.label_ranking_loss(y_true, y_score, *, sample_weight=None) [source]

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   Parameters

y_true{ndarray, sparse matrix} of shape (n_samples, n_labels)

True binary labels in binary indicator format.
y_scorendarray of shape (n_samples, n_labels)

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_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

lossfloat
   References
1
Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010). Mining multi-label data. In Data mining and knowledge discovery handbook (pp. 667-685). Springer US.


sklearn.metrics.log_loss
sklearn.metrics.log_loss(y_true, y_pred, *, eps=1e-15, normalize=True, sample_weight=None, labels=None) [source]

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:  \[L_{\log}(y, p) = -(y \log (p) + (1 - y) \log (1 - p))\] Read more in the User Guide.  Parameters

y_truearray-like or label indicator matrix

Ground truth (correct) labels for n_samples samples.
y_predarray-like of float, shape = (n_samples, n_classes) or (n_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.
epsfloat, default=1e-15

Log loss is undefined for p=0 or p=1, so probabilities are clipped to max(eps, min(1 - eps, p)).
normalizebool, default=True

If true, return the mean loss per sample. Otherwise, return the sum of the per-sample losses.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
labelsarray-like, default=None

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

lossfloat
   Notes The logarithm used is the natural logarithm (base-e). References C.M. Bishop (2006). Pattern Recognition and Machine Learning. Springer, p. 209. Examples >>> from sklearn.metrics import log_loss
>>> log_loss(["spam", "ham", "ham", "spam"],
...          [[.1, .9], [.9, .1], [.8, .2], [.35, .65]])
0.21616...

 Examples using sklearn.metrics.log_loss

  Probability Calibration for 3-class classification

  Probabilistic predictions with Gaussian process classification (GPC)


sklearn.metrics.make_scorer
sklearn.metrics.make_scorer(score_func, *, greater_is_better=True, needs_proba=False, needs_threshold=False, **kwargs) [source]

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

score_funccallable

Score function (or loss function) with signature score_func(y, y_pred, **kwargs).
greater_is_betterbool, default=True

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.
needs_probabool, default=False

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,)).
needs_thresholdbool, default=False

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.
**kwargsadditional arguments

Additional parameters to be passed to score_func.    Returns

scorercallable

Callable object that returns a scalar score; greater is better.     Notes 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. Examples >>> from sklearn.metrics import fbeta_score, make_scorer
>>> ftwo_scorer = make_scorer(fbeta_score, beta=2)
>>> ftwo_scorer
make_scorer(fbeta_score, beta=2)
>>> from sklearn.model_selection import GridSearchCV
>>> from sklearn.svm import LinearSVC
>>> grid = GridSearchCV(LinearSVC(), param_grid={'C': [1, 10]},
...                     scoring=ftwo_scorer)

 Examples using sklearn.metrics.make_scorer

  Demonstration of multi-metric evaluation on cross_val_score and GridSearchCV


sklearn.metrics.matthews_corrcoef
sklearn.metrics.matthews_corrcoef(y_true, y_pred, *, sample_weight=None) [source]

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

y_truearray, shape = [n_samples]

Ground truth (correct) target values.
y_predarray, shape = [n_samples]

Estimated targets as returned by a classifier.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.  New in version 0.18.     Returns

mccfloat

The Matthews correlation coefficient (+1 represents a perfect prediction, 0 an average random prediction and -1 and inverse prediction).     References
1
Baldi, Brunak, Chauvin, Andersen and Nielsen, (2000). Assessing the accuracy of prediction algorithms for classification: an overview.
2
Wikipedia entry for the Matthews Correlation Coefficient.
3
Gorodkin, (2004). Comparing two K-category assignments by a K-category correlation coefficient.
4
Jurman, Riccadonna, Furlanello, (2012). A Comparison of MCC and CEN Error Measures in MultiClass Prediction.   Examples >>> from sklearn.metrics import matthews_corrcoef
>>> y_true = [+1, +1, +1, -1]
>>> y_pred = [+1, -1, +1, +1]
>>> matthews_corrcoef(y_true, y_pred)
-0.33...


sklearn.metrics.max_error
sklearn.metrics.max_error(y_true, y_pred) [source]

max_error metric calculates the maximum residual error. Read more in the User Guide.  Parameters

y_truearray-like of shape (n_samples,)

Ground truth (correct) target values.
y_predarray-like of shape (n_samples,)

Estimated target values.    Returns

max_errorfloat

A positive floating point value (the best value is 0.0).     Examples >>> from sklearn.metrics import max_error
>>> y_true = [3, 2, 7, 1]
>>> y_pred = [4, 2, 7, 1]
>>> max_error(y_true, y_pred)
1


sklearn.metrics.mean_absolute_error
sklearn.metrics.mean_absolute_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average') [source]

Mean absolute error regression loss. Read more in the User Guide.  Parameters

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

Ground truth (correct) target values.
y_predarray-like of shape (n_samples,) or (n_samples, n_outputs)

Estimated target values.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
multioutput{‘raw_values’, ‘uniform_average’} or array-like of shape (n_outputs,), default=’uniform_average’

Defines aggregating of multiple output values. Array-like value defines weights used to average errors.  ‘raw_values’ :

Returns a full set of errors in case of multioutput input.  ‘uniform_average’ :

Errors of all outputs are averaged with uniform weight.      Returns

lossfloat or ndarray of floats

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.     Examples >>> from sklearn.metrics import mean_absolute_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> mean_absolute_error(y_true, y_pred)
0.5
>>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
>>> y_pred = [[0, 2], [-1, 2], [8, -5]]
>>> mean_absolute_error(y_true, y_pred)
0.75
>>> mean_absolute_error(y_true, y_pred, multioutput='raw_values')
array([0.5, 1. ])
>>> mean_absolute_error(y_true, y_pred, multioutput=[0.3, 0.7])
0.85...

 Examples using sklearn.metrics.mean_absolute_error

  Poisson regression and non-normal loss

  Tweedie regression on insurance claims


sklearn.metrics.mean_absolute_percentage_error
sklearn.metrics.mean_absolute_percentage_error(y_true, y_pred, sample_weight=None, multioutput='uniform_average') [source]

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

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

Ground truth (correct) target values.
y_predarray-like of shape (n_samples,) or (n_samples, n_outputs)

Estimated target values.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
multioutput{‘raw_values’, ‘uniform_average’} or array-like

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,).  ‘raw_values’ :

Returns a full set of errors in case of multioutput input.  ‘uniform_average’ :

Errors of all outputs are averaged with uniform weight.      Returns

lossfloat or ndarray of floats in the range [0, 1/eps]

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.     Examples >>> from sklearn.metrics import mean_absolute_percentage_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> mean_absolute_percentage_error(y_true, y_pred)
0.3273...
>>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
>>> y_pred = [[0, 2], [-1, 2], [8, -5]]
>>> mean_absolute_percentage_error(y_true, y_pred)
0.5515...
>>> mean_absolute_percentage_error(y_true, y_pred, multioutput=[0.3, 0.7])
0.6198...


sklearn.metrics.mean_gamma_deviance
sklearn.metrics.mean_gamma_deviance(y_true, y_pred, *, sample_weight=None) [source]

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

y_truearray-like of shape (n_samples,)

Ground truth (correct) target values. Requires y_true > 0.
y_predarray-like of shape (n_samples,)

Estimated target values. Requires y_pred > 0.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

lossfloat

A non-negative floating point value (the best value is 0.0).     Examples >>> from sklearn.metrics import mean_gamma_deviance
>>> y_true = [2, 0.5, 1, 4]
>>> y_pred = [0.5, 0.5, 2., 2.]
>>> mean_gamma_deviance(y_true, y_pred)
1.0568...


sklearn.metrics.mean_poisson_deviance
sklearn.metrics.mean_poisson_deviance(y_true, y_pred, *, sample_weight=None) [source]

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

y_truearray-like of shape (n_samples,)

Ground truth (correct) target values. Requires y_true >= 0.
y_predarray-like of shape (n_samples,)

Estimated target values. Requires y_pred > 0.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

lossfloat

A non-negative floating point value (the best value is 0.0).     Examples >>> from sklearn.metrics import mean_poisson_deviance
>>> y_true = [2, 0, 1, 4]
>>> y_pred = [0.5, 0.5, 2., 2.]
>>> mean_poisson_deviance(y_true, y_pred)
1.4260...

 Examples using sklearn.metrics.mean_poisson_deviance

  Poisson regression and non-normal loss


sklearn.metrics.mean_squared_error
sklearn.metrics.mean_squared_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', squared=True) [source]

Mean squared error regression loss. Read more in the User Guide.  Parameters

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

Ground truth (correct) target values.
y_predarray-like of shape (n_samples,) or (n_samples, n_outputs)

Estimated target values.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
multioutput{‘raw_values’, ‘uniform_average’} or array-like of shape (n_outputs,), default=’uniform_average’

Defines aggregating of multiple output values. Array-like value defines weights used to average errors.  ‘raw_values’ :

Returns a full set of errors in case of multioutput input.  ‘uniform_average’ :

Errors of all outputs are averaged with uniform weight.
squaredbool, default=True

If True returns MSE value, if False returns RMSE value.    Returns

lossfloat or ndarray of floats

A non-negative floating point value (the best value is 0.0), or an array of floating point values, one for each individual target.     Examples >>> from sklearn.metrics import mean_squared_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> mean_squared_error(y_true, y_pred)
0.375
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> mean_squared_error(y_true, y_pred, squared=False)
0.612...
>>> y_true = [[0.5, 1],[-1, 1],[7, -6]]
>>> y_pred = [[0, 2],[-1, 2],[8, -5]]
>>> mean_squared_error(y_true, y_pred)
0.708...
>>> mean_squared_error(y_true, y_pred, squared=False)
0.822...
>>> mean_squared_error(y_true, y_pred, multioutput='raw_values')
array([0.41666667, 1.        ])
>>> mean_squared_error(y_true, y_pred, multioutput=[0.3, 0.7])
0.825...

 Examples using sklearn.metrics.mean_squared_error

  Gradient Boosting regression

  Model Complexity Influence

  Plot Ridge coefficients as a function of the L2 regularization

  Linear Regression Example

  Robust linear estimator fitting

  Poisson regression and non-normal loss

  Tweedie regression on insurance claims


sklearn.metrics.mean_squared_log_error
sklearn.metrics.mean_squared_log_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average') [source]

Mean squared logarithmic error regression loss. Read more in the User Guide.  Parameters

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

Ground truth (correct) target values.
y_predarray-like of shape (n_samples,) or (n_samples, n_outputs)

Estimated target values.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
multioutput{‘raw_values’, ‘uniform_average’} or array-like of shape (n_outputs,), default=’uniform_average’

Defines aggregating of multiple output values. Array-like value defines weights used to average errors.  ‘raw_values’ :

Returns a full set of errors when the input is of multioutput format.  ‘uniform_average’ :

Errors of all outputs are averaged with uniform weight.      Returns

lossfloat or ndarray of floats

A non-negative floating point value (the best value is 0.0), or an array of floating point values, one for each individual target.     Examples >>> from sklearn.metrics import mean_squared_log_error
>>> y_true = [3, 5, 2.5, 7]
>>> y_pred = [2.5, 5, 4, 8]
>>> mean_squared_log_error(y_true, y_pred)
0.039...
>>> y_true = [[0.5, 1], [1, 2], [7, 6]]
>>> y_pred = [[0.5, 2], [1, 2.5], [8, 8]]
>>> mean_squared_log_error(y_true, y_pred)
0.044...
>>> mean_squared_log_error(y_true, y_pred, multioutput='raw_values')
array([0.00462428, 0.08377444])
>>> mean_squared_log_error(y_true, y_pred, multioutput=[0.3, 0.7])
0.060...


sklearn.metrics.mean_tweedie_deviance
sklearn.metrics.mean_tweedie_deviance(y_true, y_pred, *, sample_weight=None, power=0) [source]

Mean Tweedie deviance regression loss. Read more in the User Guide.  Parameters

y_truearray-like of shape (n_samples,)

Ground truth (correct) target values.
y_predarray-like of shape (n_samples,)

Estimated target values.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
powerfloat, default=0

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.  power < 0: Extreme stable distribution. Requires: y_pred > 0. power = 0 : Normal distribution, output corresponds to mean_squared_error. y_true and y_pred can be any real numbers. power = 1 : Poisson distribution. Requires: y_true >= 0 and y_pred > 0. 1 < p < 2 : Compound Poisson distribution. Requires: y_true >= 0 and y_pred > 0. power = 2 : Gamma distribution. Requires: y_true > 0 and y_pred > 0. power = 3 : Inverse Gaussian distribution. Requires: y_true > 0 and y_pred > 0. otherwise : Positive stable distribution. Requires: y_true > 0 and y_pred > 0.     Returns

lossfloat

A non-negative floating point value (the best value is 0.0).     Examples >>> from sklearn.metrics import mean_tweedie_deviance
>>> y_true = [2, 0, 1, 4]
>>> y_pred = [0.5, 0.5, 2., 2.]
>>> mean_tweedie_deviance(y_true, y_pred, power=1)
1.4260...

 Examples using sklearn.metrics.mean_tweedie_deviance

  Tweedie regression on insurance claims


sklearn.metrics.median_absolute_error
sklearn.metrics.median_absolute_error(y_true, y_pred, *, multioutput='uniform_average', sample_weight=None) [source]

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

y_truearray-like of shape = (n_samples) or (n_samples, n_outputs)

Ground truth (correct) target values.
y_predarray-like of shape = (n_samples) or (n_samples, n_outputs)

Estimated target values.
multioutput{‘raw_values’, ‘uniform_average’} or array-like of shape (n_outputs,), default=’uniform_average’

Defines aggregating of multiple output values. Array-like value defines weights used to average errors.  ‘raw_values’ :

Returns a full set of errors in case of multioutput input.  ‘uniform_average’ :

Errors of all outputs are averaged with uniform weight.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.  New in version 0.24.     Returns

lossfloat or ndarray of floats

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.     Examples >>> from sklearn.metrics import median_absolute_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> median_absolute_error(y_true, y_pred)
0.5
>>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
>>> y_pred = [[0, 2], [-1, 2], [8, -5]]
>>> median_absolute_error(y_true, y_pred)
0.75
>>> median_absolute_error(y_true, y_pred, multioutput='raw_values')
array([0.5, 1. ])
>>> median_absolute_error(y_true, y_pred, multioutput=[0.3, 0.7])
0.85

 Examples using sklearn.metrics.median_absolute_error

  Common pitfalls in interpretation of coefficients of linear models

  Effect of transforming the targets in regression model


sklearn.metrics.multilabel_confusion_matrix
sklearn.metrics.multilabel_confusion_matrix(y_true, y_pred, *, sample_weight=None, labels=None, samplewise=False) [source]

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

y_true{array-like, sparse matrix} of shape (n_samples, n_outputs) or (n_samples,)

Ground truth (correct) target values.
y_pred{array-like, sparse matrix} of shape (n_samples, n_outputs) or (n_samples,)

Estimated targets as returned by a classifier.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
labelsarray-like of shape (n_classes,), default=None

A list of classes or column indices to select some (or to force inclusion of classes absent from the data).
samplewisebool, default=False

In the multilabel case, this calculates a confusion matrix per sample.    Returns

multi_confusionndarray of shape (n_outputs, 2, 2)

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
confusion_matrix

  Notes 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. Examples Multilabel-indicator case: >>> import numpy as np
>>> from sklearn.metrics import multilabel_confusion_matrix
>>> y_true = np.array([[1, 0, 1],
...                    [0, 1, 0]])
>>> y_pred = np.array([[1, 0, 0],
...                    [0, 1, 1]])
>>> multilabel_confusion_matrix(y_true, y_pred)
array([[[1, 0],
        [0, 1]],

       [[1, 0],
        [0, 1]],

       [[0, 1],
        [1, 0]]])
 Multiclass case: >>> y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
>>> y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
>>> multilabel_confusion_matrix(y_true, y_pred,
...                             labels=["ant", "bird", "cat"])
array([[[3, 1],
        [0, 2]],

       [[5, 0],
        [1, 0]],

       [[2, 1],
        [1, 2]]])


sklearn.metrics.mutual_info_score
sklearn.metrics.mutual_info_score(labels_true, labels_pred, *, contingency=None) [source]

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:  \[MI(U,V)=\sum_{i=1}^{|U|} \sum_{j=1}^{|V|} \frac{|U_i\cap V_j|}{N} \log\frac{N|U_i \cap V_j|}{|U_i||V_j|}\] 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.  Parameters

labels_trueint array, shape = [n_samples]

A clustering of the data into disjoint subsets.
labels_predint array-like of shape (n_samples,)

A clustering of the data into disjoint subsets.
contingency{ndarray, sparse matrix} of shape (n_classes_true, n_classes_pred), default=None

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

mifloat

Mutual information, a non-negative value      See also
adjusted_mutual_info_score

Adjusted against chance Mutual Information.
normalized_mutual_info_score

Normalized Mutual Information.    Notes The logarithm used is the natural logarithm (base-e).
 Examples using sklearn.metrics.mutual_info_score

  Adjustment for chance in clustering performance evaluation


sklearn.metrics.ndcg_score
sklearn.metrics.ndcg_score(y_true, y_score, *, k=None, sample_weight=None, ignore_ties=False) [source]

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

y_truendarray of shape (n_samples, n_labels)

True targets of multilabel classification, or true scores of entities to be ranked.
y_scorendarray of shape (n_samples, n_labels)

Target scores, can either be probability estimates, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).
kint, default=None

Only consider the highest k scores in the ranking. If None, use all outputs.
sample_weightndarray of shape (n_samples,), default=None

Sample weights. If None, all samples are given the same weight.
ignore_tiesbool, default=False

Assume that there are no ties in y_score (which is likely to be the case if y_score is continuous) for efficiency gains.    Returns

normalized_discounted_cumulative_gainfloat in [0., 1.]

The averaged NDCG scores for all samples.      See also
dcg_score

Discounted Cumulative Gain (not normalized).    References 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. Examples >>> from sklearn.metrics import ndcg_score
>>> # we have groud-truth relevance of some answers to a query:
>>> true_relevance = np.asarray([[10, 0, 0, 1, 5]])
>>> # we predict some scores (relevance) for the answers
>>> scores = np.asarray([[.1, .2, .3, 4, 70]])
>>> ndcg_score(true_relevance, scores)
0.69...
>>> scores = np.asarray([[.05, 1.1, 1., .5, .0]])
>>> ndcg_score(true_relevance, scores)
0.49...
>>> # we can set k to truncate the sum; only top k answers contribute.
>>> ndcg_score(true_relevance, scores, k=4)
0.35...
>>> # the normalization takes k into account so a perfect answer
>>> # would still get 1.0
>>> ndcg_score(true_relevance, true_relevance, k=4)
1.0
>>> # now we have some ties in our prediction
>>> scores = np.asarray([[1, 0, 0, 0, 1]])
>>> # by default ties are averaged, so here we get the average (normalized)
>>> # true relevance of our top predictions: (10 / 10 + 5 / 10) / 2 = .75
>>> ndcg_score(true_relevance, scores, k=1)
0.75
>>> # we can choose to ignore ties for faster results, but only
>>> # if we know there aren't ties in our scores, otherwise we get
>>> # wrong results:
>>> ndcg_score(true_relevance,
...           scores, k=1, ignore_ties=True)
0.5


sklearn.metrics.normalized_mutual_info_score
sklearn.metrics.normalized_mutual_info_score(labels_true, labels_pred, *, average_method='arithmetic') [source]

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

labels_trueint array, shape = [n_samples]

A clustering of the data into disjoint subsets.
labels_predint array-like of shape (n_samples,)

A clustering of the data into disjoint subsets.
average_methodstr, default=’arithmetic’

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’.     Returns

nmifloat

score between 0.0 and 1.0. 1.0 stands for perfectly complete labeling      See also
v_measure_score

V-Measure (NMI with arithmetic mean option).
adjusted_rand_score

Adjusted Rand Index.
adjusted_mutual_info_score

Adjusted Mutual Information (adjusted against chance).    Examples Perfect labelings are both homogeneous and complete, hence have score 1.0: >>> from sklearn.metrics.cluster import normalized_mutual_info_score
>>> normalized_mutual_info_score([0, 0, 1, 1], [0, 0, 1, 1])
...
1.0
>>> normalized_mutual_info_score([0, 0, 1, 1], [1, 1, 0, 0])
...
1.0
 If classes members are completely split across different clusters, the assignment is totally in-complete, hence the NMI is null: >>> normalized_mutual_info_score([0, 0, 0, 0], [0, 1, 2, 3])
...
0.0


sklearn.metrics.pairwise.additive_chi2_kernel
sklearn.metrics.pairwise.additive_chi2_kernel(X, Y=None) [source]

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: k(x, y) = -Sum [(x - y)^2 / (x + y)]
 It can be interpreted as a weighted difference per entry. Read more in the User Guide.  Parameters

Xarray-like of shape (n_samples_X, n_features)

Yndarray of shape (n_samples_Y, n_features), default=None
  Returns

kernel_matrixndarray of shape (n_samples_X, n_samples_Y)
    See also
chi2_kernel

The exponentiated version of the kernel, which is usually preferable.
sklearn.kernel_approximation.AdditiveChi2Sampler

A Fourier approximation to this kernel.    Notes As the negative of a distance, this kernel is only conditionally positive definite. References  Zhang, J. and Marszalek, M. and Lazebnik, S. and Schmid, C. Local features and kernels for classification of texture and object categories: A comprehensive study International Journal of Computer Vision 2007 https://research.microsoft.com/en-us/um/people/manik/projects/trade-off/papers/ZhangIJCV06.pdf


sklearn.metrics.pairwise.chi2_kernel
sklearn.metrics.pairwise.chi2_kernel(X, Y=None, gamma=1.0) [source]

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: k(x, y) = exp(-gamma Sum [(x - y)^2 / (x + y)])
 It can be interpreted as a weighted difference per entry. Read more in the User Guide.  Parameters

Xarray-like of shape (n_samples_X, n_features)

Yndarray of shape (n_samples_Y, n_features), default=None

gammafloat, default=1.

Scaling parameter of the chi2 kernel.    Returns

kernel_matrixndarray of shape (n_samples_X, n_samples_Y)
    See also
additive_chi2_kernel

The additive version of this kernel.
sklearn.kernel_approximation.AdditiveChi2Sampler

A Fourier approximation to the additive version of this kernel.    References  Zhang, J. and Marszalek, M. and Lazebnik, S. and Schmid, C. Local features and kernels for classification of texture and object categories: A comprehensive study International Journal of Computer Vision 2007 https://research.microsoft.com/en-us/um/people/manik/projects/trade-off/papers/ZhangIJCV06.pdf


sklearn.metrics.pairwise.cosine_distances
sklearn.metrics.pairwise.cosine_distances(X, Y=None) [source]

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

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

Matrix X.
Y{array-like, sparse matrix} of shape (n_samples_Y, n_features), default=None

Matrix Y.    Returns

distance matrixndarray of shape (n_samples_X, n_samples_Y)
    See also
cosine_similarity

scipy.spatial.distance.cosine

Dense matrices only.


sklearn.metrics.pairwise.cosine_similarity
sklearn.metrics.pairwise.cosine_similarity(X, Y=None, dense_output=True) [source]

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

X{ndarray, sparse matrix} of shape (n_samples_X, n_features)

Input data.
Y{ndarray, sparse matrix} of shape (n_samples_Y, n_features), default=None

Input data. If None, the output will be the pairwise similarities between all samples in X.
dense_outputbool, default=True

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

kernel matrixndarray of shape (n_samples_X, n_samples_Y)


sklearn.metrics.pairwise.distance_metrics
sklearn.metrics.pairwise.distance_metrics() [source]

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.


sklearn.metrics.pairwise.euclidean_distances
sklearn.metrics.pairwise.euclidean_distances(X, Y=None, *, Y_norm_squared=None, squared=False, X_norm_squared=None) [source]

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: dist(x, y) = sqrt(dot(x, x) - 2 * dot(x, y) + dot(y, y))
 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.  Parameters

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

Y{array-like, sparse matrix} of shape (n_samples_Y, n_features), default=None

Y_norm_squaredarray-like of shape (n_samples_Y,), default=None

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.
squaredbool, default=False

Return squared Euclidean distances.
X_norm_squaredarray-like of shape (n_samples,), default=None

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

distancesndarray of shape (n_samples_X, n_samples_Y)
    See also
paired_distances

Distances betweens pairs of elements of X and Y.    Notes To achieve better accuracy, X_norm_squared and Y_norm_squared may be unused if they are passed as float32. Examples >>> from sklearn.metrics.pairwise import euclidean_distances
>>> X = [[0, 1], [1, 1]]
>>> # distance between rows of X
>>> euclidean_distances(X, X)
array([[0., 1.],
       [1., 0.]])
>>> # get distance to origin
>>> euclidean_distances(X, [[0, 0]])
array([[1.        ],
       [1.41421356]])


sklearn.metrics.pairwise.haversine_distances
sklearn.metrics.pairwise.haversine_distances(X, Y=None) [source]

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.  \[D(x, y) = 2\arcsin[\sqrt{\sin^2((x1 - y1) / 2) + \cos(x1)\cos(y1)\sin^2((x2 - y2) / 2)}]\]  Parameters

Xarray-like of shape (n_samples_X, 2)

Yarray-like of shape (n_samples_Y, 2), default=None
  Returns

distancendarray of shape (n_samples_X, n_samples_Y)
   Notes 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. Examples We want to calculate the distance between the Ezeiza Airport (Buenos Aires, Argentina) and the Charles de Gaulle Airport (Paris, France). >>> from sklearn.metrics.pairwise import haversine_distances
>>> from math import radians
>>> bsas = [-34.83333, -58.5166646]
>>> paris = [49.0083899664, 2.53844117956]
>>> bsas_in_radians = [radians(_) for _ in bsas]
>>> paris_in_radians = [radians(_) for _ in paris]
>>> result = haversine_distances([bsas_in_radians, paris_in_radians])
>>> result * 6371000/1000  # multiply by Earth radius to get kilometers
array([[    0.        , 11099.54035582],
       [11099.54035582,     0.        ]])


sklearn.metrics.pairwise.kernel_metrics
sklearn.metrics.pairwise.kernel_metrics() [source]

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.  The valid distance metrics, and the function they map to, are:

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.


sklearn.metrics.pairwise.laplacian_kernel
sklearn.metrics.pairwise.laplacian_kernel(X, Y=None, gamma=None) [source]

Compute the laplacian kernel between X and Y. The laplacian kernel is defined as: K(x, y) = exp(-gamma ||x-y||_1)
 for each pair of rows x in X and y in Y. Read more in the User Guide.  New in version 0.17.   Parameters

Xndarray of shape (n_samples_X, n_features)

Yndarray of shape (n_samples_Y, n_features), default=None

gammafloat, default=None

If None, defaults to 1.0 / n_features.    Returns

kernel_matrixndarray of shape (n_samples_X, n_samples_Y)


sklearn.metrics.pairwise.linear_kernel
sklearn.metrics.pairwise.linear_kernel(X, Y=None, dense_output=True) [source]

Compute the linear kernel between X and Y. Read more in the User Guide.  Parameters

Xndarray of shape (n_samples_X, n_features)

Yndarray of shape (n_samples_Y, n_features), default=None

dense_outputbool, default=True

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

Gram matrixndarray of shape (n_samples_X, n_samples_Y)


sklearn.metrics.pairwise.manhattan_distances
sklearn.metrics.pairwise.manhattan_distances(X, Y=None, *, sum_over_features=True) [source]

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

Xarray-like of shape (n_samples_X, n_features)

Yarray-like of shape (n_samples_Y, n_features), default=None

sum_over_featuresbool, default=True

If True the function returns the pairwise distance matrix else it returns the componentwise L1 pairwise-distances. Not supported for sparse matrix inputs.    Returns

Dndarray of shape (n_samples_X * n_samples_Y, n_features) or (n_samples_X, n_samples_Y)

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.     Notes 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. Examples >>> from sklearn.metrics.pairwise import manhattan_distances
>>> manhattan_distances([[3]], [[3]])
array([[0.]])
>>> manhattan_distances([[3]], [[2]])
array([[1.]])
>>> manhattan_distances([[2]], [[3]])
array([[1.]])
>>> manhattan_distances([[1, 2], [3, 4]],         [[1, 2], [0, 3]])
array([[0., 2.],
       [4., 4.]])
>>> import numpy as np
>>> X = np.ones((1, 2))
>>> y = np.full((2, 2), 2.)
>>> manhattan_distances(X, y, sum_over_features=False)
array([[1., 1.],
       [1., 1.]])


sklearn.metrics.pairwise.nan_euclidean_distances
sklearn.metrics.pairwise.nan_euclidean_distances(X, Y=None, *, squared=False, missing_values=nan, copy=True) [source]

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:  \[\sqrt{\frac{4}{2}((3-1)^2 + (6-5)^2)}\] 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.   Parameters

Xarray-like of shape=(n_samples_X, n_features)

Yarray-like of shape=(n_samples_Y, n_features), default=None

squaredbool, default=False

Return squared Euclidean distances.
missing_valuesnp.nan or int, default=np.nan

Representation of missing value.
copybool, default=True

Make and use a deep copy of X and Y (if Y exists).    Returns

distancesndarray of shape (n_samples_X, n_samples_Y)
    See also
paired_distances

Distances between pairs of elements of X and Y.    References  John K. Dixon, “Pattern Recognition with Partly Missing Data”, IEEE Transactions on Systems, Man, and Cybernetics, Volume: 9, Issue: 10, pp. 617 - 621, Oct. 1979. http://ieeexplore.ieee.org/abstract/document/4310090/
  Examples >>> from sklearn.metrics.pairwise import nan_euclidean_distances
>>> nan = float("NaN")
>>> X = [[0, 1], [1, nan]]
>>> nan_euclidean_distances(X, X) # distance between rows of X
array([[0.        , 1.41421356],
       [1.41421356, 0.        ]])
 >>> # get distance to origin
>>> nan_euclidean_distances(X, [[0, 0]])
array([[1.        ],
       [1.41421356]])


sklearn.metrics.pairwise.paired_cosine_distances
sklearn.metrics.pairwise.paired_cosine_distances(X, Y) [source]

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

Xarray-like of shape (n_samples, n_features)

Yarray-like of shape (n_samples, n_features)
  Returns

distancesndarray of shape (n_samples,)
   Notes The cosine distance is equivalent to the half the squared euclidean distance if each sample is normalized to unit norm.


sklearn.metrics.pairwise.paired_distances
sklearn.metrics.pairwise.paired_distances(X, Y, *, metric='euclidean', **kwds) [source]

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

Xndarray of shape (n_samples, n_features)

Array 1 for distance computation.
Yndarray of shape (n_samples, n_features)

Array 2 for distance computation.
metricstr or callable, default=”euclidean”

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

distancesndarray of shape (n_samples,)
    See also
pairwise_distances

Computes the distance between every pair of samples.    Examples >>> from sklearn.metrics.pairwise import paired_distances
>>> X = [[0, 1], [1, 1]]
>>> Y = [[0, 1], [2, 1]]
>>> paired_distances(X, Y)
array([0., 1.])


sklearn.metrics.pairwise.paired_euclidean_distances
sklearn.metrics.pairwise.paired_euclidean_distances(X, Y) [source]

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

Xarray-like of shape (n_samples, n_features)

Yarray-like of shape (n_samples, n_features)
  Returns

distancesndarray of shape (n_samples,)


sklearn.metrics.pairwise.paired_manhattan_distances
sklearn.metrics.pairwise.paired_manhattan_distances(X, Y) [source]

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

Xarray-like of shape (n_samples, n_features)

Yarray-like of shape (n_samples, n_features)
  Returns

distancesndarray of shape (n_samples,)


sklearn.metrics.pairwise.pairwise_kernels
sklearn.metrics.pairwise.pairwise_kernels(X, Y=None, metric='linear', *, filter_params=False, n_jobs=None, **kwds) [source]

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.  Valid values for metric are:

[‘additive_chi2’, ‘chi2’, ‘linear’, ‘poly’, ‘polynomial’, ‘rbf’, ‘laplacian’, ‘sigmoid’, ‘cosine’]   Read more in the User Guide.  Parameters

Xndarray of shape (n_samples_X, n_samples_X) or (n_samples_X, n_features)

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.
Yndarray of shape (n_samples_Y, n_features), default=None

A second feature array only if X has shape (n_samples_X, n_features).
metricstr or callable, default=”linear”

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.
filter_paramsbool, default=False

Whether to filter invalid parameters or not.
n_jobsint, default=None

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.
**kwdsoptional keyword parameters

Any further parameters are passed directly to the kernel function.    Returns

Kndarray of shape (n_samples_X, n_samples_X) or (n_samples_X, n_samples_Y)

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.     Notes If metric is ‘precomputed’, Y is ignored and X is returned.


sklearn.metrics.pairwise.polynomial_kernel
sklearn.metrics.pairwise.polynomial_kernel(X, Y=None, degree=3, gamma=None, coef0=1) [source]

Compute the polynomial kernel between X and Y: K(X, Y) = (gamma <X, Y> + coef0)^degree
 Read more in the User Guide.  Parameters

Xndarray of shape (n_samples_X, n_features)

Yndarray of shape (n_samples_Y, n_features), default=None

degreeint, default=3

gammafloat, default=None

If None, defaults to 1.0 / n_features.
coef0float, default=1
  Returns

Gram matrixndarray of shape (n_samples_X, n_samples_Y)


sklearn.metrics.pairwise.rbf_kernel
sklearn.metrics.pairwise.rbf_kernel(X, Y=None, gamma=None) [source]

Compute the rbf (gaussian) kernel between X and Y: K(x, y) = exp(-gamma ||x-y||^2)
 for each pair of rows x in X and y in Y. Read more in the User Guide.  Parameters

Xndarray of shape (n_samples_X, n_features)

Yndarray of shape (n_samples_Y, n_features), default=None

gammafloat, default=None

If None, defaults to 1.0 / n_features.    Returns

kernel_matrixndarray of shape (n_samples_X, n_samples_Y)


sklearn.metrics.pairwise.sigmoid_kernel
sklearn.metrics.pairwise.sigmoid_kernel(X, Y=None, gamma=None, coef0=1) [source]

Compute the sigmoid kernel between X and Y: K(X, Y) = tanh(gamma <X, Y> + coef0)
 Read more in the User Guide.  Parameters

Xndarray of shape (n_samples_X, n_features)

Yndarray of shape (n_samples_Y, n_features), default=None

gammafloat, default=None

If None, defaults to 1.0 / n_features.
coef0float, default=1
  Returns

Gram matrixndarray of shape (n_samples_X, n_samples_Y)


sklearn.metrics.pairwise_distances
sklearn.metrics.pairwise_distances(X, Y=None, metric='euclidean', *, n_jobs=None, force_all_finite=True, **kwds) [source]

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:  From scikit-learn: [‘cityblock’, ‘cosine’, ‘euclidean’, ‘l1’, ‘l2’, ‘manhattan’]. These metrics support sparse matrix inputs. [‘nan_euclidean’] but it does not yet support sparse matrices. From scipy.spatial.distance: [‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘correlation’, ‘dice’, ‘hamming’, ‘jaccard’, ‘kulsinski’, ‘mahalanobis’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’] See the documentation for scipy.spatial.distance for details on these metrics. These metrics do not support sparse matrix inputs.  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.  Parameters

Xndarray of shape (n_samples_X, n_samples_X) or (n_samples_X, n_features)

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.
Yndarray of shape (n_samples_Y, n_features), default=None

An optional second feature array. Only allowed if metric != “precomputed”.
metricstr or callable, default=’euclidean’

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.
n_jobsint, default=None

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.
force_all_finitebool or ‘allow-nan’, default=True

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:  True: Force all values of array to be finite. False: accepts np.inf, np.nan, pd.NA in array. ‘allow-nan’: accepts only np.nan and pd.NA values in array. Values cannot be infinite.   New in version 0.22: force_all_finite accepts the string 'allow-nan'.   Changed in version 0.23: Accepts pd.NA and converts it into np.nan.
**kwdsoptional keyword parameters

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

Dndarray of shape (n_samples_X, n_samples_X) or (n_samples_X, n_samples_Y)

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
pairwise_distances_chunked

Performs the same calculation as this function, but returns a generator of chunks of the distance matrix, in order to limit memory usage.
paired_distances

Computes the distances between corresponding elements of two arrays.
 Examples using sklearn.metrics.pairwise_distances

  Agglomerative clustering with different metrics


sklearn.metrics.pairwise_distances_argmin
sklearn.metrics.pairwise_distances_argmin(X, Y, *, axis=1, metric='euclidean', metric_kwargs=None) [source]

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

Xarray-like of shape (n_samples_X, n_features)

Array containing points.
Yarray-like of shape (n_samples_Y, n_features)

Arrays containing points.
axisint, default=1

Axis along which the argmin and distances are to be computed.
metricstr or callable, default=”euclidean”

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:  from scikit-learn: [‘cityblock’, ‘cosine’, ‘euclidean’, ‘l1’, ‘l2’, ‘manhattan’] from scipy.spatial.distance: [‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘correlation’, ‘dice’, ‘hamming’, ‘jaccard’, ‘kulsinski’, ‘mahalanobis’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’]  See the documentation for scipy.spatial.distance for details on these metrics.
metric_kwargsdict, default=None

Keyword arguments to pass to specified metric function.    Returns

argminnumpy.ndarray

Y[argmin[i], :] is the row in Y that is closest to X[i, :].      See also
sklearn.metrics.pairwise_distances

sklearn.metrics.pairwise_distances_argmin_min

 Examples using sklearn.metrics.pairwise_distances_argmin

  Color Quantization using K-Means

  Comparison of the K-Means and MiniBatchKMeans clustering algorithms


sklearn.metrics.pairwise_distances_argmin_min
sklearn.metrics.pairwise_distances_argmin_min(X, Y, *, axis=1, metric='euclidean', metric_kwargs=None) [source]

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).argmin(axis=axis),

pairwise_distances(X, Y=Y, metric=metric).min(axis=axis))   but uses much less memory, and is faster for large arrays.  Parameters

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

Array containing points.
Y{array-like, sparse matrix} of shape (n_samples_Y, n_features)

Array containing points.
axisint, default=1

Axis along which the argmin and distances are to be computed.
metricstr or callable, default=’euclidean’

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:  from scikit-learn: [‘cityblock’, ‘cosine’, ‘euclidean’, ‘l1’, ‘l2’, ‘manhattan’] from scipy.spatial.distance: [‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘correlation’, ‘dice’, ‘hamming’, ‘jaccard’, ‘kulsinski’, ‘mahalanobis’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’]  See the documentation for scipy.spatial.distance for details on these metrics.
metric_kwargsdict, default=None

Keyword arguments to pass to specified metric function.    Returns

argminndarray

Y[argmin[i], :] is the row in Y that is closest to X[i, :].
distancesndarray

distances[i] is the distance between the i-th row in X and the argmin[i]-th row in Y.      See also
sklearn.metrics.pairwise_distances

sklearn.metrics.pairwise_distances_argmin


sklearn.metrics.pairwise_distances_chunked
sklearn.metrics.pairwise_distances_chunked(X, Y=None, *, reduce_func=None, metric='euclidean', n_jobs=None, working_memory=None, **kwds) [source]

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

Xndarray of shape (n_samples_X, n_samples_X) or (n_samples_X, n_features)

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.
Yndarray of shape (n_samples_Y, n_features), default=None

An optional second feature array. Only allowed if metric != “precomputed”.
reduce_funccallable, default=None

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.
metricstr or callable, default=’euclidean’

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.
n_jobsint, default=None

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.
working_memoryint, default=None

The sought maximum memory for temporary distance matrix chunks. When None (default), the value of sklearn.get_config()['working_memory'] is used.
`**kwds`optional keyword parameters

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

D_chunk{ndarray, sparse matrix}

A contiguous slice of distance matrix, optionally processed by reduce_func.     Examples Without reduce_func: >>> import numpy as np
>>> from sklearn.metrics import pairwise_distances_chunked
>>> X = np.random.RandomState(0).rand(5, 3)
>>> D_chunk = next(pairwise_distances_chunked(X))
>>> D_chunk
array([[0.  ..., 0.29..., 0.41..., 0.19..., 0.57...],
       [0.29..., 0.  ..., 0.57..., 0.41..., 0.76...],
       [0.41..., 0.57..., 0.  ..., 0.44..., 0.90...],
       [0.19..., 0.41..., 0.44..., 0.  ..., 0.51...],
       [0.57..., 0.76..., 0.90..., 0.51..., 0.  ...]])
 Retrieve all neighbors and average distance within radius r: >>> r = .2
>>> def reduce_func(D_chunk, start):
...     neigh = [np.flatnonzero(d < r) for d in D_chunk]
...     avg_dist = (D_chunk * (D_chunk < r)).mean(axis=1)
...     return neigh, avg_dist
>>> gen = pairwise_distances_chunked(X, reduce_func=reduce_func)
>>> neigh, avg_dist = next(gen)
>>> neigh
[array([0, 3]), array([1]), array([2]), array([0, 3]), array([4])]
>>> avg_dist
array([0.039..., 0.        , 0.        , 0.039..., 0.        ])
 Where r is defined per sample, we need to make use of start: >>> r = [.2, .4, .4, .3, .1]
>>> def reduce_func(D_chunk, start):
...     neigh = [np.flatnonzero(d < r[i])
...              for i, d in enumerate(D_chunk, start)]
...     return neigh
>>> neigh = next(pairwise_distances_chunked(X, reduce_func=reduce_func))
>>> neigh
[array([0, 3]), array([0, 1]), array([2]), array([0, 3]), array([4])]
 Force row-by-row generation by reducing working_memory: >>> gen = pairwise_distances_chunked(X, reduce_func=reduce_func,
...                                  working_memory=0)
>>> next(gen)
[array([0, 3])]
>>> next(gen)
[array([0, 1])]


sklearn.metrics.plot_confusion_matrix
sklearn.metrics.plot_confusion_matrix(estimator, X, y_true, *, labels=None, sample_weight=None, normalize=None, display_labels=None, include_values=True, xticks_rotation='horizontal', values_format=None, cmap='viridis', ax=None, colorbar=True) [source]

Plot Confusion Matrix. Read more in the User Guide.  Parameters

estimatorestimator instance

Fitted classifier or a fitted Pipeline in which the last estimator is a classifier.
X{array-like, sparse matrix} of shape (n_samples, n_features)

Input values.
y_truearray-like of shape (n_samples,)

Target values.
labelsarray-like of shape (n_classes,), default=None

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_weightarray-like of shape (n_samples,), default=None

Sample weights.
normalize{‘true’, ‘pred’, ‘all’}, default=None

Normalizes confusion matrix over the true (rows), predicted (columns) conditions or all the population. If None, confusion matrix will not be normalized.
display_labelsarray-like of shape (n_classes,), default=None

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.
include_valuesbool, default=True

Includes values in confusion matrix.
xticks_rotation{‘vertical’, ‘horizontal’} or float, default=’horizontal’

Rotation of xtick labels.
values_formatstr, default=None

Format specification for values in confusion matrix. If None, the format specification is ‘d’ or ‘.2g’ whichever is shorter.
cmapstr or matplotlib Colormap, default=’viridis’

Colormap recognized by matplotlib.
axmatplotlib Axes, default=None

Axes object to plot on. If None, a new figure and axes is created.
colorbarbool, default=True

Whether or not to add a colorbar to the plot.  New in version 0.24.     Returns

displayConfusionMatrixDisplay
    See also
confusion_matrix

Compute Confusion Matrix to evaluate the accuracy of a classification.
ConfusionMatrixDisplay

Confusion Matrix visualization.    Examples >>> import matplotlib.pyplot as plt
>>> from sklearn.datasets import make_classification
>>> from sklearn.metrics import plot_confusion_matrix
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.svm import SVC
>>> X, y = make_classification(random_state=0)
>>> X_train, X_test, y_train, y_test = train_test_split(
...         X, y, random_state=0)
>>> clf = SVC(random_state=0)
>>> clf.fit(X_train, y_train)
SVC(random_state=0)
>>> plot_confusion_matrix(clf, X_test, y_test)
>>> plt.show()

 Examples using sklearn.metrics.plot_confusion_matrix

  Recognizing hand-written digits

  Confusion matrix


sklearn.metrics.plot_det_curve
sklearn.metrics.plot_det_curve(estimator, X, y, *, sample_weight=None, response_method='auto', name=None, ax=None, pos_label=None, **kwargs) [source]

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

estimatorestimator instance

Fitted classifier or a fitted Pipeline in which the last estimator is a classifier.
X{array-like, sparse matrix} of shape (n_samples, n_features)

Input values.
yarray-like of shape (n_samples,)

Target values.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
response_method{‘predict_proba’, ‘decision_function’, ‘auto’} default=’auto’

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.
namestr, default=None

Name of DET curve for labeling. If None, use the name of the estimator.
axmatplotlib axes, default=None

Axes object to plot on. If None, a new figure and axes is created.
pos_labelstr or int, default=None

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

displayDetCurveDisplay

Object that stores computed values.      See also
det_curve

Compute error rates for different probability thresholds.
DetCurveDisplay

DET curve visualization.
plot_roc_curve

Plot Receiver operating characteristic (ROC) curve.    Examples >>> import matplotlib.pyplot as plt
>>> from sklearn import datasets, metrics, model_selection, svm
>>> X, y = datasets.make_classification(random_state=0)
>>> X_train, X_test, y_train, y_test = model_selection.train_test_split(
...     X, y, random_state=0)
>>> clf = svm.SVC(random_state=0)
>>> clf.fit(X_train, y_train)
SVC(random_state=0)
>>> metrics.plot_det_curve(clf, X_test, y_test)
>>> plt.show()

 Examples using sklearn.metrics.plot_det_curve

  Detection error tradeoff (DET) curve


sklearn.metrics.plot_precision_recall_curve
sklearn.metrics.plot_precision_recall_curve(estimator, X, y, *, sample_weight=None, response_method='auto', name=None, ax=None, pos_label=None, **kwargs) [source]

Plot Precision Recall Curve for binary classifiers. Extra keyword arguments will be passed to matplotlib’s plot. Read more in the User Guide.  Parameters

estimatorestimator instance

Fitted classifier or a fitted Pipeline in which the last estimator is a classifier.
X{array-like, sparse matrix} of shape (n_samples, n_features)

Input values.
yarray-like of shape (n_samples,)

Binary target values.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
response_method{‘predict_proba’, ‘decision_function’, ‘auto’}, default=’auto’

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.
namestr, default=None

Name for labeling curve. If None, the name of the estimator is used.
axmatplotlib axes, default=None

Axes object to plot on. If None, a new figure and axes is created.
pos_labelstr or int, default=None

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.
**kwargsdict

Keyword arguments to be passed to matplotlib’s plot.    Returns

displayPrecisionRecallDisplay

Object that stores computed values.      See also
precision_recall_curve

Compute precision-recall pairs for different probability thresholds.
PrecisionRecallDisplay

Precision Recall visualization.
 Examples using sklearn.metrics.plot_precision_recall_curve

  Precision-Recall


sklearn.metrics.plot_roc_curve
sklearn.metrics.plot_roc_curve(estimator, X, y, *, sample_weight=None, drop_intermediate=True, response_method='auto', name=None, ax=None, pos_label=None, **kwargs) [source]

Plot Receiver operating characteristic (ROC) curve. Extra keyword arguments will be passed to matplotlib’s plot. Read more in the User Guide.  Parameters

estimatorestimator instance

Fitted classifier or a fitted Pipeline in which the last estimator is a classifier.
X{array-like, sparse matrix} of shape (n_samples, n_features)

Input values.
yarray-like of shape (n_samples,)

Target values.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
drop_intermediateboolean, default=True

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.
response_method{‘predict_proba’, ‘decision_function’, ‘auto’} default=’auto’

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.
namestr, default=None

Name of ROC Curve for labeling. If None, use the name of the estimator.
axmatplotlib axes, default=None

Axes object to plot on. If None, a new figure and axes is created.
pos_labelstr or int, default=None

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

displayRocCurveDisplay

Object that stores computed values.      See also
roc_curve

Compute Receiver operating characteristic (ROC) curve.
RocCurveDisplay

ROC Curve visualization.
roc_auc_score

Compute the area under the ROC curve.    Examples >>> import matplotlib.pyplot as plt
>>> from sklearn import datasets, metrics, model_selection, svm
>>> X, y = datasets.make_classification(random_state=0)
>>> X_train, X_test, y_train, y_test = model_selection.train_test_split(
...     X, y, random_state=0)
>>> clf = svm.SVC(random_state=0)
>>> clf.fit(X_train, y_train)
SVC(random_state=0)
>>> metrics.plot_roc_curve(clf, X_test, y_test)
>>> plt.show()

 Examples using sklearn.metrics.plot_roc_curve

  Release Highlights for scikit-learn 0.22

  ROC Curve with Visualization API

  Detection error tradeoff (DET) curve

  Receiver Operating Characteristic (ROC) with cross validation


sklearn.metrics.precision_recall_curve
sklearn.metrics.precision_recall_curve(y_true, probas_pred, *, pos_label=None, sample_weight=None) [source]

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

y_truendarray of shape (n_samples,)

True binary labels. If labels are not either {-1, 1} or {0, 1}, then pos_label should be explicitly given.
probas_predndarray of shape (n_samples,)

Estimated probabilities or output of a decision function.
pos_labelint or str, default=None

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_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

precisionndarray of shape (n_thresholds + 1,)

Precision values such that element i is the precision of predictions with score >= thresholds[i] and the last element is 1.
recallndarray of shape (n_thresholds + 1,)

Decreasing recall values such that element i is the recall of predictions with score >= thresholds[i] and the last element is 0.
thresholdsndarray of shape (n_thresholds,)

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

Plot Precision Recall Curve for binary classifiers.
PrecisionRecallDisplay

Precision Recall visualization.
average_precision_score

Compute average precision from prediction scores.
det_curve

Compute error rates for different probability thresholds.
roc_curve

Compute Receiver operating characteristic (ROC) curve.    Examples >>> import numpy as np
>>> from sklearn.metrics import precision_recall_curve
>>> y_true = np.array([0, 0, 1, 1])
>>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> precision, recall, thresholds = precision_recall_curve(
...     y_true, y_scores)
>>> precision
array([0.66666667, 0.5       , 1.        , 1.        ])
>>> recall
array([1. , 0.5, 0.5, 0. ])
>>> thresholds
array([0.35, 0.4 , 0.8 ])

 Examples using sklearn.metrics.precision_recall_curve

  Visualizations with Display Objects

  Precision-Recall


sklearn.metrics.precision_recall_fscore_support
sklearn.metrics.precision_recall_fscore_support(y_true, y_pred, *, beta=1.0, labels=None, pos_label=1, average=None, warn_for='precision', 'recall', 'f-score', sample_weight=None, zero_division='warn') [source]

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

y_true1d array-like, or label indicator array / sparse matrix

Ground truth (correct) target values.
y_pred1d array-like, or label indicator array / sparse matrix

Estimated targets as returned by a classifier.
betafloat, default=1.0

The strength of recall versus precision in the F-score.
labelsarray-like, default=None

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.
pos_labelstr or int, default=1

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.
average{‘binary’, ‘micro’, ‘macro’, ‘samples’,’weighted’}, default=None

If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data:
'binary':

Only report results for the class specified by pos_label. This is applicable only if targets (y_{true,pred}) are binary.
'micro':

Calculate metrics globally by counting the total true positives, false negatives and false positives.
'macro':

Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
'weighted':

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.
'samples':

Calculate metrics for each instance, and find their average (only meaningful for multilabel classification where this differs from accuracy_score).
warn_fortuple or set, for internal use

This determines which warnings will be made in the case that this function is being used to return only one of its metrics.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
zero_division“warn”, 0 or 1, default=”warn”

 Sets the value to return when there is a zero division:

 recall: when there are no positive labels precision: when there are no positive predictions f-score: both    If set to “warn”, this acts as 0, but warnings are also raised.    Returns

precisionfloat (if average is not None) or array of float, shape = [n_unique_labels]

recallfloat (if average is not None) or array of float, , shape = [n_unique_labels]

fbeta_scorefloat (if average is not None) or array of float, shape = [n_unique_labels]

supportNone (if average is not None) or array of int, shape = [n_unique_labels]

The number of occurrences of each label in y_true.     Notes 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. References
1
Wikipedia entry for the Precision and recall.
2
Wikipedia entry for the F1-score.
3
Discriminative Methods for Multi-labeled Classification Advances in Knowledge Discovery and Data Mining (2004), pp. 22-30 by Shantanu Godbole, Sunita Sarawagi.   Examples >>> import numpy as np
>>> from sklearn.metrics import precision_recall_fscore_support
>>> y_true = np.array(['cat', 'dog', 'pig', 'cat', 'dog', 'pig'])
>>> y_pred = np.array(['cat', 'pig', 'dog', 'cat', 'cat', 'dog'])
>>> precision_recall_fscore_support(y_true, y_pred, average='macro')
(0.22..., 0.33..., 0.26..., None)
>>> precision_recall_fscore_support(y_true, y_pred, average='micro')
(0.33..., 0.33..., 0.33..., None)
>>> precision_recall_fscore_support(y_true, y_pred, average='weighted')
(0.22..., 0.33..., 0.26..., None)
 It is possible to compute per-label precisions, recalls, F1-scores and supports instead of averaging: >>> precision_recall_fscore_support(y_true, y_pred, average=None,
... labels=['pig', 'dog', 'cat'])
(array([0.        , 0.        , 0.66...]),
 array([0., 0., 1.]), array([0. , 0. , 0.8]),
 array([2, 2, 2]))


sklearn.metrics.precision_score
sklearn.metrics.precision_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn') [source]

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

y_true1d array-like, or label indicator array / sparse matrix

Ground truth (correct) target values.
y_pred1d array-like, or label indicator array / sparse matrix

Estimated targets as returned by a classifier.
labelsarray-like, default=None

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.
pos_labelstr or int, default=1

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.
average{‘micro’, ‘macro’, ‘samples’, ‘weighted’, ‘binary’} default=’binary’

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:
'binary':

Only report results for the class specified by pos_label. This is applicable only if targets (y_{true,pred}) are binary.
'micro':

Calculate metrics globally by counting the total true positives, false negatives and false positives.
'macro':

Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
'weighted':

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.
'samples':

Calculate metrics for each instance, and find their average (only meaningful for multilabel classification where this differs from accuracy_score).
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
zero_division“warn”, 0 or 1, default=”warn”

Sets the value to return when there is a zero division. If set to “warn”, this acts as 0, but warnings are also raised.    Returns

precisionfloat (if average is not None) or array of float of shape

(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
precision_recall_fscore_support, multilabel_confusion_matrix

  Notes When true positive + false positive == 0, precision returns 0 and raises UndefinedMetricWarning. This behavior can be modified with zero_division. Examples >>> from sklearn.metrics import precision_score
>>> y_true = [0, 1, 2, 0, 1, 2]
>>> y_pred = [0, 2, 1, 0, 0, 1]
>>> precision_score(y_true, y_pred, average='macro')
0.22...
>>> precision_score(y_true, y_pred, average='micro')
0.33...
>>> precision_score(y_true, y_pred, average='weighted')
0.22...
>>> precision_score(y_true, y_pred, average=None)
array([0.66..., 0.        , 0.        ])
>>> y_pred = [0, 0, 0, 0, 0, 0]
>>> precision_score(y_true, y_pred, average=None)
array([0.33..., 0.        , 0.        ])
>>> precision_score(y_true, y_pred, average=None, zero_division=1)
array([0.33..., 1.        , 1.        ])

 Examples using sklearn.metrics.precision_score

  Probability Calibration curves

  Precision-Recall


sklearn.metrics.r2_score
sklearn.metrics.r2_score(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average') [source]

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

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

Ground truth (correct) target values.
y_predarray-like of shape (n_samples,) or (n_samples, n_outputs)

Estimated target values.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
multioutput{‘raw_values’, ‘uniform_average’, ‘variance_weighted’}, array-like of shape (n_outputs,) or None, default=’uniform_average’

Defines aggregating of multiple output scores. Array-like value defines weights used to average scores. Default is “uniform_average”.  ‘raw_values’ :

Returns a full set of scores in case of multioutput input.  ‘uniform_average’ :

Scores of all outputs are averaged with uniform weight.  ‘variance_weighted’ :

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’.     Returns

zfloat or ndarray of floats

The R^2 score or ndarray of scores if ‘multioutput’ is ‘raw_values’.     Notes 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. References
1
Wikipedia entry on the Coefficient of determination   Examples >>> from sklearn.metrics import r2_score
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> r2_score(y_true, y_pred)
0.948...
>>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
>>> y_pred = [[0, 2], [-1, 2], [8, -5]]
>>> r2_score(y_true, y_pred,
...          multioutput='variance_weighted')
0.938...
>>> y_true = [1, 2, 3]
>>> y_pred = [1, 2, 3]
>>> r2_score(y_true, y_pred)
1.0
>>> y_true = [1, 2, 3]
>>> y_pred = [2, 2, 2]
>>> r2_score(y_true, y_pred)
0.0
>>> y_true = [1, 2, 3]
>>> y_pred = [3, 2, 1]
>>> r2_score(y_true, y_pred)
-3.0

 Examples using sklearn.metrics.r2_score

  Non-negative least squares

  Linear Regression Example

  Lasso and Elastic Net for Sparse Signals

  Effect of transforming the targets in regression model


sklearn.metrics.rand_score
sklearn.metrics.rand_score(labels_true, labels_pred) [source]

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

labels_truearray-like of shape (n_samples,), dtype=integral

Ground truth class labels to be used as a reference.
labels_predarray-like of shape (n_samples,), dtype=integral

Cluster labels to evaluate.    Returns

RIfloat

Similarity score between 0.0 and 1.0, inclusive, 1.0 stands for perfect match.      See also
adjusted_rand_score

Adjusted Rand Score
adjusted_mutual_info_score

Adjusted Mutual Information    References Examples Perfectly matching labelings have a score of 1 even >>> from sklearn.metrics.cluster import rand_score
>>> rand_score([0, 0, 1, 1], [1, 1, 0, 0])
1.0
 Labelings that assign all classes members to the same clusters are complete but may not always be pure, hence penalized: >>> rand_score([0, 0, 1, 2], [0, 0, 1, 1])
0.83...