@@ -667,7 +667,8 @@ def f1_score(y_true, y_pred, labels=None, pos_label=1, average='binary',
667
667
668
668
References
669
669
----------
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>`_
671
672
672
673
Examples
673
674
--------
@@ -1452,7 +1453,8 @@ class 2 1.00 0.67 0.80 3
1452
1453
return report
1453
1454
1454
1455
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 ):
1456
1458
"""Compute the average Hamming loss.
1457
1459
1458
1460
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):
1467
1469
y_pred : 1d array-like, or label indicator array / sparse matrix
1468
1470
Predicted labels, as returned by a classifier.
1469
1471
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
1472
1477
1473
1478
sample_weight : array-like of shape = [n_samples], optional
1474
1479
Sample weights.
1475
1480
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
+
1476
1485
Returns
1477
1486
-------
1478
1487
loss : float or int,
@@ -1520,12 +1529,17 @@ def hamming_loss(y_true, y_pred, classes=None, sample_weight=None):
1520
1529
>>> hamming_loss(np.array([[0, 1], [1, 1]]), np.zeros((2, 2)))
1521
1530
0.75
1522
1531
"""
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
+
1523
1537
y_type , y_true , y_pred = _check_targets (y_true , y_pred )
1524
1538
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 )
1527
1541
else :
1528
- classes = np .asarray (classes )
1542
+ labels = np .asarray (labels )
1529
1543
1530
1544
if sample_weight is None :
1531
1545
weight_average = 1.
@@ -1536,7 +1550,7 @@ def hamming_loss(y_true, y_pred, classes=None, sample_weight=None):
1536
1550
n_differences = count_nonzero (y_true - y_pred ,
1537
1551
sample_weight = sample_weight )
1538
1552
return (n_differences /
1539
- (y_true .shape [0 ] * len (classes ) * weight_average ))
1553
+ (y_true .shape [0 ] * len (labels ) * weight_average ))
1540
1554
1541
1555
elif y_type in ["binary" , "multiclass" ]:
1542
1556
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,
1620
1634
1621
1635
if len (lb .classes_ ) == 1 :
1622
1636
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 ]))
1626
1640
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_ ))
1629
1644
1630
1645
transformed_labels = lb .transform (y_true )
1631
1646
@@ -1647,11 +1662,13 @@ def log_loss(y_true, y_pred, eps=1e-15, normalize=True, sample_weight=None,
1647
1662
transformed_labels = check_array (transformed_labels )
1648
1663
if len (lb .classes_ ) != y_pred .shape [1 ]:
1649
1664
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 "
1653
1669
"y_true: {2}" .format (transformed_labels .shape [1 ],
1654
- y_pred .shape [1 ], lb .classes_ ))
1670
+ y_pred .shape [1 ],
1671
+ lb .classes_ ))
1655
1672
else :
1656
1673
raise ValueError ('The number of classes in labels is different '
1657
1674
'from that in y_pred. Classes found in '
0 commit comments