Fork me on GitHub

sklearn.metrics.zero_one_loss

sklearn.metrics.zero_one_loss(y_true, y_pred, normalize=True)

Zero-one classification loss.

If normalize is True, return the fraction of misclassifications (float), else it returns the number of misclassifications (int). The best performance is 0.

Parameters :

y_true : array-like or list of labels or label indicator matrix

Ground truth (correct) labels.

y_pred : array-like or list of labels or label indicator matrix

Predicted labels, as returned by a classifier.

normalize : bool, optional (default=True)

If False, return the number of misclassifications. Otherwise, return the fraction of misclassifications.

Returns :

loss : float or int,

If normalize == True, return the fraction of misclassifications (float), else it returns the number of misclassifications (int).

Notes

In multilabel classification, the zero_one_loss function corresponds to the subset zero-one loss: for each sample, the entire set of labels must be correctly predicted, otherwise the loss for that sample is equal to one.

Examples

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

In the multilabel case with binary indicator format:

>>> zero_one_loss(np.array([[0.0, 1.0], [1.0, 1.0]]), np.ones((2, 2)))
0.5

and with a list of labels format:

>>> zero_one_loss([(1, ), (3, )], [(1, 2), tuple()])
1.0
Previous
Next