Skip to content

Commit 5dd9bfe

Browse files
jnothmanamueller
authored andcommitted
[MRG + 1] DOC comment on measures in classification_report (scikit-learn#7897)
* DOC comment on measures in classification_report * Detail what kind of average. * Describe relationship to sensitivity/specificity in the binary case Also reimplemented formatting to take advantage of py 2.7 * FIX more care with unicode * COSMIT remove unnecessary parens * DOC typo micro->macro
1 parent 31d3c3e commit 5dd9bfe

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

sklearn/metrics/classification.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,14 @@ def classification_report(y_true, y_pred, labels=None, target_names=None,
13651365
report : string
13661366
Text summary of the precision, recall, F1 score for each class.
13671367
1368+
The reported averages are a prevalence-weighted macro-average across
1369+
classes (equivalent to :func:`precision_recall_fscore_support` with
1370+
``average='weighted'``).
1371+
1372+
Note that in binary classification, recall of the positive class
1373+
is also known as "sensitivity"; recall of the negative class is
1374+
"specificity".
1375+
13681376
Examples
13691377
--------
13701378
>>> from sklearn.metrics import classification_report
@@ -1391,42 +1399,35 @@ class 2 1.00 0.67 0.80 3
13911399
last_line_heading = 'avg / total'
13921400

13931401
if target_names is None:
1394-
target_names = ['%s' % l for l in labels]
1402+
target_names = [u'%s' % l for l in labels]
13951403
name_width = max(len(cn) for cn in target_names)
13961404
width = max(name_width, len(last_line_heading), digits)
13971405

13981406
headers = ["precision", "recall", "f1-score", "support"]
1399-
fmt = '%% %ds' % width # first column: class name
1400-
fmt += ' '
1401-
fmt += ' '.join(['% 9s' for _ in headers])
1402-
fmt += '\n'
1403-
1404-
headers = [""] + headers
1405-
report = fmt % tuple(headers)
1406-
report += '\n'
1407+
head_fmt = u'{:>{width}s} ' + u' {:>9}' * len(headers)
1408+
report = head_fmt.format(u'', *headers, width=width)
1409+
report += u'\n\n'
14071410

14081411
p, r, f1, s = precision_recall_fscore_support(y_true, y_pred,
14091412
labels=labels,
14101413
average=None,
14111414
sample_weight=sample_weight)
14121415

1413-
for i, label in enumerate(labels):
1414-
values = [target_names[i]]
1415-
for v in (p[i], r[i], f1[i]):
1416-
values += ["{0:0.{1}f}".format(v, digits)]
1417-
values += ["{0}".format(s[i])]
1418-
report += fmt % tuple(values)
1416+
row_fmt = u'{:>{width}s} ' + u' {:>9.{digits}f}' * 3 + u' {:>9}\n'
1417+
rows = zip(target_names, p, r, f1, s)
1418+
for row in rows:
1419+
report += row_fmt.format(*row, width=width, digits=digits)
14191420

1420-
report += '\n'
1421+
report += u'\n'
14211422

14221423
# compute averages
1423-
values = [last_line_heading]
1424-
for v in (np.average(p, weights=s),
1425-
np.average(r, weights=s),
1426-
np.average(f1, weights=s)):
1427-
values += ["{0:0.{1}f}".format(v, digits)]
1428-
values += ['{0}'.format(np.sum(s))]
1429-
report += fmt % tuple(values)
1424+
report += row_fmt.format(last_line_heading,
1425+
np.average(p, weights=s),
1426+
np.average(r, weights=s),
1427+
np.average(f1, weights=s),
1428+
np.sum(s),
1429+
width=width, digits=digits)
1430+
14301431
return report
14311432

14321433

0 commit comments

Comments
 (0)