3.3. Metrics and scoring: quantifying the quality of predictions

There are 3 different APIs for evaluating the quality of a model’spredictions:

Finally, Dummy estimators are useful to get a baselinevalue of those metrics for random predictions.

See also

For “pairwise” metrics, between samples and not estimators orpredictions, see the Pairwise metrics, Affinities and Kernels section.

3.3.1. The scoring parameter: defining model evaluation rules

Model selection and evaluation using tools, such asmodel_selection.GridSearchCV andmodel_selection.cross_val_score, take a scoring parameter thatcontrols what metric they apply to the estimators evaluated.

3.3.1.1. Common cases: predefined values

For the most common use cases, you can designate a scorer object with thescoring parameter; the table below shows all possible values.All scorer objects follow the convention that higher return values are betterthan lower return values. Thus metrics which measure the distance betweenthe model and the data, like metrics.mean_squared_error, areavailable as neg_mean_squared_error which return the negated valueof the metric.

ScoringFunctionComment
Classification
‘accuracy’metrics.accuracy_score
‘balanced_accuracy’metrics.balanced_accuracy_score
‘average_precision’metrics.average_precision_score
‘neg_brier_score’metrics.brier_score_loss
‘f1’metrics.f1_scorefor binary targets
‘f1_micro’metrics.f1_scoremicro-averaged
‘f1_macro’metrics.f1_scoremacro-averaged
‘f1_weighted’metrics.f1_scoreweighted average
‘f1_samples’metrics.f1_scoreby multilabel sample
‘neg_log_loss’metrics.log_lossrequires predict_proba support
‘precision’ etc.metrics.precision_scoresuffixes apply as with ‘f1’
‘recall’ etc.metrics.recall_scoresuffixes apply as with ‘f1’
‘jaccard’ etc.metrics.jaccard_scoresuffixes apply as with ‘f1’
‘roc_auc’metrics.roc_auc_score
‘roc_auc_ovr’metrics.roc_auc_score
‘roc_auc_ovo’metrics.roc_auc_score
‘roc_auc_ovr_weighted’metrics.roc_auc_score
‘roc_auc_ovo_weighted’metrics.roc_auc_score
Clustering
‘adjusted_mutual_info_score’metrics.adjusted_mutual_info_score
‘adjusted_rand_score’metrics.adjusted_rand_score
‘completeness_score’metrics.completeness_score
‘fowlkes_mallows_score’metrics.fowlkes_mallows_score
‘homogeneity_score’metrics.homogeneity_score
‘mutual_info_score’metrics.mutual_info_score
‘normalized_mutual_info_score’metrics.normalized_mutual_info_score
‘v_measure_score’metrics.v_measure_score
Regression
‘explained_variance’metrics.explained_variance_score
‘max_error’metrics.max_error
‘neg_mean_absolute_error’metrics.mean_absolute_error
‘neg_mean_squared_error’metrics.mean_squared_error
‘neg_root_mean_squared_error’metrics.mean_squared_error
‘neg_mean_squared_log_error’metrics.mean_squared_log_error
‘neg_median_absolute_error’metrics.median_absolute_error
‘r2’metrics.r2_score
‘neg_mean_poisson_deviance’metrics.mean_poisson_deviance
‘neg_mean_gamma_deviance’metrics.mean_gamma_deviance

Usage examples:

>>>

  1. >>> from sklearn import svm, datasets
  2. >>> from sklearn.model_selection import cross_val_score
  3. >>> X, y = datasets.load_iris(return_X_y=True)
  4. >>> clf = svm.SVC(random_state=0)
  5. >>> cross_val_score(clf, X, y, cv=5, scoring='recall_macro')
  6. array([0.96..., 0.96..., 0.96..., 0.93..., 1. ])
  7. >>> model = svm.SVC()
  8. >>> cross_val_score(model, X, y, cv=5, scoring='wrong_choice')
  9. Traceback (most recent call last):
  10. ValueError: 'wrong_choice' is not a valid scoring value. Use sorted(sklearn.metrics.SCORERS.keys()) to get valid options.

Note

The values listed by the ValueError exception correspond to the functions measuringprediction accuracy described in the following sections.The scorer objects for those functions are stored in the dictionarysklearn.metrics.SCORERS.

3.3.1.2. Defining your scoring strategy from metric functions

The module sklearn.metrics also exposes a set of simple functionsmeasuring a prediction error given ground truth and prediction:

  • functions ending with _score return a value tomaximize, the higher the better.

  • functions ending with _error or _loss return avalue to minimize, the lower the better. When convertinginto a scorer object using make_scorer, setthe greater_is_better parameter to False (True by default; see theparameter description below).

Metrics available for various machine learning tasks are detailed in sectionsbelow.

Many metrics are not given names to be used as scoring values,sometimes because they require additional parameters, such asfbeta_score. In such cases, you need to generate an appropriatescoring object. The simplest way to generate a callable object for scoringis by using make_scorer. That function converts metricsinto callables that can be used for model evaluation.

One typical use case is to wrap an existing metric function from the librarywith non-default values for its parameters, such as the beta parameter forthe fbeta_score function:

>>>

  1. >>> from sklearn.metrics import fbeta_score, make_scorer
  2. >>> ftwo_scorer = make_scorer(fbeta_score, beta=2)
  3. >>> from sklearn.model_selection import GridSearchCV
  4. >>> from sklearn.svm import LinearSVC
  5. >>> grid = GridSearchCV(LinearSVC(), param_grid={'C': [1, 10]},
  6. ... scoring=ftwo_scorer, cv=5)

The second use case is to build a completely custom scorer objectfrom a simple python function using make_scorer, which cantake several parameters:

  • the python function you want to use (my_custom_loss_funcin the example below)

  • whether the python function returns a score (greater_is_better=True,the default) or a loss (greater_is_better=False). If a loss, the outputof the python function is negated by the scorer object, conforming tothe cross validation convention that scorers return higher values for better models.

  • for classification metrics only: whether the python function you provided requires continuous decisioncertainties (needs_threshold=True). The default value isFalse.

  • any additional parameters, such as beta or labels in f1_score.

Here is an example of building custom scorers, and of using thegreater_is_better parameter:

>>>

  1. >>> import numpy as np
  2. >>> def my_custom_loss_func(y_true, y_pred):
  3. ... diff = np.abs(y_true - y_pred).max()
  4. ... return np.log1p(diff)
  5. ...
  6. >>> # score will negate the return value of my_custom_loss_func,
  7. >>> # which will be np.log(2), 0.693, given the values for X
  8. >>> # and y defined below.
  9. >>> score = make_scorer(my_custom_loss_func, greater_is_better=False)
  10. >>> X = [[1], [1]]
  11. >>> y = [0, 1]
  12. >>> from sklearn.dummy import DummyClassifier
  13. >>> clf = DummyClassifier(strategy='most_frequent', random_state=0)
  14. >>> clf = clf.fit(X, y)
  15. >>> my_custom_loss_func(clf.predict(X), y)
  16. 0.69...
  17. >>> score(clf, X, y)
  18. -0.69...

3.3.1.3. Implementing your own scoring object

You can generate even more flexible model scorers by constructing your ownscoring object from scratch, without using the make_scorer factory.For a callable to be a scorer, it needs to meet the protocol specified bythe following two rules:

  • It can be called with parameters (estimator, X, y), where estimatoris the model that should be evaluated, X is validation data, and y isthe ground truth target for X (in the supervised case) or None (in theunsupervised case).

  • It returns a floating point number that quantifies theestimator prediction quality on X, with reference to y.Again, by convention higher numbers are better, so if your scorerreturns loss, that value should be negated.

Note

Using custom scorers in functions where n_jobs > 1

While defining the custom scoring function alongside the calling functionshould work out of the box with the default joblib backend (loky),importing it from another module will be a more robust approach and workindependently of the joblib backend.

For example, to use n_jobs greater than 1 in the example below,custom_scoring_function function is saved in a user-created module(custom_scorer_module.py) and imported:

>>>

  1. >>> from custom_scorer_module import custom_scoring_function
  2. >>> cross_val_score(model,
  3. ... X_train,
  4. ... y_train,
  5. ... scoring=make_scorer(custom_scoring_function, greater_is_better=False),
  6. ... cv=5,
  7. ... n_jobs=-1)

3.3.1.4. Using multiple metric evaluation

Scikit-learn also permits evaluation of multiple metrics in GridSearchCV,RandomizedSearchCV and cross_validate.

There are two ways to specify multiple scoring metrics for the scoringparameter:

    • As an iterable of string metrics::

>>>

  1. >>> scoring = ['accuracy', 'precision']
    • As a dict mapping the scorer name to the scoring function::

>>>

  1. >>> from sklearn.metrics import accuracy_score
  2. >>> from sklearn.metrics import make_scorer
  3. >>> scoring = {'accuracy': make_scorer(accuracy_score),
  4. ... 'prec': 'precision'}

Note that the dict values can either be scorer functions or one of thepredefined metric strings.

Currently only those scorer functions that return a single score can be passedinside the dict. Scorer functions that return multiple values are notpermitted and will require a wrapper to return a single metric:

>>>

  1. >>> from sklearn.model_selection import cross_validate
  2. >>> from sklearn.metrics import confusion_matrix
  3. >>> # A sample toy binary classification dataset
  4. >>> X, y = datasets.make_classification(n_classes=2, random_state=0)
  5. >>> svm = LinearSVC(random_state=0)
  6. >>> def tn(y_true, y_pred): return confusion_matrix(y_true, y_pred)[0, 0]
  7. >>> def fp(y_true, y_pred): return confusion_matrix(y_true, y_pred)[0, 1]
  8. >>> def fn(y_true, y_pred): return confusion_matrix(y_true, y_pred)[1, 0]
  9. >>> def tp(y_true, y_pred): return confusion_matrix(y_true, y_pred)[1, 1]
  10. >>> scoring = {'tp': make_scorer(tp), 'tn': make_scorer(tn),
  11. ... 'fp': make_scorer(fp), 'fn': make_scorer(fn)}
  12. >>> cv_results = cross_validate(svm.fit(X, y), X, y, cv=5, scoring=scoring)
  13. >>> # Getting the test set true positive scores
  14. >>> print(cv_results['test_tp'])
  15. [10 9 8 7 8]
  16. >>> # Getting the test set false negative scores
  17. >>> print(cv_results['test_fn'])
  18. [0 1 2 3 2]

3.3.2. Classification metrics

The sklearn.metrics module implements several loss, score, and utilityfunctions to measure classification performance.Some metrics might require probability estimates of the positive class,confidence values, or binary decisions values.Most implementations allow each sample to provide a weighted contributionto the overall score, through the sample_weight parameter.

Some of these are restricted to the binary classification case:

precision_recall_curve(y_true, probas_pred)Compute precision-recall pairs for different probability thresholds
roc_curve(y_true, y_score[, pos_label, …])Compute Receiver operating characteristic (ROC)

Others also work in the multiclass case:

balanced_accuracy_score(y_true, y_pred[, …])Compute the balanced accuracy
cohen_kappa_score(y1, y2[, labels, weights, …])Cohen’s kappa: a statistic that measures inter-annotator agreement.
confusion_matrix(y_true, y_pred[, labels, …])Compute confusion matrix to evaluate the accuracy of a classification.
hinge_loss(y_true, pred_decision[, labels, …])Average hinge loss (non-regularized)
matthews_corrcoef(y_true, y_pred[, …])Compute the Matthews correlation coefficient (MCC)
roc_auc_score(y_true, y_score[, average, …])Compute Area Under the Receiver Operating Characteristic Curve (ROC AUC) from prediction scores.

Some also work in the multilabel case:

accuracy_score(y_true, y_pred[, normalize, …])Accuracy classification score.
classification_report(y_true, y_pred[, …])Build a text report showing the main classification metrics
f1_score(y_true, y_pred[, labels, …])Compute the F1 score, also known as balanced F-score or F-measure
fbeta_score(y_true, y_pred, beta[, labels, …])Compute the F-beta score
hamming_loss(y_true, y_pred[, labels, …])Compute the average Hamming loss.
jaccard_score(y_true, y_pred[, labels, …])Jaccard similarity coefficient score
log_loss(y_true, y_pred[, eps, normalize, …])Log loss, aka logistic loss or cross-entropy loss.
multilabel_confusion_matrix(y_true, y_pred)Compute a confusion matrix for each class or sample
precision_recall_fscore_support(y_true, y_pred)Compute precision, recall, F-measure and support for each class
precision_score(y_true, y_pred[, labels, …])Compute the precision
recall_score(y_true, y_pred[, labels, …])Compute the recall
roc_auc_score(y_true, y_score[, average, …])Compute Area Under the Receiver Operating Characteristic Curve (ROC AUC) from prediction scores.
zero_one_loss(y_true, y_pred[, normalize, …])Zero-one classification loss.

And some work with binary and multilabel (but not multiclass) problems:

average_precision_score(y_true, y_score[, …])Compute average precision (AP) from prediction scores

In the following sub-sections, we will describe each of those functions,preceded by some notes on common API and metric definition.

3.3.2.1. From binary to multiclass and multilabel

Some metrics are essentially defined for binary classification tasks (e.g.f1_score, roc_auc_score). In these cases, by defaultonly the positive label is evaluated, assuming by default that the positiveclass is labelled 1 (though this may be configurable through thepos_label parameter).

In extending a binary metric to multiclass or multilabel problems, the datais treated as a collection of binary problems, one for each class.There are then a number of ways to average binary metric calculations acrossthe set of classes, each of which may be useful in some scenario.Where available, you should select among these using the average parameter.

  • "macro" simply calculates the mean of the binary metrics,giving equal weight to each class. In problems where infrequent classesare nonetheless important, macro-averaging may be a means of highlightingtheir performance. On the other hand, the assumption that all classes areequally important is often untrue, such that macro-averaging willover-emphasize the typically low performance on an infrequent class.

  • "weighted" accounts for class imbalance by computing the average ofbinary metrics in which each class’s score is weighted by its presence in thetrue data sample.

  • "micro" gives each sample-class pair an equal contribution to the overallmetric (except as a result of sample-weight). Rather than summing themetric per class, this sums the dividends and divisors that make up theper-class metrics to calculate an overall quotient.Micro-averaging may be preferred in multilabel settings, includingmulticlass classification where a majority class is to be ignored.

  • "samples" applies only to multilabel problems. It does not calculate aper-class measure, instead calculating the metric over the true and predictedclasses for each sample in the evaluation data, and returning their(sample_weight-weighted) average.

  • Selecting average=None will return an array with the score for eachclass.

While multiclass data is provided to the metric, like binary targets, as anarray of class labels, multilabel data is specified as an indicator matrix,in which cell [i, j] has value 1 if sample i has label j and value0 otherwise.

3.3.2.2. Accuracy score

The accuracy_score function computes theaccuracy, either the fraction(default) or the count (normalize=False) of correct predictions.

In multilabel classification, the function returns the subset accuracy. Ifthe entire set of predicted labels for a sample strictly match with the trueset of labels, then the subset accuracy is 1.0; otherwise it is 0.0.

If

3.3. Metrics and scoring: quantifying the quality of predictions - 图1 is the predicted value ofthe3.3. Metrics and scoring: quantifying the quality of predictions - 图2-th sample and3.3. Metrics and scoring: quantifying the quality of predictions - 图3 is the corresponding true value,then the fraction of correct predictions over3.3. Metrics and scoring: quantifying the quality of predictions - 图4 isdefined as

3.3. Metrics and scoring: quantifying the quality of predictions - 图5

where

3.3. Metrics and scoring: quantifying the quality of predictions - 图6 is the indicator function.

>>>

  1. >>> import numpy as np
  2. >>> from sklearn.metrics import accuracy_score
  3. >>> y_pred = [0, 2, 1, 3]
  4. >>> y_true = [0, 1, 2, 3]
  5. >>> accuracy_score(y_true, y_pred)
  6. 0.5
  7. >>> accuracy_score(y_true, y_pred, normalize=False)
  8. 2

In the multilabel case with binary label indicators:

>>>

  1. >>> accuracy_score(np.array([[0, 1], [1, 1]]), np.ones((2, 2)))
  2. 0.5

Example:

3.3.2.3. Balanced accuracy score

The balanced_accuracy_score function computes the balanced accuracy, which avoids inflatedperformance estimates on imbalanced datasets. It is the macro-average of recallscores per class or, equivalently, raw accuracy where each sample is weightedaccording to the inverse prevalence of its true class.Thus for balanced datasets, the score is equal to accuracy.

In the binary case, balanced accuracy is equal to the arithmetic mean ofsensitivity(true positive rate) and specificity (true negativerate), or the area under the ROC curve with binary predictions rather thanscores.

If the classifier performs equally well on either class, this term reduces tothe conventional accuracy (i.e., the number of correct predictions divided bythe total number of predictions).

In contrast, if the conventional accuracy is above chance only because theclassifier takes advantage of an imbalanced test set, then the balancedaccuracy, as appropriate, will drop to

3.3. Metrics and scoring: quantifying the quality of predictions - 图7.

The score ranges from 0 to 1, or when adjusted=True is used, it rescaled tothe range

3.3. Metrics and scoring: quantifying the quality of predictions - 图8 to 1, inclusive, withperformance at random scoring 0.

If

3.3. Metrics and scoring: quantifying the quality of predictions - 图9 is the true value of the3.3. Metrics and scoring: quantifying the quality of predictions - 图10-th sample, and3.3. Metrics and scoring: quantifying the quality of predictions - 图11is the corresponding sample weight, then we adjust the sample weight to:

3.3. Metrics and scoring: quantifying the quality of predictions - 图12

where

3.3. Metrics and scoring: quantifying the quality of predictions - 图13 is the indicator function.Given predicted3.3. Metrics and scoring: quantifying the quality of predictions - 图14 for sample3.3. Metrics and scoring: quantifying the quality of predictions - 图15, balanced accuracy isdefined as:

3.3. Metrics and scoring: quantifying the quality of predictions - 图16

With adjusted=True, balanced accuracy reports the relative increase from

3.3. Metrics and scoring: quantifying the quality of predictions - 图17. In the binary case, this is also known asYouden’s J statistic,or informedness.

Note

The multiclass definition here seems the most reasonable extension of themetric used in binary classification, though there is no certain consensusin the literature:

3.3. Metrics and scoring: quantifying the quality of predictions - 图18 and perfect predictions have a score of3.3. Metrics and scoring: quantifying the quality of predictions - 图19..

  • Class balanced accuracy as described in [Mosley2013]: the minimum between the precisionand the recall for each class is computed. Those values are then averaged over the totalnumber of classes to get the balanced accuracy.

  • Balanced Accuracy as described in [Urbanowicz2015]: the average of sensitivity and specificityis computed for each class and then averaged over total number of classes.

References:

3.3.2.4. Cohen’s kappa

The function cohen_kappa_score computes Cohen’s kappa statistic.This measure is intended to compare labelings by different human annotators,not a classifier versus a ground truth.

The kappa score (see docstring) is a number between -1 and 1.Scores above .8 are generally considered good agreement;zero or lower means no agreement (practically random labels).

Kappa scores can be computed for binary or multiclass problems,but not for multilabel problems (except by manually computing a per-label score)and not for more than two annotators.

>>>

  1. >>> from sklearn.metrics import cohen_kappa_score
  2. >>> y_true = [2, 0, 2, 2, 0, 1]
  3. >>> y_pred = [0, 0, 2, 2, 0, 2]
  4. >>> cohen_kappa_score(y_true, y_pred)
  5. 0.4285714285714286

3.3.2.5. Confusion matrix

The confusion_matrix function evaluatesclassification accuracy by computing the confusion matrixwith each row corresponding to the true class<https://en.wikipedia.org/wiki/Confusion_matrix>`_.(Wikipedia and other references may use different convention for axes.)

By definition, entry

3.3. Metrics and scoring: quantifying the quality of predictions - 图20 in a confusion matrix isthe number of observations actually in group3.3. Metrics and scoring: quantifying the quality of predictions - 图21, butpredicted to be in group3.3. Metrics and scoring: quantifying the quality of predictions - 图22. Here is an example:

>>>

  1. >>> from sklearn.metrics import confusion_matrix
  2. >>> y_true = [2, 0, 2, 2, 0, 1]
  3. >>> y_pred = [0, 0, 2, 2, 0, 2]
  4. >>> confusion_matrix(y_true, y_pred)
  5. array([[2, 0, 0],
  6. [0, 0, 1],
  7. [1, 0, 2]])

plot_confusion_matrix can be used to visually represent a confusionmatrix as shown in theConfusion matrixexample, which creates the following figure:../_images/sphx_glr_plot_confusion_matrix_0011.pngThe parameter normalize allows to report ratios instead of counts. Theconfusion matrix can be normalized in 3 different ways: 'pred', 'true',and 'all' which will divide the counts by the sum of each columns, rows, orthe entire matrix, respectively.

>>>

  1. >>> y_true = [0, 0, 0, 1, 1, 1, 1, 1]
  2. >>> y_pred = [0, 1, 0, 1, 0, 1, 0, 1]
  3. >>> confusion_matrix(y_true, y_pred, normalize='all')
  4. array([[0.25 , 0.125],
  5. [0.25 , 0.375]])

For binary problems, we can get counts of true negatives, false positives,false negatives and true positives as follows:

>>>

  1. >>> y_true = [0, 0, 0, 1, 1, 1, 1, 1]
  2. >>> y_pred = [0, 1, 0, 1, 0, 1, 0, 1]
  3. >>> tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
  4. >>> tn, fp, fn, tp
  5. (2, 1, 2, 3)

Example:

3.3.2.6. Classification report

The classification_report function builds a text report showing themain classification metrics. Here is a small example with custom target_namesand inferred labels:

>>>

  1. >>> from sklearn.metrics import classification_report
  2. >>> y_true = [0, 1, 2, 2, 0]
  3. >>> y_pred = [0, 0, 2, 1, 0]
  4. >>> target_names = ['class 0', 'class 1', 'class 2']
  5. >>> print(classification_report(y_true, y_pred, target_names=target_names))
  6. precision recall f1-score support
  7.  
  8. class 0 0.67 1.00 0.80 2
  9. class 1 0.00 0.00 0.00 1
  10. class 2 1.00 0.50 0.67 2
  11.  
  12. accuracy 0.60 5
  13. macro avg 0.56 0.50 0.49 5
  14. weighted avg 0.67 0.60 0.59 5

Example:

3.3.2.7. Hamming loss

The hamming_loss computes the average Hamming loss or Hammingdistance between two setsof samples.

If

3.3. Metrics and scoring: quantifying the quality of predictions - 图24 is the predicted value for the3.3. Metrics and scoring: quantifying the quality of predictions - 图25-th label ofa given sample,3.3. Metrics and scoring: quantifying the quality of predictions - 图26 is the corresponding true value, and3.3. Metrics and scoring: quantifying the quality of predictions - 图27 is the number of classes or labels, then theHamming loss3.3. Metrics and scoring: quantifying the quality of predictions - 图28 between two samples is defined as:

3.3. Metrics and scoring: quantifying the quality of predictions - 图29

where

3.3. Metrics and scoring: quantifying the quality of predictions - 图30 is the indicator function.

>>>

  1. >>> from sklearn.metrics import hamming_loss
  2. >>> y_pred = [1, 2, 3, 4]
  3. >>> y_true = [2, 2, 3, 4]
  4. >>> hamming_loss(y_true, y_pred)
  5. 0.25

In the multilabel case with binary label indicators:

>>>

  1. >>> hamming_loss(np.array([[0, 1], [1, 1]]), np.zeros((2, 2)))
  2. 0.75

Note

In multiclass classification, the Hamming loss corresponds to the Hammingdistance between y_true and y_pred which is similar to theZero one loss function. However, while zero-one loss penalizesprediction sets that do not strictly match true sets, the Hamming losspenalizes individual labels. Thus the Hamming loss, upper bounded by the zero-oneloss, is always between zero and one, inclusive; and predicting a proper subsetor superset of the true labels will give a Hamming loss betweenzero and one, exclusive.

3.3.2.8. Precision, recall and F-measures

Intuitively, precision is the abilityof the classifier not to label as positive a sample that is negative, andrecall is theability of the classifier to find all the positive samples.

The F-measure(

3.3. Metrics and scoring: quantifying the quality of predictions - 图31 and3.3. Metrics and scoring: quantifying the quality of predictions - 图32 measures) can be interpreted as a weightedharmonic mean of the precision and recall. A3.3. Metrics and scoring: quantifying the quality of predictions - 图33 measure reaches its best value at 1 and its worst score at 0.With3.3. Metrics and scoring: quantifying the quality of predictions - 图34,3.3. Metrics and scoring: quantifying the quality of predictions - 图35 and3.3. Metrics and scoring: quantifying the quality of predictions - 图36 are equivalent, and the recall and the precision are equally important.

The precision_recall_curve computes a precision-recall curvefrom the ground truth label and a score given by the classifierby varying a decision threshold.

The average_precision_score function computes theaverage precision(AP) from prediction scores. The value is between 0 and 1 and higher is better.AP is defined as

3.3. Metrics and scoring: quantifying the quality of predictions - 图37

where

3.3. Metrics and scoring: quantifying the quality of predictions - 图38 and3.3. Metrics and scoring: quantifying the quality of predictions - 图39 are the precision and recall at thenth threshold. With random predictions, the AP is the fraction of positivesamples.

References [Manning2008] and [Everingham2010] present alternative variants ofAP that interpolate the precision-recall curve. Currently,average_precision_score does not implement any interpolated variant.References [Davis2006] and [Flach2015] describe why a linear interpolation ofpoints on the precision-recall curve provides an overly-optimistic measure ofclassifier performance. This linear interpolation is used when computing areaunder the curve with the trapezoidal rule in auc.

Several functions allow you to analyze the precision, recall and F-measuresscore:

average_precision_score(y_true, y_score[, …])Compute average precision (AP) from prediction scores
f1_score(y_true, y_pred[, labels, …])Compute the F1 score, also known as balanced F-score or F-measure
fbeta_score(y_true, y_pred, beta[, labels, …])Compute the F-beta score
precision_recall_curve(y_true, probas_pred)Compute precision-recall pairs for different probability thresholds
precision_recall_fscore_support(y_true, y_pred)Compute precision, recall, F-measure and support for each class
precision_score(y_true, y_pred[, labels, …])Compute the precision
recall_score(y_true, y_pred[, labels, …])Compute the recall

Note that the precision_recall_curve function is restricted to thebinary case. The average_precision_score function works only inbinary classification and multilabel indicator format. Theplot_precision_recall_curve function plots the precision recall asfollows.../_images/sphx_glr_plot_precision_recall_0011.png

Examples:

References:

3.3.2.8.1. Binary classification

In a binary classification task, the terms ‘’positive’’ and ‘’negative’’ referto the classifier’s prediction, and the terms ‘’true’’ and ‘’false’’ refer towhether that prediction corresponds to the external judgment (sometimes knownas the ‘’observation’’). Given these definitions, we can formulate thefollowing table:

Actual class (observation)
Predicted class(expectation)tp (true positive)Correct resultfp (false positive)Unexpected result
fn (false negative)Missing resulttn (true negative)Correct absence of result

In this context, we can define the notions of precision, recall and F-measure:

3.3. Metrics and scoring: quantifying the quality of predictions - 图41

3.3. Metrics and scoring: quantifying the quality of predictions - 图42

3.3. Metrics and scoring: quantifying the quality of predictions - 图43

Here are some small examples in binary classification:

>>>

  1. >>> from sklearn import metrics
  2. >>> y_pred = [0, 1, 0, 0]
  3. >>> y_true = [0, 1, 0, 1]
  4. >>> metrics.precision_score(y_true, y_pred)
  5. 1.0
  6. >>> metrics.recall_score(y_true, y_pred)
  7. 0.5
  8. >>> metrics.f1_score(y_true, y_pred)
  9. 0.66...
  10. >>> metrics.fbeta_score(y_true, y_pred, beta=0.5)
  11. 0.83...
  12. >>> metrics.fbeta_score(y_true, y_pred, beta=1)
  13. 0.66...
  14. >>> metrics.fbeta_score(y_true, y_pred, beta=2)
  15. 0.55...
  16. >>> metrics.precision_recall_fscore_support(y_true, y_pred, beta=0.5)
  17. (array([0.66..., 1. ]), array([1. , 0.5]), array([0.71..., 0.83...]), array([2, 2]))
  18.  
  19.  
  20. >>> import numpy as np
  21. >>> from sklearn.metrics import precision_recall_curve
  22. >>> from sklearn.metrics import average_precision_score
  23. >>> y_true = np.array([0, 0, 1, 1])
  24. >>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
  25. >>> precision, recall, threshold = precision_recall_curve(y_true, y_scores)
  26. >>> precision
  27. array([0.66..., 0.5 , 1. , 1. ])
  28. >>> recall
  29. array([1. , 0.5, 0.5, 0. ])
  30. >>> threshold
  31. array([0.35, 0.4 , 0.8 ])
  32. >>> average_precision_score(y_true, y_scores)
  33. 0.83...

3.3.2.8.2. Multiclass and multilabel classification

In multiclass and multilabel classification task, the notions of precision,recall, and F-measures can be applied to each label independently.There are a few ways to combine results across labels,specified by the average argument to theaverage_precision_score (multilabel only), f1_score,fbeta_score, precision_recall_fscore_support,precision_score and recall_score functions, as describedabove. Note that if all labels are included, “micro”-averagingin a multiclass setting will produce precision, recall and

3.3. Metrics and scoring: quantifying the quality of predictions - 图44that are all identical to accuracy. Also note that “weighted” averaging mayproduce an F-score that is not between precision and recall.

To make this more explicit, consider the following notation:

3.3. Metrics and scoring: quantifying the quality of predictions - 图45 the set of predicted3.3. Metrics and scoring: quantifying the quality of predictions - 图46 pairs

3.3. Metrics and scoring: quantifying the quality of predictions - 图47 the set of true3.3. Metrics and scoring: quantifying the quality of predictions - 图48 pairs

3.3. Metrics and scoring: quantifying the quality of predictions - 图49 the set of labels

3.3. Metrics and scoring: quantifying the quality of predictions - 图50 the set of samples

3.3. Metrics and scoring: quantifying the quality of predictions - 图51 the subset of3.3. Metrics and scoring: quantifying the quality of predictions - 图52 with sample3.3. Metrics and scoring: quantifying the quality of predictions - 图53,i.e.3.3. Metrics and scoring: quantifying the quality of predictions - 图54

3.3. Metrics and scoring: quantifying the quality of predictions - 图55 the subset of3.3. Metrics and scoring: quantifying the quality of predictions - 图56 with label3.3. Metrics and scoring: quantifying the quality of predictions - 图57

  • similarly,

3.3. Metrics and scoring: quantifying the quality of predictions - 图58 and3.3. Metrics and scoring: quantifying the quality of predictions - 图59 are subsets of3.3. Metrics and scoring: quantifying the quality of predictions - 图60

3.3. Metrics and scoring: quantifying the quality of predictions - 图61 for somesets3.3. Metrics and scoring: quantifying the quality of predictions - 图62 and3.3. Metrics and scoring: quantifying the quality of predictions - 图63

3.3. Metrics and scoring: quantifying the quality of predictions - 图64(Conventions vary on handling3.3. Metrics and scoring: quantifying the quality of predictions - 图65; this implementation uses3.3. Metrics and scoring: quantifying the quality of predictions - 图66, and similar for3.3. Metrics and scoring: quantifying the quality of predictions - 图67.)

3.3. Metrics and scoring: quantifying the quality of predictions - 图68

Then the metrics are defined as:

averagePrecisionRecallF_beta
"micro"3.3. Metrics and scoring: quantifying the quality of predictions - 图693.3. Metrics and scoring: quantifying the quality of predictions - 图703.3. Metrics and scoring: quantifying the quality of predictions - 图71
"samples"3.3. Metrics and scoring: quantifying the quality of predictions - 图723.3. Metrics and scoring: quantifying the quality of predictions - 图733.3. Metrics and scoring: quantifying the quality of predictions - 图74
"macro"3.3. Metrics and scoring: quantifying the quality of predictions - 图753.3. Metrics and scoring: quantifying the quality of predictions - 图763.3. Metrics and scoring: quantifying the quality of predictions - 图77
"weighted"3.3. Metrics and scoring: quantifying the quality of predictions - 图783.3. Metrics and scoring: quantifying the quality of predictions - 图793.3. Metrics and scoring: quantifying the quality of predictions - 图80
None3.3. Metrics and scoring: quantifying the quality of predictions - 图813.3. Metrics and scoring: quantifying the quality of predictions - 图823.3. Metrics and scoring: quantifying the quality of predictions - 图83

>>>

  1. >>> from sklearn import metrics
  2. >>> y_true = [0, 1, 2, 0, 1, 2]
  3. >>> y_pred = [0, 2, 1, 0, 0, 1]
  4. >>> metrics.precision_score(y_true, y_pred, average='macro')
  5. 0.22...
  6. >>> metrics.recall_score(y_true, y_pred, average='micro')
  7. 0.33...
  8. >>> metrics.f1_score(y_true, y_pred, average='weighted')
  9. 0.26...
  10. >>> metrics.fbeta_score(y_true, y_pred, average='macro', beta=0.5)
  11. 0.23...
  12. >>> metrics.precision_recall_fscore_support(y_true, y_pred, beta=0.5, average=None)
  13. (array([0.66..., 0. , 0. ]), array([1., 0., 0.]), array([0.71..., 0. , 0. ]), array([2, 2, 2]...))

For multiclass classification with a “negative class”, it is possible to exclude some labels:

>>>

  1. >>> metrics.recall_score(y_true, y_pred, labels=[1, 2], average='micro')
  2. ... # excluding 0, no labels were correctly recalled
  3. 0.0

Similarly, labels not present in the data sample may be accounted for in macro-averaging.

>>>

  1. >>> metrics.precision_score(y_true, y_pred, labels=[0, 1, 2, 3], average='macro')
  2. 0.166...

3.3.2.9. Jaccard similarity coefficient score

The jaccard_score function computes the average of Jaccard similaritycoefficients, also called theJaccard index, between pairs of label sets.

The Jaccard similarity coefficient of the

3.3. Metrics and scoring: quantifying the quality of predictions - 图84-th samples,with a ground truth label set3.3. Metrics and scoring: quantifying the quality of predictions - 图85 and predicted label set3.3. Metrics and scoring: quantifying the quality of predictions - 图86, is defined as

3.3. Metrics and scoring: quantifying the quality of predictions - 图87

jaccard_score works like precision_recall_fscore_support as anaively set-wise measure applying natively to binary targets, and extended toapply to multilabel and multiclass through the use of average (seeabove).

In the binary case:

>>>

  1. >>> import numpy as np
  2. >>> from sklearn.metrics import jaccard_score
  3. >>> y_true = np.array([[0, 1, 1],
  4. ... [1, 1, 0]])
  5. >>> y_pred = np.array([[1, 1, 1],
  6. ... [1, 0, 0]])
  7. >>> jaccard_score(y_true[0], y_pred[0])
  8. 0.6666...

In the multilabel case with binary label indicators:

>>>

  1. >>> jaccard_score(y_true, y_pred, average='samples')
  2. 0.5833...
  3. >>> jaccard_score(y_true, y_pred, average='macro')
  4. 0.6666...
  5. >>> jaccard_score(y_true, y_pred, average=None)
  6. array([0.5, 0.5, 1. ])

Multiclass problems are binarized and treated like the correspondingmultilabel problem:

>>>

  1. >>> y_pred = [0, 2, 1, 2]
  2. >>> y_true = [0, 1, 2, 2]
  3. >>> jaccard_score(y_true, y_pred, average=None)
  4. array([1. , 0. , 0.33...])
  5. >>> jaccard_score(y_true, y_pred, average='macro')
  6. 0.44...
  7. >>> jaccard_score(y_true, y_pred, average='micro')
  8. 0.33...

3.3.2.10. Hinge loss

The hinge_loss function computes the average distance betweenthe model and the data usinghinge loss, a one-sided metricthat considers only prediction errors. (Hingeloss is used in maximal margin classifiers such as support vector machines.)

If the labels are encoded with +1 and -1,

3.3. Metrics and scoring: quantifying the quality of predictions - 图88: is the truevalue, and3.3. Metrics and scoring: quantifying the quality of predictions - 图89 is the predicted decisions as output bydecision_function, then the hinge loss is defined as:

3.3. Metrics and scoring: quantifying the quality of predictions - 图90

If there are more than two labels, hinge_loss uses a multiclass variantdue to Crammer & Singer.Here isthe paper describing it.

If

3.3. Metrics and scoring: quantifying the quality of predictions - 图91 is the predicted decision for true label and3.3. Metrics and scoring: quantifying the quality of predictions - 图92 is themaximum of the predicted decisions for all other labels, where predicteddecisions are output by decision function, then multiclass hinge loss is definedby:

3.3. Metrics and scoring: quantifying the quality of predictions - 图93

Here a small example demonstrating the use of the hinge_loss functionwith a svm classifier in a binary class problem:

>>>

  1. >>> from sklearn import svm
  2. >>> from sklearn.metrics import hinge_loss
  3. >>> X = [[0], [1]]
  4. >>> y = [-1, 1]
  5. >>> est = svm.LinearSVC(random_state=0)
  6. >>> est.fit(X, y)
  7. LinearSVC(random_state=0)
  8. >>> pred_decision = est.decision_function([[-2], [3], [0.5]])
  9. >>> pred_decision
  10. array([-2.18..., 2.36..., 0.09...])
  11. >>> hinge_loss([-1, 1, 1], pred_decision)
  12. 0.3...

Here is an example demonstrating the use of the hinge_loss functionwith a svm classifier in a multiclass problem:

>>>

  1. >>> X = np.array([[0], [1], [2], [3]])
  2. >>> Y = np.array([0, 1, 2, 3])
  3. >>> labels = np.array([0, 1, 2, 3])
  4. >>> est = svm.LinearSVC()
  5. >>> est.fit(X, Y)
  6. LinearSVC()
  7. >>> pred_decision = est.decision_function([[-1], [2], [3]])
  8. >>> y_true = [0, 2, 3]
  9. >>> hinge_loss(y_true, pred_decision, labels)
  10. 0.56...

3.3.2.11. Log loss

Log loss, also called logistic regression loss orcross-entropy loss, is defined on probability estimates. It iscommonly used in (multinomial) logistic regression and neural networks, as wellas in some variants of expectation-maximization, and can be used to evaluate theprobability outputs (predict_proba) of a classifier instead of itsdiscrete predictions.

For binary classification with a true label

3.3. Metrics and scoring: quantifying the quality of predictions - 图94and a probability estimate3.3. Metrics and scoring: quantifying the quality of predictions - 图95,the log loss per sample is the negative log-likelihoodof the classifier given the true label:

3.3. Metrics and scoring: quantifying the quality of predictions - 图96

This extends to the multiclass case as follows.Let the true labels for a set of samplesbe encoded as a 1-of-K binary indicator matrix

3.3. Metrics and scoring: quantifying the quality of predictions - 图97,i.e.,3.3. Metrics and scoring: quantifying the quality of predictions - 图98 if sample3.3. Metrics and scoring: quantifying the quality of predictions - 图99 has label3.3. Metrics and scoring: quantifying the quality of predictions - 图100taken from a set of3.3. Metrics and scoring: quantifying the quality of predictions - 图101 labels.Let3.3. Metrics and scoring: quantifying the quality of predictions - 图102 be a matrix of probability estimates,with3.3. Metrics and scoring: quantifying the quality of predictions - 图103.Then the log loss of the whole set is

3.3. Metrics and scoring: quantifying the quality of predictions - 图104

To see how this generalizes the binary log loss given above,note that in the binary case,

3.3. Metrics and scoring: quantifying the quality of predictions - 图105 and3.3. Metrics and scoring: quantifying the quality of predictions - 图106,so expanding the inner sum over3.3. Metrics and scoring: quantifying the quality of predictions - 图107gives the binary log loss.

The log_loss function computes log loss given a list of ground-truthlabels and a probability matrix, as returned by an estimator’s predict_probamethod.

>>>

  1. >>> from sklearn.metrics import log_loss
  2. >>> y_true = [0, 0, 1, 1]
  3. >>> y_pred = [[.9, .1], [.8, .2], [.3, .7], [.01, .99]]
  4. >>> log_loss(y_true, y_pred)
  5. 0.1738...

The first [.9, .1] in y_pred denotes 90% probability that the firstsample has label 0. The log loss is non-negative.

3.3.2.12. Matthews correlation coefficient

The matthews_corrcoef function computes theMatthew’s correlation coefficient (MCC)for binary classes. Quoting Wikipedia:

“The Matthews correlation coefficient is used in machine learning as ameasure of the quality of binary (two-class) classifications. It takesinto account true and false positives and negatives and is generallyregarded as a balanced measure which can be used even if the classes areof very different sizes. The MCC is in essence a correlation coefficientvalue between -1 and +1. A coefficient of +1 represents a perfectprediction, 0 an average random prediction and -1 an inverse prediction.The statistic is also known as the phi coefficient.”

In the binary (two-class) case,

3.3. Metrics and scoring: quantifying the quality of predictions - 图108,3.3. Metrics and scoring: quantifying the quality of predictions - 图109,3.3. Metrics and scoring: quantifying the quality of predictions - 图110 and3.3. Metrics and scoring: quantifying the quality of predictions - 图111 are respectively the number of true positives, true negatives, falsepositives and false negatives, the MCC is defined as

3.3. Metrics and scoring: quantifying the quality of predictions - 图112

In the multiclass case, the Matthews correlation coefficient can be defined in terms of aconfusion_matrix

3.3. Metrics and scoring: quantifying the quality of predictions - 图113 for3.3. Metrics and scoring: quantifying the quality of predictions - 图114 classes. To simplify thedefinition consider the following intermediate variables:

3.3. Metrics and scoring: quantifying the quality of predictions - 图115 the number of times class3.3. Metrics and scoring: quantifying the quality of predictions - 图116 truly occurred,

3.3. Metrics and scoring: quantifying the quality of predictions - 图117 the number of times class3.3. Metrics and scoring: quantifying the quality of predictions - 图118 was predicted,

3.3. Metrics and scoring: quantifying the quality of predictions - 图119 the total number of samples correctly predicted,

3.3. Metrics and scoring: quantifying the quality of predictions - 图120 the total number of samples.

Then the multiclass MCC is defined as:

3.3. Metrics and scoring: quantifying the quality of predictions - 图121

When there are more than two labels, the value of the MCC will no longer rangebetween -1 and +1. Instead the minimum value will be somewhere between -1 and 0depending on the number and distribution of ground true labels. The maximumvalue is always +1.

Here is a small example illustrating the usage of the matthews_corrcoeffunction:

>>>

  1. >>> from sklearn.metrics import matthews_corrcoef
  2. >>> y_true = [+1, +1, +1, -1]
  3. >>> y_pred = [+1, -1, +1, +1]
  4. >>> matthews_corrcoef(y_true, y_pred)
  5. -0.33...

3.3.2.13. Multi-label confusion matrix

The multilabel_confusion_matrix function computes class-wise (default)or sample-wise (samplewise=True) multilabel confusion matrix to evaluatethe accuracy of a classification. multilabel_confusion_matrix also treatsmulticlass data as if it were multilabel, as this is a transformation commonlyapplied to evaluate multiclass problems with binary classification metrics(such as precision, recall, etc.).

When calculating class-wise multilabel confusion matrix

3.3. Metrics and scoring: quantifying the quality of predictions - 图122, thecount of true negatives for class3.3. Metrics and scoring: quantifying the quality of predictions - 图123 is3.3. Metrics and scoring: quantifying the quality of predictions - 图124, falsenegatives is3.3. Metrics and scoring: quantifying the quality of predictions - 图125, true positives is3.3. Metrics and scoring: quantifying the quality of predictions - 图126and false positives is3.3. Metrics and scoring: quantifying the quality of predictions - 图127.

Here is an example demonstrating the use of themultilabel_confusion_matrix function withmultilabel indicator matrix input:

>>>

  1. >>> import numpy as np
  2. >>> from sklearn.metrics import multilabel_confusion_matrix
  3. >>> y_true = np.array([[1, 0, 1],
  4. ... [0, 1, 0]])
  5. >>> y_pred = np.array([[1, 0, 0],
  6. ... [0, 1, 1]])
  7. >>> multilabel_confusion_matrix(y_true, y_pred)
  8. array([[[1, 0],
  9. [0, 1]],
  10.  
  11. [[1, 0],
  12. [0, 1]],
  13.  
  14. [[0, 1],
  15. [1, 0]]])

Or a confusion matrix can be constructed for each sample’s labels:

>>>

  1. >>> multilabel_confusion_matrix(y_true, y_pred, samplewise=True)
  2. array([[[1, 0],
  3. [1, 1]],
  4. <BLANKLINE>
  5. [[1, 1],
  6. [0, 1]]])

Here is an example demonstrating the use of themultilabel_confusion_matrix function withmulticlass input:

>>>

  1. >>> y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
  2. >>> y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
  3. >>> multilabel_confusion_matrix(y_true, y_pred,
  4. ... labels=["ant", "bird", "cat"])
  5. array([[[3, 1],
  6. [0, 2]],
  7.  
  8. [[5, 0],
  9. [1, 0]],
  10.  
  11. [[2, 1],
  12. [1, 2]]])

Here are some examples demonstrating the use of themultilabel_confusion_matrix function to calculate recall(or sensitivity), specificity, fall out and miss rate for each class in aproblem with multilabel indicator matrix input.

Calculatingrecall(also called the true positive rate or the sensitivity) for each class:

>>>

  1. >>> y_true = np.array([[0, 0, 1],
  2. ... [0, 1, 0],
  3. ... [1, 1, 0]])
  4. >>> y_pred = np.array([[0, 1, 0],
  5. ... [0, 0, 1],
  6. ... [1, 1, 0]])
  7. >>> mcm = multilabel_confusion_matrix(y_true, y_pred)
  8. >>> tn = mcm[:, 0, 0]
  9. >>> tp = mcm[:, 1, 1]
  10. >>> fn = mcm[:, 1, 0]
  11. >>> fp = mcm[:, 0, 1]
  12. >>> tp / (tp + fn)
  13. array([1. , 0.5, 0. ])

Calculatingspecificity(also called the true negative rate) for each class:

>>>

  1. >>> tn / (tn + fp)
  2. array([1. , 0. , 0.5])

Calculating fall out(also called the false positive rate) for each class:

>>>

  1. >>> fp / (fp + tn)
  2. array([0. , 1. , 0.5])

Calculating miss rate(also called the false negative rate) for each class:

>>>

  1. >>> fn / (fn + tp)
  2. array([0. , 0.5, 1. ])

3.3.2.14. Receiver operating characteristic (ROC)

The function roc_curve computes thereceiver operating characteristic curve, or ROC curve.Quoting Wikipedia :

“A receiver operating characteristic (ROC), or simply ROC curve, is agraphical plot which illustrates the performance of a binary classifiersystem as its discrimination threshold is varied. It is created by plottingthe fraction of true positives out of the positives (TPR = true positiverate) vs. the fraction of false positives out of the negatives (FPR = falsepositive rate), at various threshold settings. TPR is also known assensitivity, and FPR is one minus the specificity or true negative rate.”

This function requires the true binaryvalue and the target scores, which can either be probability estimates of thepositive class, confidence values, or binary decisions.Here is a small example of how to use the roc_curve function:

>>>

  1. >>> import numpy as np
  2. >>> from sklearn.metrics import roc_curve
  3. >>> y = np.array([1, 1, 2, 2])
  4. >>> scores = np.array([0.1, 0.4, 0.35, 0.8])
  5. >>> fpr, tpr, thresholds = roc_curve(y, scores, pos_label=2)
  6. >>> fpr
  7. array([0. , 0. , 0.5, 0.5, 1. ])
  8. >>> tpr
  9. array([0. , 0.5, 0.5, 1. , 1. ])
  10. >>> thresholds
  11. array([1.8 , 0.8 , 0.4 , 0.35, 0.1 ])

This figure shows an example of such an ROC curve:../_images/sphx_glr_plot_roc_0011.pngThe roc_auc_score function computes the area under the receiveroperating characteristic (ROC) curve, which is also denoted byAUC or AUROC. By computing thearea under the roc curve, the curve information is summarized in one number.For more information see the Wikipedia article on AUC.

>>>

  1. >>> import numpy as np
  2. >>> from sklearn.metrics import roc_auc_score
  3. >>> y_true = np.array([0, 0, 1, 1])
  4. >>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
  5. >>> roc_auc_score(y_true, y_scores)
  6. 0.75

In multi-label classification, the roc_auc_score function isextended by averaging over the labels as above.

Compared to metrics such as the subset accuracy, the Hamming loss, or theF1 score, ROC doesn’t require optimizing a threshold for each label.

The roc_auc_score function can also be used in multi-classclassification. Two averaging strategies are currently supported: theone-vs-one algorithm computes the average of the pairwise ROC AUC scores, andthe one-vs-rest algorithm computes the average of the ROC AUC scores for eachclass against all other classes. In both cases, the predicted labels areprovided in an array with values from 0 to n_classes, and the scorescorrespond to the probability estimates that a sample belongs to a particularclass. The OvO and OvR algorithms support weighting uniformly(average='macro') and by prevalence (average='weighted').

One-vs-one Algorithm: Computes the average AUC of all possible pairwisecombinations of classes. [HT2001] defines a multiclass AUC metric weighteduniformly:

3.3. Metrics and scoring: quantifying the quality of predictions - 图129

where

3.3. Metrics and scoring: quantifying the quality of predictions - 图130 is the number of classes and3.3. Metrics and scoring: quantifying the quality of predictions - 图131 is theAUC with class3.3. Metrics and scoring: quantifying the quality of predictions - 图132 as the positive class and class3.3. Metrics and scoring: quantifying the quality of predictions - 图133 as thenegative class. In general,3.3. Metrics and scoring: quantifying the quality of predictions - 图134 in the multiclasscase. This algorithm is used by setting the keyword argument multiclassto 'ovo' and average to 'macro'.

The [HT2001] multiclass AUC metric can be extended to be weighted by theprevalence:

3.3. Metrics and scoring: quantifying the quality of predictions - 图135

where

3.3. Metrics and scoring: quantifying the quality of predictions - 图136 is the number of classes. This algorithm is used by settingthe keyword argument multiclass to 'ovo' and average to'weighted'. The 'weighted' option returns a prevalence-weighted averageas described in [FC2009].

One-vs-rest Algorithm: Computes the AUC of each class against the rest[PD2000]. The algorithm is functionally the same as the multilabel case. Toenable this algorithm set the keyword argument multiclass to 'ovr'.Like OvO, OvR supports two types of averaging: 'macro' [F2006] and'weighted' [F2001].

In applications where a high false positive rate is not tolerable the parametermax_fpr of roc_auc_score can be used to summarize the ROC curve upto the given limit.../_images/sphx_glr_plot_roc_0021.png

Examples:

References:

3.3.2.15. Zero one loss

The zero_one_loss function computes the sum or the average of the 0-1classification loss (

3.3. Metrics and scoring: quantifying the quality of predictions - 图138) over3.3. Metrics and scoring: quantifying the quality of predictions - 图139. Bydefault, the function normalizes over the sample. To get the sum of the3.3. Metrics and scoring: quantifying the quality of predictions - 图140, set normalize to False.

In multilabel classification, the zero_one_loss scores a subset asone if its labels strictly match the predictions, and as a zero if thereare any errors. By default, the function returns the percentage of imperfectlypredicted subsets. To get the count of such subsets instead, setnormalize to False

If

3.3. Metrics and scoring: quantifying the quality of predictions - 图141 is the predicted value ofthe3.3. Metrics and scoring: quantifying the quality of predictions - 图142-th sample and3.3. Metrics and scoring: quantifying the quality of predictions - 图143 is the corresponding true value,then the 0-1 loss3.3. Metrics and scoring: quantifying the quality of predictions - 图144 is defined as:

3.3. Metrics and scoring: quantifying the quality of predictions - 图145

where

3.3. Metrics and scoring: quantifying the quality of predictions - 图146 is the indicator function.

>>>

  1. >>> from sklearn.metrics import zero_one_loss
  2. >>> y_pred = [1, 2, 3, 4]
  3. >>> y_true = [2, 2, 3, 4]
  4. >>> zero_one_loss(y_true, y_pred)
  5. 0.25
  6. >>> zero_one_loss(y_true, y_pred, normalize=False)
  7. 1

In the multilabel case with binary label indicators, where the first labelset [0,1] has an error:

>>>

  1. >>> zero_one_loss(np.array([[0, 1], [1, 1]]), np.ones((2, 2)))
  2. 0.5
  3.  
  4. >>> zero_one_loss(np.array([[0, 1], [1, 1]]), np.ones((2, 2)), normalize=False)
  5. 1

Example:

3.3.2.16. Brier score loss

The brier_score_loss function computes theBrier scorefor binary classes. Quoting Wikipedia:

“The Brier score is a proper score function that measures the accuracy ofprobabilistic predictions. It is applicable to tasks in which predictionsmust assign probabilities to a set of mutually exclusive discrete outcomes.”

This function returns a score of the mean square difference between the actualoutcome and the predicted probability of the possible outcome. The actualoutcome has to be 1 or 0 (true or false), while the predicted probability ofthe actual outcome can be a value between 0 and 1.

The brier score loss is also between 0 to 1 and the lower the score (the meansquare difference is smaller), the more accurate the prediction is. It can bethought of as a measure of the “calibration” of a set of probabilisticpredictions.

3.3. Metrics and scoring: quantifying the quality of predictions - 图147

where :

3.3. Metrics and scoring: quantifying the quality of predictions - 图148 is the total number of predictions,3.3. Metrics and scoring: quantifying the quality of predictions - 图149 is thepredicted probability of the actual outcome3.3. Metrics and scoring: quantifying the quality of predictions - 图150.

Here is a small example of usage of this function::

>>>

  1. >>> import numpy as np
  2. >>> from sklearn.metrics import brier_score_loss
  3. >>> y_true = np.array([0, 1, 1, 0])
  4. >>> y_true_categorical = np.array(["spam", "ham", "ham", "spam"])
  5. >>> y_prob = np.array([0.1, 0.9, 0.8, 0.4])
  6. >>> y_pred = np.array([0, 1, 1, 0])
  7. >>> brier_score_loss(y_true, y_prob)
  8. 0.055
  9. >>> brier_score_loss(y_true, 1 - y_prob, pos_label=0)
  10. 0.055
  11. >>> brier_score_loss(y_true_categorical, y_prob, pos_label="ham")
  12. 0.055
  13. >>> brier_score_loss(y_true, y_prob > 0.5)
  14. 0.0

Example:

References:

3.3.3. Multilabel ranking metrics

In multilabel learning, each sample can have any number of ground truth labelsassociated with it. The goal is to give high scores and better rank tothe ground truth labels.

3.3.3.1. Coverage error

The coverage_error function computes the average number of labels thathave to be included in the final prediction such that all true labelsare predicted. This is useful if you want to know how many top-scored-labelsyou have to predict in average without missing any true one. The best valueof this metrics is thus the average number of true labels.

Note

Our implementation’s score is 1 greater than the one given in Tsoumakaset al., 2010. This extends it to handle the degenerate case in which aninstance has 0 true labels.

Formally, given a binary indicator matrix of the ground truth labels

3.3. Metrics and scoring: quantifying the quality of predictions - 图151 and thescore associated with each label3.3. Metrics and scoring: quantifying the quality of predictions - 图152,the coverage is defined as

3.3. Metrics and scoring: quantifying the quality of predictions - 图153

with

3.3. Metrics and scoring: quantifying the quality of predictions - 图154.Given the rank definition, ties in y_scores are broken by giving themaximal rank that would have been assigned to all tied values.

Here is a small example of usage of this function:

>>>

  1. >>> import numpy as np
  2. >>> from sklearn.metrics import coverage_error
  3. >>> y_true = np.array([[1, 0, 0], [0, 0, 1]])
  4. >>> y_score = np.array([[0.75, 0.5, 1], [1, 0.2, 0.1]])
  5. >>> coverage_error(y_true, y_score)
  6. 2.5

3.3.3.2. Label ranking average precision

The label_ranking_average_precision_score functionimplements label ranking average precision (LRAP). This metric is linked tothe average_precision_score function, but is based on the notion oflabel ranking instead of precision and recall.

Label ranking average precision (LRAP) averages over the samples the answer tothe following question: for each ground truth label, what fraction ofhigher-ranked labels were true labels? This performance measure will be higherif you are able to give better rank to the labels associated with each sample.The obtained score is always strictly greater than 0, and the best value is 1.If there is exactly one relevant label per sample, label ranking averageprecision is equivalent to the meanreciprocal rank.

Formally, given a binary indicator matrix of the ground truth labels

3.3. Metrics and scoring: quantifying the quality of predictions - 图155and the score associated with each label3.3. Metrics and scoring: quantifying the quality of predictions - 图156,the average precision is defined as

3.3. Metrics and scoring: quantifying the quality of predictions - 图157

where

3.3. Metrics and scoring: quantifying the quality of predictions - 图158,3.3. Metrics and scoring: quantifying the quality of predictions - 图159,3.3. Metrics and scoring: quantifying the quality of predictions - 图160 computes the cardinality of the set (i.e., the number ofelements in the set), and3.3. Metrics and scoring: quantifying the quality of predictions - 图161 is the3.3. Metrics and scoring: quantifying the quality of predictions - 图162 “norm”(which computes the number of nonzero elements in a vector).

Here is a small example of usage of this function:

>>>

  1. >>> import numpy as np
  2. >>> from sklearn.metrics import label_ranking_average_precision_score
  3. >>> y_true = np.array([[1, 0, 0], [0, 0, 1]])
  4. >>> y_score = np.array([[0.75, 0.5, 1], [1, 0.2, 0.1]])
  5. >>> label_ranking_average_precision_score(y_true, y_score)
  6. 0.416...

3.3.3.3. Ranking loss

The label_ranking_loss function computes the ranking loss whichaverages over the samples the number of label pairs that are incorrectlyordered, i.e. true labels have a lower score than false labels, weighted bythe inverse of the number of ordered pairs of false and true labels.The lowest achievable ranking loss is zero.

Formally, given a binary indicator matrix of the ground truth labels

3.3. Metrics and scoring: quantifying the quality of predictions - 图163 and thescore associated with each label3.3. Metrics and scoring: quantifying the quality of predictions - 图164,the ranking loss is defined as

3.3. Metrics and scoring: quantifying the quality of predictions - 图165

where

3.3. Metrics and scoring: quantifying the quality of predictions - 图166 computes the cardinality of the set (i.e., the number ofelements in the set) and3.3. Metrics and scoring: quantifying the quality of predictions - 图167 is the3.3. Metrics and scoring: quantifying the quality of predictions - 图168 “norm”(which computes the number of nonzero elements in a vector).

Here is a small example of usage of this function:

>>>

  1. >>> import numpy as np
  2. >>> from sklearn.metrics import label_ranking_loss
  3. >>> y_true = np.array([[1, 0, 0], [0, 0, 1]])
  4. >>> y_score = np.array([[0.75, 0.5, 1], [1, 0.2, 0.1]])
  5. >>> label_ranking_loss(y_true, y_score)
  6. 0.75...
  7. >>> # With the following prediction, we have perfect and minimal loss
  8. >>> y_score = np.array([[1.0, 0.1, 0.2], [0.1, 0.2, 0.9]])
  9. >>> label_ranking_loss(y_true, y_score)
  10. 0.0

References:

  • Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010). Mining multi-label data. InData mining and knowledge discovery handbook (pp. 667-685). Springer US.

3.3.3.4. Normalized Discounted Cumulative Gain

Discounted Cumulative Gain (DCG) and Normalized Discounted Cumulative Gain(NDCG) are ranking metrics; they compare a predicted order to ground-truthscores, such as the relevance of answers to a query.

from the Wikipedia page for Discounted Cumulative Gain:

“Discounted cumulative gain (DCG) is a measure of ranking quality. Ininformation retrieval, it is often used to measure effectiveness of web searchengine algorithms or related applications. Using a graded relevance scale ofdocuments in a search-engine result set, DCG measures the usefulness, or gain,of a document based on its position in the result list. The gain is accumulatedfrom the top of the result list to the bottom, with the gain of each resultdiscounted at lower ranks”

DCG orders the true targets (e.g. relevance of query answers) in the predictedorder, then multiplies them by a logarithmic decay and sums the result. The sumcan be truncated after the first

3.3. Metrics and scoring: quantifying the quality of predictions - 图169 results, in which case we call itDCG@K.NDCG, or NDCG@K is DCG divided by the DCG obtained by a perfect prediction, sothat it is always between 0 and 1. Usually, NDCG is preferred to DCG.

Compared with the ranking loss, NDCG can take into account relevance scores,rather than a ground-truth ranking. So if the ground-truth consists only of anordering, the ranking loss should be preferred; if the ground-truth consists ofactual usefulness scores (e.g. 0 for irrelevant, 1 for relevant, 2 for veryrelevant), NDCG can be used.

For one sample, given the vector of continuous ground-truth values for eachtarget

3.3. Metrics and scoring: quantifying the quality of predictions - 图170, where3.3. Metrics and scoring: quantifying the quality of predictions - 图171 is the number of outputs, andthe prediction3.3. Metrics and scoring: quantifying the quality of predictions - 图172, which induces the ranking function3.3. Metrics and scoring: quantifying the quality of predictions - 图173, theDCG score is

3.3. Metrics and scoring: quantifying the quality of predictions - 图174

and the NDCG score is the DCG score divided by the DCG score obtained for

3.3. Metrics and scoring: quantifying the quality of predictions - 图175.

References:

  • Wikipedia entry for Discounted Cumulative Gain:https://en.wikipedia.org/wiki/Discounted_cumulative_gain

  • Jarvelin, K., & Kekalainen, J. (2002).Cumulated gain-based evaluation of IR techniques. ACM Transactions onInformation 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 26thAnnual Conference on Learning Theory (COLT 2013)

  • McSherry, F., & Najork, M. (2008, March). Computing information retrievalperformance measures efficiently in the presence of tied scores. InEuropean conference on information retrieval (pp. 414-421). Springer,Berlin, Heidelberg.

3.3.4. Regression metrics

The sklearn.metrics module implements several loss, score, and utilityfunctions to measure regression performance. Some of those have been enhancedto handle the multioutput case: mean_squared_error,mean_absolute_error, explained_variance_score andr2_score.

These functions have an multioutput keyword argument which specifies theway the scores or losses for each individual target should be averaged. Thedefault is 'uniform_average', which specifies a uniformly weighted meanover outputs. If an ndarray of shape (n_outputs,) is passed, then itsentries are interpreted as weights and an according weighted average isreturned. If multioutput is 'raw_values' is specified, then allunaltered individual scores or losses will be returned in an array of shape(n_outputs,).

The r2_score and explained_variance_score accept an additionalvalue 'variance_weighted' for the multioutput parameter. This optionleads to a weighting of each individual score by the variance of thecorresponding target variable. This setting quantifies the globally capturedunscaled variance. If the target variables are of different scale, then thisscore puts more importance on well explaining the higher variance variables.multioutput='variance_weighted' is the default value for r2_scorefor backward compatibility. This will be changed to uniform_average in thefuture.

3.3.4.1. Explained variance score

The explained_variance_score computes the explained varianceregression score.

If

3.3. Metrics and scoring: quantifying the quality of predictions - 图176 is the estimated target output,3.3. Metrics and scoring: quantifying the quality of predictions - 图177 the corresponding(correct) target output, and3.3. Metrics and scoring: quantifying the quality of predictions - 图178 is Variance, the square of the standard deviation,then the explained variance is estimated as follow:

3.3. Metrics and scoring: quantifying the quality of predictions - 图179

The best possible score is 1.0, lower values are worse.

Here is a small example of usage of the explained_variance_scorefunction:

>>>

  1. >>> from sklearn.metrics import explained_variance_score
  2. >>> y_true = [3, -0.5, 2, 7]
  3. >>> y_pred = [2.5, 0.0, 2, 8]
  4. >>> explained_variance_score(y_true, y_pred)
  5. 0.957...
  6. >>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
  7. >>> y_pred = [[0, 2], [-1, 2], [8, -5]]
  8. >>> explained_variance_score(y_true, y_pred, multioutput='raw_values')
  9. array([0.967..., 1. ])
  10. >>> explained_variance_score(y_true, y_pred, multioutput=[0.3, 0.7])
  11. 0.990...

3.3.4.2. Max error

The max_error function computes the maximum residual error , a metricthat captures the worst case error between the predicted value andthe true value. In a perfectly fitted single output regressionmodel, max_error would be 0 on the training set and though thiswould be highly unlikely in the real world, this metric shows theextent of error that the model had when it was fitted.

If

3.3. Metrics and scoring: quantifying the quality of predictions - 图180 is the predicted value of the3.3. Metrics and scoring: quantifying the quality of predictions - 图181-th sample,and3.3. Metrics and scoring: quantifying the quality of predictions - 图182 is the corresponding true value, then the max error isdefined as

3.3. Metrics and scoring: quantifying the quality of predictions - 图183

Here is a small example of usage of the max_error function:

>>>

  1. >>> from sklearn.metrics import max_error
  2. >>> y_true = [3, 2, 7, 1]
  3. >>> y_pred = [9, 2, 7, 1]
  4. >>> max_error(y_true, y_pred)
  5. 6

The max_error does not support multioutput.

3.3.4.3. Mean absolute error

The mean_absolute_error function computes mean absoluteerror, a riskmetric corresponding to the expected value of the absolute error loss or

3.3. Metrics and scoring: quantifying the quality of predictions - 图184-norm loss.

If

3.3. Metrics and scoring: quantifying the quality of predictions - 图185 is the predicted value of the3.3. Metrics and scoring: quantifying the quality of predictions - 图186-th sample,and3.3. Metrics and scoring: quantifying the quality of predictions - 图187 is the corresponding true value, then the mean absolute error(MAE) estimated over3.3. Metrics and scoring: quantifying the quality of predictions - 图188 is defined as

3.3. Metrics and scoring: quantifying the quality of predictions - 图189

Here is a small example of usage of the mean_absolute_error function:

>>>

  1. >>> from sklearn.metrics import mean_absolute_error
  2. >>> y_true = [3, -0.5, 2, 7]
  3. >>> y_pred = [2.5, 0.0, 2, 8]
  4. >>> mean_absolute_error(y_true, y_pred)
  5. 0.5
  6. >>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
  7. >>> y_pred = [[0, 2], [-1, 2], [8, -5]]
  8. >>> mean_absolute_error(y_true, y_pred)
  9. 0.75
  10. >>> mean_absolute_error(y_true, y_pred, multioutput='raw_values')
  11. array([0.5, 1. ])
  12. >>> mean_absolute_error(y_true, y_pred, multioutput=[0.3, 0.7])
  13. 0.85...

3.3.4.4. Mean squared error

The mean_squared_error function computes mean squareerror, a riskmetric corresponding to the expected value of the squared (quadratic) error orloss.

If

3.3. Metrics and scoring: quantifying the quality of predictions - 图190 is the predicted value of the3.3. Metrics and scoring: quantifying the quality of predictions - 图191-th sample,and3.3. Metrics and scoring: quantifying the quality of predictions - 图192 is the corresponding true value, then the mean squared error(MSE) estimated over3.3. Metrics and scoring: quantifying the quality of predictions - 图193 is defined as

3.3. Metrics and scoring: quantifying the quality of predictions - 图194

Here is a small example of usage of the mean_squared_errorfunction:

>>>

  1. >>> from sklearn.metrics import mean_squared_error
  2. >>> y_true = [3, -0.5, 2, 7]
  3. >>> y_pred = [2.5, 0.0, 2, 8]
  4. >>> mean_squared_error(y_true, y_pred)
  5. 0.375
  6. >>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
  7. >>> y_pred = [[0, 2], [-1, 2], [8, -5]]
  8. >>> mean_squared_error(y_true, y_pred)
  9. 0.7083...

Examples:

3.3.4.5. Mean squared logarithmic error

The mean_squared_log_error function computes a risk metriccorresponding to the expected value of the squared logarithmic (quadratic)error or loss.

If

3.3. Metrics and scoring: quantifying the quality of predictions - 图195 is the predicted value of the3.3. Metrics and scoring: quantifying the quality of predictions - 图196-th sample,and3.3. Metrics and scoring: quantifying the quality of predictions - 图197 is the corresponding true value, then the mean squaredlogarithmic error (MSLE) estimated over3.3. Metrics and scoring: quantifying the quality of predictions - 图198 isdefined as

3.3. Metrics and scoring: quantifying the quality of predictions - 图199

Where

3.3. Metrics and scoring: quantifying the quality of predictions - 图200 means the natural logarithm of3.3. Metrics and scoring: quantifying the quality of predictions - 图201. This metricis best to use when targets having exponential growth, such as populationcounts, average sales of a commodity over a span of years etc. Note that thismetric penalizes an under-predicted estimate greater than an over-predictedestimate.

Here is a small example of usage of the mean_squared_log_errorfunction:

>>>

  1. >>> from sklearn.metrics import mean_squared_log_error
  2. >>> y_true = [3, 5, 2.5, 7]
  3. >>> y_pred = [2.5, 5, 4, 8]
  4. >>> mean_squared_log_error(y_true, y_pred)
  5. 0.039...
  6. >>> y_true = [[0.5, 1], [1, 2], [7, 6]]
  7. >>> y_pred = [[0.5, 2], [1, 2.5], [8, 8]]
  8. >>> mean_squared_log_error(y_true, y_pred)
  9. 0.044...

3.3.4.6. Median absolute error

The median_absolute_error is particularly interesting because it isrobust to outliers. The loss is calculated by taking the median of all absolutedifferences between the target and the prediction.

If

3.3. Metrics and scoring: quantifying the quality of predictions - 图202 is the predicted value of the3.3. Metrics and scoring: quantifying the quality of predictions - 图203-th sampleand3.3. Metrics and scoring: quantifying the quality of predictions - 图204 is the corresponding true value, then the median absolute error(MedAE) estimated over3.3. Metrics and scoring: quantifying the quality of predictions - 图205 is defined as

3.3. Metrics and scoring: quantifying the quality of predictions - 图206

The median_absolute_error does not support multioutput.

Here is a small example of usage of the median_absolute_errorfunction:

>>>

  1. >>> from sklearn.metrics import median_absolute_error
  2. >>> y_true = [3, -0.5, 2, 7]
  3. >>> y_pred = [2.5, 0.0, 2, 8]
  4. >>> median_absolute_error(y_true, y_pred)
  5. 0.5

3.3.4.7. R² score, the coefficient of determination

The r2_score function computes the coefficient ofdetermination,usually denoted as R².

It represents the proportion of variance (of y) that has been explained by theindependent variables in the model. It provides an indication of goodness offit and therefore a measure of how well unseen samples are likely to bepredicted by the model, through the proportion of explained variance.

As such variance is dataset dependent, R² may not be meaningfully comparableacross different datasets. Best possible score is 1.0 and it can be negative(because the model can be arbitrarily worse). A constant model that alwayspredicts the expected value of y, disregarding the input features, would get aR² score of 0.0.

If

3.3. Metrics and scoring: quantifying the quality of predictions - 图207 is the predicted value of the3.3. Metrics and scoring: quantifying the quality of predictions - 图208-th sampleand3.3. Metrics and scoring: quantifying the quality of predictions - 图209 is the corresponding true value for total3.3. Metrics and scoring: quantifying the quality of predictions - 图210 samples,the estimated R² is defined as:

3.3. Metrics and scoring: quantifying the quality of predictions - 图211

where

3.3. Metrics and scoring: quantifying the quality of predictions - 图212 and3.3. Metrics and scoring: quantifying the quality of predictions - 图213.

Note that r2_score calculates unadjusted R² without correcting forbias in sample variance of y.

Here is a small example of usage of the r2_score function:

>>>

  1. >>> from sklearn.metrics import r2_score
  2. >>> y_true = [3, -0.5, 2, 7]
  3. >>> y_pred = [2.5, 0.0, 2, 8]
  4. >>> r2_score(y_true, y_pred)
  5. 0.948...
  6. >>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
  7. >>> y_pred = [[0, 2], [-1, 2], [8, -5]]
  8. >>> r2_score(y_true, y_pred, multioutput='variance_weighted')
  9. 0.938...
  10. >>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
  11. >>> y_pred = [[0, 2], [-1, 2], [8, -5]]
  12. >>> r2_score(y_true, y_pred, multioutput='uniform_average')
  13. 0.936...
  14. >>> r2_score(y_true, y_pred, multioutput='raw_values')
  15. array([0.965..., 0.908...])
  16. >>> r2_score(y_true, y_pred, multioutput=[0.3, 0.7])
  17. 0.925...

Example:

3.3.4.8. Mean Poisson, Gamma, and Tweedie deviances

The mean_tweedie_deviance function computes the mean Tweediedeviance errorwith a power parameter (

3.3. Metrics and scoring: quantifying the quality of predictions - 图214). This is a metric that elicitspredicted expectation values of regression targets.

Following special cases exist,

If

3.3. Metrics and scoring: quantifying the quality of predictions - 图215 is the predicted value of the3.3. Metrics and scoring: quantifying the quality of predictions - 图216-th sample,and3.3. Metrics and scoring: quantifying the quality of predictions - 图217 is the corresponding true value, then the mean Tweediedeviance error (D) for power3.3. Metrics and scoring: quantifying the quality of predictions - 图218, estimated over3.3. Metrics and scoring: quantifying the quality of predictions - 图219is defined as

3.3. Metrics and scoring: quantifying the quality of predictions - 图220

Tweedie deviance is a homogeneous function of degree 2-power.Thus, Gamma distribution with power=2 means that simultaneously scalingy_true and y_pred has no effect on the deviance. For Poissondistribution power=1 the deviance scales linearly, and for Normaldistribution (power=0), quadratically. In general, the higherpower the less weight is given to extreme deviations between trueand predicted targets.

For instance, let’s compare the two predictions 1.0 and 100 that are both50% of their corresponding true value.

The mean squared error (power=0) is very sensitive to theprediction difference of the second point,:

>>>

  1. >>> from sklearn.metrics import mean_tweedie_deviance
  2. >>> mean_tweedie_deviance([1.0], [1.5], power=0)
  3. 0.25
  4. >>> mean_tweedie_deviance([100.], [150.], power=0)
  5. 2500.0

If we increase power to 1,:

>>>

  1. >>> mean_tweedie_deviance([1.0], [1.5], power=1)
  2. 0.18...
  3. >>> mean_tweedie_deviance([100.], [150.], power=1)
  4. 18.9...

the difference in errors decreases. Finally, by setting, power=2:

>>>

  1. >>> mean_tweedie_deviance([1.0], [1.5], power=2)
  2. 0.14...
  3. >>> mean_tweedie_deviance([100.], [150.], power=2)
  4. 0.14...

we would get identical errors. The deviance when power=2 is thus onlysensitive to relative errors.

3.3.5. Clustering metrics

The sklearn.metrics module implements several loss, score, and utilityfunctions. For more information see the Clustering performance evaluationsection for instance clustering, and Biclustering evaluation forbiclustering.

3.3.6. Dummy estimators

When doing supervised learning, a simple sanity check consists of comparingone’s estimator against simple rules of thumb. DummyClassifierimplements several such simple strategies for classification:

  • stratified generates random predictions by respecting the trainingset class distribution.

  • most_frequent always predicts the most frequent label in the training set.

  • prior always predicts the class that maximizes the class prior(like most_frequent) and predict_proba returns the class prior.

  • uniform generates predictions uniformly at random.

    • constant always predicts a constant label that is provided by the user.
    • A major motivation of this method is F1-scoring, when the positive classis in the minority.

Note that with all these strategies, the predict method completely ignoresthe input data!

To illustrate DummyClassifier, first let’s create an imbalanceddataset:

>>>

  1. >>> from sklearn.datasets import load_iris
  2. >>> from sklearn.model_selection import train_test_split
  3. >>> X, y = load_iris(return_X_y=True)
  4. >>> y[y != 1] = -1
  5. >>> X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

Next, let’s compare the accuracy of SVC and most_frequent:

>>>

  1. >>> from sklearn.dummy import DummyClassifier
  2. >>> from sklearn.svm import SVC
  3. >>> clf = SVC(kernel='linear', C=1).fit(X_train, y_train)
  4. >>> clf.score(X_test, y_test)
  5. 0.63...
  6. >>> clf = DummyClassifier(strategy='most_frequent', random_state=0)
  7. >>> clf.fit(X_train, y_train)
  8. DummyClassifier(random_state=0, strategy='most_frequent')
  9. >>> clf.score(X_test, y_test)
  10. 0.57...

We see that SVC doesn’t do much better than a dummy classifier. Now, let’schange the kernel:

>>>

  1. >>> clf = SVC(kernel='rbf', C=1).fit(X_train, y_train)
  2. >>> clf.score(X_test, y_test)
  3. 0.94...

We see that the accuracy was boosted to almost 100%. A cross validationstrategy is recommended for a better estimate of the accuracy, if itis not too CPU costly. For more information see the Cross-validation: evaluating estimator performancesection. Moreover if you want to optimize over the parameter space, it is highlyrecommended to use an appropriate methodology; see the Tuning the hyper-parameters of an estimatorsection for details.

More generally, when the accuracy of a classifier is too close to random, itprobably means that something went wrong: features are not helpful, ahyperparameter is not correctly tuned, the classifier is suffering from classimbalance, etc…

DummyRegressor also implements four simple rules of thumb for regression:

  • mean always predicts the mean of the training targets.

  • median always predicts the median of the training targets.

  • quantile always predicts a user provided quantile of the training targets.

  • constant always predicts a constant value that is provided by the user.

In all these strategies, the predict method completely ignoresthe input data.