@@ -922,6 +922,11 @@ def f1_score(y_true, y_pred, labels=None, pos_label=1, average='binary',
922
922
>>> f1_score(y_true, y_pred, average=None)
923
923
array([0.8, 0. , 0. ])
924
924
925
+ Notes
926
+ -----
927
+ When ``true positive + false positive == 0`` or
928
+ ``true positive + false negative == 0``, f-score returns 0 and raises
929
+ ``UndefinedMetricWarning``.
925
930
"""
926
931
return fbeta_score (y_true , y_pred , 1 , labels = labels ,
927
932
pos_label = pos_label , average = average ,
@@ -1036,6 +1041,11 @@ def fbeta_score(y_true, y_pred, beta, labels=None, pos_label=1,
1036
1041
... # doctest: +ELLIPSIS
1037
1042
array([0.71..., 0. , 0. ])
1038
1043
1044
+ Notes
1045
+ -----
1046
+ When ``true positive + false positive == 0`` or
1047
+ ``true positive + false negative == 0``, f-score returns 0 and raises
1048
+ ``UndefinedMetricWarning``.
1039
1049
"""
1040
1050
_ , _ , f , _ = precision_recall_fscore_support (y_true , y_pred ,
1041
1051
beta = beta ,
@@ -1233,6 +1243,12 @@ def precision_recall_fscore_support(y_true, y_pred, beta=1.0, labels=None,
1233
1243
array([0., 0., 1.]), array([0. , 0. , 0.8]),
1234
1244
array([2, 2, 2]))
1235
1245
1246
+ Notes
1247
+ -----
1248
+ When ``true positive + false positive == 0``, precision is undefined;
1249
+ When ``true positive + false negative == 0``, recall is undefined.
1250
+ In such cases, the metric will be set to 0, as will f-score, and
1251
+ ``UndefinedMetricWarning`` will be raised.
1236
1252
"""
1237
1253
average_options = (None , 'micro' , 'macro' , 'weighted' , 'samples' )
1238
1254
if average not in average_options and average != 'binary' :
@@ -1247,13 +1263,9 @@ def precision_recall_fscore_support(y_true, y_pred, beta=1.0, labels=None,
1247
1263
1248
1264
if average == 'binary' :
1249
1265
if y_type == 'binary' :
1250
- if pos_label not in present_labels :
1251
- if len (present_labels ) < 2 :
1252
- # Only negative labels
1253
- return (0. , 0. , 0. , 0 )
1254
- else :
1255
- raise ValueError ("pos_label=%r is not a valid label: %r" %
1256
- (pos_label , present_labels ))
1266
+ if pos_label not in present_labels and len (present_labels ) >= 2 :
1267
+ raise ValueError ("pos_label=%r is not a valid label: %r" %
1268
+ (pos_label , present_labels ))
1257
1269
labels = [pos_label ]
1258
1270
else :
1259
1271
raise ValueError ("Target is %s but average='binary'. Please "
@@ -1279,7 +1291,6 @@ def precision_recall_fscore_support(y_true, y_pred, beta=1.0, labels=None,
1279
1291
true_sum = np .array ([true_sum .sum ()])
1280
1292
1281
1293
# Finally, we have all our sufficient statistics. Divide! #
1282
-
1283
1294
beta2 = beta ** 2
1284
1295
with np .errstate (divide = 'ignore' , invalid = 'ignore' ):
1285
1296
# Divide, and on zero-division, set scores to 0 and warn:
@@ -1297,7 +1308,6 @@ def precision_recall_fscore_support(y_true, y_pred, beta=1.0, labels=None,
1297
1308
f_score [tp_sum == 0 ] = 0.0
1298
1309
1299
1310
# Average the results
1300
-
1301
1311
if average == 'weighted' :
1302
1312
weights = true_sum
1303
1313
if weights .sum () == 0 :
@@ -1410,6 +1420,10 @@ def precision_score(y_true, y_pred, labels=None, pos_label=1,
1410
1420
>>> precision_score(y_true, y_pred, average=None) # doctest: +ELLIPSIS
1411
1421
array([0.66..., 0. , 0. ])
1412
1422
1423
+ Notes
1424
+ -----
1425
+ When ``true positive + false positive == 0``, precision returns 0 and
1426
+ raises ``UndefinedMetricWarning``.
1413
1427
"""
1414
1428
p , _ , _ , _ = precision_recall_fscore_support (y_true , y_pred ,
1415
1429
labels = labels ,
@@ -1512,6 +1526,10 @@ def recall_score(y_true, y_pred, labels=None, pos_label=1, average='binary',
1512
1526
>>> recall_score(y_true, y_pred, average=None)
1513
1527
array([1., 0., 0.])
1514
1528
1529
+ Notes
1530
+ -----
1531
+ When ``true positive + false negative == 0``, recall returns 0 and raises
1532
+ ``UndefinedMetricWarning``.
1515
1533
"""
1516
1534
_ , r , _ , _ = precision_recall_fscore_support (y_true , y_pred ,
1517
1535
labels = labels ,
0 commit comments