Skip to content

Commit e2648b1

Browse files
srvanrelljnothman
authored andcommitted
classes parameter renamed to labels in hamming_loss()
1 parent 6a2b4f7 commit e2648b1

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

doc/whats_new.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,11 @@ API changes summary
420420
(`#7187 <https://github.com/scikit-learn/scikit-learn/pull/7187>`_)
421421
by `YenChen Lin`_.
422422

423+
- ``classes`` parameter was renamed to ``labels`` in
424+
:func:`metrics.classification.hamming_loss`.
425+
(`#7260 <https://github.com/scikit-learn/scikit-learn/pull/7260>`_) by
426+
`Sebastián Vanrell`_.
427+
423428

424429
.. currentmodule:: sklearn
425430

@@ -4402,3 +4407,5 @@ David Huard, Dave Morrill, Ed Schofield, Travis Oliphant, Pearu Peterson.
44024407
.. _Hong Guangguo: https://github.com/hongguangguo
44034408

44044409
.. _Mads Jensen: https://github.com/indianajensen
4410+
4411+
.. _Sebastián Vanrell: https://github.com/srvanrell

sklearn/metrics/classification.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,8 @@ def f1_score(y_true, y_pred, labels=None, pos_label=1, average='binary',
667667
668668
References
669669
----------
670-
.. [1] `Wikipedia entry for the F1-score <https://en.wikipedia.org/wiki/F1_score>`_
670+
.. [1] `Wikipedia entry for the F1-score
671+
<https://en.wikipedia.org/wiki/F1_score>`_
671672
672673
Examples
673674
--------
@@ -1452,7 +1453,8 @@ class 2 1.00 0.67 0.80 3
14521453
return report
14531454

14541455

1455-
def hamming_loss(y_true, y_pred, classes=None, sample_weight=None):
1456+
def hamming_loss(y_true, y_pred, labels=None, sample_weight=None,
1457+
classes=None):
14561458
"""Compute the average Hamming loss.
14571459
14581460
The Hamming loss is the fraction of labels that are incorrectly predicted.
@@ -1467,12 +1469,19 @@ def hamming_loss(y_true, y_pred, classes=None, sample_weight=None):
14671469
y_pred : 1d array-like, or label indicator array / sparse matrix
14681470
Predicted labels, as returned by a classifier.
14691471
1470-
classes : array, shape = [n_labels], optional
1471-
Integer array of labels.
1472+
labels : array, shape = [n_labels], optional (default=None)
1473+
Integer array of labels. If not provided, labels will be inferred
1474+
from y_true and y_pred.
1475+
1476+
.. versionadded:: 0.18
14721477
14731478
sample_weight : array-like of shape = [n_samples], optional
14741479
Sample weights.
14751480
1481+
classes : array, shape = [n_labels], optional
1482+
(deprecated) Integer array of labels. This parameter has been
1483+
renamed to ``labels`` in version 0.18 and will be removed in 0.20.
1484+
14761485
Returns
14771486
-------
14781487
loss : float or int,
@@ -1520,12 +1529,17 @@ def hamming_loss(y_true, y_pred, classes=None, sample_weight=None):
15201529
>>> hamming_loss(np.array([[0, 1], [1, 1]]), np.zeros((2, 2)))
15211530
0.75
15221531
"""
1532+
if classes is not None:
1533+
warnings.warn("'classes' was renamed to 'labels' in version 0.18 and "
1534+
"will be removed in 0.20.", DeprecationWarning)
1535+
labels = classes
1536+
15231537
y_type, y_true, y_pred = _check_targets(y_true, y_pred)
15241538

1525-
if classes is None:
1526-
classes = unique_labels(y_true, y_pred)
1539+
if labels is None:
1540+
labels = unique_labels(y_true, y_pred)
15271541
else:
1528-
classes = np.asarray(classes)
1542+
labels = np.asarray(labels)
15291543

15301544
if sample_weight is None:
15311545
weight_average = 1.
@@ -1536,7 +1550,7 @@ def hamming_loss(y_true, y_pred, classes=None, sample_weight=None):
15361550
n_differences = count_nonzero(y_true - y_pred,
15371551
sample_weight=sample_weight)
15381552
return (n_differences /
1539-
(y_true.shape[0] * len(classes) * weight_average))
1553+
(y_true.shape[0] * len(labels) * weight_average))
15401554

15411555
elif y_type in ["binary", "multiclass"]:
15421556
return _weighted_sum(y_true != y_pred, sample_weight, normalize=True)
@@ -1620,12 +1634,13 @@ def log_loss(y_true, y_pred, eps=1e-15, normalize=True, sample_weight=None,
16201634

16211635
if len(lb.classes_) == 1:
16221636
if labels is None:
1623-
raise ValueError('y_true contains only one label ({0}). Please provide '
1624-
'the true labels explicitly through the labels '
1625-
'argument.'.format(lb.classes_[0]))
1637+
raise ValueError('y_true contains only one label ({0}). Please '
1638+
'provide the true labels explicitly through the '
1639+
'labels argument.'.format(lb.classes_[0]))
16261640
else:
1627-
raise ValueError('The labels array needs to contain at least two labels'
1628-
'for log_loss, got {0}.'.format(lb.classes_))
1641+
raise ValueError('The labels array needs to contain at least two '
1642+
'labels for log_loss, '
1643+
'got {0}.'.format(lb.classes_))
16291644

16301645
transformed_labels = lb.transform(y_true)
16311646

@@ -1647,11 +1662,13 @@ def log_loss(y_true, y_pred, eps=1e-15, normalize=True, sample_weight=None,
16471662
transformed_labels = check_array(transformed_labels)
16481663
if len(lb.classes_) != y_pred.shape[1]:
16491664
if labels is None:
1650-
raise ValueError("y_true and y_pred contain different number of classes "
1651-
"{0}, {1}. Please provide the true labels explicitly "
1652-
"through the labels argument. Classes found in"
1665+
raise ValueError("y_true and y_pred contain different number of "
1666+
"classes {0}, {1}. Please provide the true "
1667+
"labels explicitly through the labels argument. "
1668+
"Classes found in "
16531669
"y_true: {2}".format(transformed_labels.shape[1],
1654-
y_pred.shape[1], lb.classes_))
1670+
y_pred.shape[1],
1671+
lb.classes_))
16551672
else:
16561673
raise ValueError('The number of classes in labels is different '
16571674
'from that in y_pred. Classes found in '

sklearn/metrics/tests/test_classification.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ def test_multilabel_hamming_loss():
767767
assert_equal(hamming_loss(y1, np.zeros_like(y1), sample_weight=w), 2. / 3)
768768
# sp_hamming only works with 1-D arrays
769769
assert_equal(hamming_loss(y1[0], y2[0]), sp_hamming(y1[0], y2[0]))
770+
assert_warns(DeprecationWarning, hamming_loss, y1, y2, classes=[0, 1])
770771

771772

772773
def test_multilabel_jaccard_similarity_score():

sklearn/metrics/tests/test_common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@
258258
METRICS_WITH_LABELS = [
259259
"confusion_matrix",
260260

261+
"hamming_loss",
262+
261263
"precision_score", "recall_score", "f1_score", "f2_score", "f0.5_score",
262264

263265
"weighted_f0.5_score", "weighted_f1_score", "weighted_f2_score",

0 commit comments

Comments
 (0)