liveBook · Manning

F-score

As explained in Deep Learning with PyTorch, Second Edition

A harmonic-mean classification metric that balances precision and recall, with the F-score parameter changing the relative emphasis between them.

A classifier can look accurate while missing every positive case. In nodule-candidate classification, a model that calls everything negative scores well on a heavily negative dataset but does not find nodules. The F1 score gives precision and recall a joint place in the evaluation. section 14.0

F1 is the harmonic mean of precision and recall: 2 × precision × recall ÷ (precision + recall). It ranges from 0 to 1 when defined. Unlike an ordinary average, it cannot reward a classifier for perfect performance on one measure when the other is zero. section 14.3.4

Why not just average?

Precision 1.0 with recall 0.0 has an arithmetic average of 0.5—the same as precision 0.5 with recall 0.5. F1 gives the first pair 0 and the second 0.5, making the failure to find positives visible. section 14.3.4

Listing 14.3 Calculating F1 score
metrics_dict['pr/f1_score'] = \
  2 * (precision * recall) / (precision + recall)

The short Python listing calculates F1 from precision and recall for the training metrics. section 14.3.4

When the score is undefined

An initial training run reported 99.8% correct while identifying no positive samples. Both precision and recall were zero, so the F1 calculation divided by zero and logged nan. A high accuracy figure had hidden the failure; the new metrics exposed it. section 14.3.5

Log F1 alongside precision and recall each epoch rather than relying on a single accuracy percentage. F1 is not the only possible balance: implementing F2 and F0.5, then deciding which suits nodule classification, is a further exercise. section 14.3.4 section 14.8

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains the arithmetic-average comparison and implements F1 in Python, then diagnoses undefined scores when precision and recall vanish.
Examples
Contrasts two precision-recall pairs and examines a nodule classifier reporting 99.8% accuracy but nan F1.
What it assumes
Introduces precision and recall before combining them into F1.
What you can do after
Build it from scratch using the F1 formula, log it by epoch, and diagnose undefined results.
How it connects
Links precision and recall to classification thresholds, class imbalance, and data balancing.

Also explained in

Compare how 14 books explain f-score →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.