What Is the F1 Score in Machine Learning?
Learn what the F1 score is, how it balances precision and recall, why it beats plain accuracy on imbalanced datasets, and how the F-beta variant generalizes it.
Expected Interview Answer
The F1 score is the harmonic mean of precision and recall, combining both into a single number between 0 and 1 that is high only when a classifier achieves both few false positives and few false negatives.
Precision measures how many predicted positives were actually correct, while recall measures how many actual positives were successfully found; optimizing for one alone can trivially inflate it at the expense of the other, so F1 punishes that imbalance because the harmonic mean is dragged down by whichever of the two is lower. It is especially useful on imbalanced datasets, where accuracy alone can look deceptively high just by predicting the majority class. Variants like F-beta let you weight recall more or less heavily than precision depending on which type of error is costlier for the task.
- Balances precision and recall into one interpretable number
- Far more informative than raw accuracy on imbalanced datasets
- Punishes models that sacrifice one metric to inflate the other
- F-beta variants let you weight the metric toward business priorities
- Widely used as a standard benchmark across classification tasks
AI Mentor Explanation
The F1 score is like judging a bowler by combining accuracy (how many deliveries actually hit the target line) with coverage (how many dangerous batters they actually dismissed), rather than either alone. A bowler who nails a narrow line but takes no wickets, or takes wickets only with wild deliveries, would still score poorly on this combined measure.
Step-by-Step Explanation
Step 1
Compute precision
Divide true positives by all predicted positives (true positives plus false positives).
Step 2
Compute recall
Divide true positives by all actual positives (true positives plus false negatives).
Step 3
Take the harmonic mean
F1 = 2 * (precision * recall) / (precision + recall), which is pulled down heavily by whichever metric is lower.
Step 4
Compare against accuracy on imbalanced data
Check whether accuracy alone is misleadingly high due to a dominant majority class, which F1 exposes.
Step 5
Consider F-beta for weighted priorities
Use an F-beta variant to weight recall more heavily than precision, or vice versa, based on which error type is costlier.
What Interviewer Expects
- Can state the formula and explain harmonic mean over arithmetic mean
- Explains precision and recall clearly and independently
- Knows why F1 is preferred over raw accuracy on imbalanced datasets
- Understands F1 punishes sacrificing one metric to inflate the other
- Can mention F-beta as a way to weight precision versus recall
Common Mistakes
- Confusing F1 score with plain accuracy
- Using an arithmetic mean instead of the harmonic mean of precision and recall
- Optimizing only for precision or only for recall and ignoring the trade-off
- Using F1 blindly without considering which error type is actually costlier for the business
Best Answer (HR Friendly)
“The F1 score is a single number that balances two ideas: how often a model's positive predictions are correct, and how many actual positive cases it actually catches. It is especially useful when the data is imbalanced, such as detecting rare fraud cases, where plain accuracy can be misleading.”
Code Example
from sklearn.metrics import precision_score, recall_score, f1_score
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1, 1, 0]
print("Precision:", precision_score(y_true, y_pred))
print("Recall:", recall_score(y_true, y_pred))
print("F1 score:", f1_score(y_true, y_pred))Follow-up Questions
- Why does F1 use the harmonic mean instead of the arithmetic mean?
- When would you prioritize recall over precision, or the reverse?
- What is the F-beta score and how does it generalize F1?
- Why can accuracy be misleading on an imbalanced dataset?
- How is F1 extended to multi-class classification problems?
MCQ Practice
1. What does the F1 score combine?
The F1 score is the harmonic mean of precision and recall, combining both into one number.
2. Why is F1 often preferred over accuracy for imbalanced datasets?
On imbalanced data, a model predicting only the majority class can achieve high accuracy while performing poorly on the minority class, which F1 exposes.
3. Why does F1 use the harmonic mean rather than the arithmetic mean?
The harmonic mean is dragged down by the smaller of the two values, so F1 punishes models that sacrifice one metric to inflate the other.
Flash Cards
What is the F1 score? — The harmonic mean of precision and recall, combining both into a single score between 0 and 1.
What does precision measure? — The proportion of predicted positives that were actually correct.
What does recall measure? — The proportion of actual positives that were successfully identified.
Why prefer F1 over accuracy on imbalanced data? — Accuracy can be misleadingly high by favoring the majority class, while F1 requires balance between precision and recall.