F1 Score

What Is F1 Score?

F1 score is an evaluation metric used in classification. It combines precision and recall into one score.

Key Concepts
  • Precision — Out of all predicted positives, how many were actually positive?
  • Recall — Out of all actual positives, how many did the model find?
  • F1 Score — A balance between precision and recall

A high F1 score means the model has both strong precision and strong recall.

Why F1 Score Matters

F1 score matters because accuracy alone can be misleading, especially when one class is much more common than another.

Example — Fraud Detection Dataset
  • Normal Transactions: 99%
  • Fraud Transactions: 1%
  • Model predicts everything as normal
  • Accuracy: 99%
  • Fraud Recall: 0%

High accuracy hides poor fraud detection.

Precision Review

Precision measures how many predicted positives were actually positive. It is important when false positives are costly.

Precision Formula

Precision = True Positives / (True Positives + False Positives)

In spam detection, if an email is predicted as spam, precision tells us how likely it is to actually be spam. High precision means fewer normal emails are marked as spam.

Recall Review

Recall measures how many actual positives were found. It is important when false negatives are costly.

Recall Formula

Recall = True Positives / (True Positives + False Negatives)

In disease detection, if a patient truly has a disease, recall tells us how likely the model is to catch that case. High recall means fewer actual disease cases are missed.

F1 Score Formula

The F1 score is the harmonic mean of precision and recall.

Formula

F1 Score = 2 × (Precision × Recall) / (Precision + Recall)

  • Score ranges from 0 to 1
  • 0 — poor performance
  • 1 — perfect precision and perfect recall

Why Harmonic Mean?

F1 score uses the harmonic mean instead of a simple average because the harmonic mean punishes uneven scores.

Example
  • Precision: 1.00
  • Recall: 0.10
  • Simple Average: 0.55
  • F1 Score: much lower than 0.55

A model should not get a strong F1 score if either precision or recall is very weak.

F1 Score Example

Suppose a model has 80% precision and 60% recall.

Calculation
  • Precision: 0.80
  • Recall: 0.60
  • F1 = 2 × (0.80 × 0.60) / (0.80 + 0.60) = 2 × 0.48 / 1.40 = 0.96 / 1.40
  • F1 Score: 68.6%

This score reflects the balance between precision and recall.

High F1 Score

A high F1 score means both precision and recall are strong.

Example
  • Precision: 90%
  • Recall: 88%
  • F1 Score: about 89%

The model makes trustworthy positive predictions and finds most actual positives. A high F1 score is useful when both false positives and false negatives matter.

Low F1 Score

A low F1 score means precision, recall, or both are weak.

High Precision, Low Recall
  • Precision: 90%
  • Recall: 20%
  • The model misses many actual positives
  • F1 score will be low
Low Precision, High Recall
  • Precision: 20%
  • Recall: 90%
  • Many positive predictions are wrong
  • F1 score will also be low

F1 Score and Accuracy

Accuracy measures overall correctness. F1 score focuses on the balance between precision and recall.

Accuracy Asks

Out of all predictions, how many were correct?

F1 Score Asks

How well does the model balance finding positives and making correct positive predictions?

F1 score is often better than accuracy when classes are imbalanced.

F1 Score vs Precision

Precision focuses only on the quality of positive predictions. F1 score includes precision but also considers recall.

When Precision Alone Falls Short

A model with high precision but low recall makes trustworthy positive predictions, but misses many actual positives.

F1 score will show that the model still has a weakness.

F1 Score vs Recall

Recall focuses only on finding actual positives. F1 score includes recall but also considers precision.

When Recall Alone Falls Short

A model with high recall but low precision finds many actual positives, but also creates many false positives.

F1 score will show that the model still has a weakness.

When F1 Score Is Useful

F1 score is useful when both false positives and false negatives matter.

F1 Score Is Useful When
  • Classes are imbalanced
  • Accuracy is misleading
  • The positive class is important
  • Both false positives and false negatives matter
  • You need one score balancing precision and recall
  • You care about finding positives without too many false alarms
Examples
  • Fraud detection
  • Spam detection
  • Disease screening
  • Safety detection
  • Search results
  • Recommendation systems

When F1 Score May Not Be Enough

F1 score is useful, but it does not show every detail of model performance.

F1 Score Does Not Show
  • Exact number of false positives
  • Exact number of false negatives
  • Performance on each class
  • Performance across different groups
  • How confident the model is
  • Whether one error type is much worse than another

If false positives and false negatives have very different costs, precision and recall should also be reviewed separately.

F1 Score in Imbalanced Datasets

F1 score is especially useful for imbalanced datasets because it focuses on positive-class performance.

Fraud Dataset

Fraud cases are rare. Most transactions are normal, so accuracy can look high without catching any fraud.

F1 score focuses on whether the model correctly finds the fraud class — making it more informative than accuracy for rare but important positives.

F1 Score in Spam Detection

In spam detection, both false positives and false negatives matter.

Spam Detection Tradeoffs
  • False Positive — A normal email is marked as spam
  • False Negative — A spam email reaches the inbox
  • Precision — Helps avoid false positives
  • Recall — Helps catch spam
  • F1 Score — Balances both

F1 Score in Medical Diagnosis

In medical diagnosis, recall is often very important, but precision also matters because false alarms can create stress or unnecessary follow-up.

Example
  • Precision: 75%
  • Recall: 90%

F1 score shows how balanced the model is overall. For high-risk healthcare tasks, precision and recall should still be reviewed separately.

F1 Score in Fraud Detection

In fraud detection, recall helps catch fraud, while precision helps avoid blocking normal transactions.

High Recall + Low Precision

Many fraud cases are caught, but many normal transactions are flagged.

High Precision + Low Recall

Fraud alerts are trustworthy, but too much fraud is missed.

F1 score helps evaluate the balance.

F1 Score and Confusion Matrix

F1 score is based on values from the confusion matrix. It uses true positives, false positives, and false negatives.

What F1 Uses
  • True Positives
  • False Positives
  • False Negatives

F1 does not directly use True Negatives. When true negatives dominate the dataset, accuracy may look high, but F1 score focuses on positive-class performance.

Confusion Matrix Example

This example shows how precision, recall, and F1 score can be calculated from confusion matrix values.

Calculation
  • True Positives: 40
  • False Positives: 10
  • False Negatives: 20
  • Precision = 40 / (40 + 10) = 0.80
  • Recall = 40 / (40 + 20) = 0.67
  • F1 Score = 2 × (0.80 × 0.67) / (0.80 + 0.67) ≈ 0.73

Perfect F1 Score

A perfect F1 score means both precision and recall are perfect.

Perfect Score
  • Precision: 1.00
  • Recall: 1.00
  • F1 Score: 1.00
  • No false positives
  • No false negatives
  • All positive predictions are correct
  • All actual positives are found

Perfect F1 scores are rare in real-world problems.

Zero F1 Score

An F1 score can be 0 when the model fails to correctly identify positive examples.

Example

A fraud model predicts every transaction as normal.

  • Fraud Found: 0
  • True Positives: 0
  • F1 Score for Fraud: 0

Macro F1 Score

Macro F1 calculates the F1 score for each class separately and then averages them. Each class is treated equally.

Example
  • Class A F1: 0.90
  • Class B F1: 0.60
  • Class C F1: 0.75
  • Macro F1: Average of all three

Macro F1 is useful when you care about performance across all classes, including smaller classes.

Micro F1 Score

Micro F1 combines all true positives, false positives, and false negatives across classes before calculating F1.

Micro F1

Combines results across all classes first and gives more weight to larger classes.

Micro F1 is useful when overall performance across all examples matters. In some cases, micro F1 can behave similarly to accuracy.

Weighted F1 Score

Weighted F1 calculates the F1 score for each class and averages them based on how many examples each class has.

Weighted F1

Larger classes have more influence. If Class A has many examples and Class B has few, Class A affects weighted F1 more.

Weighted F1 is useful when classes are imbalanced but you still want a score that reflects class size.

Choosing Macro, Micro, or Weighted F1

F1 TypeBest Used When
Macro F1Every class should matter equally
Micro F1Overall example-level performance matters
Weighted F1Classes are imbalanced and class size matters

F1 Score and Thresholds

Many classification models output probabilities. A threshold decides when the model predicts positive.

Example
  • Fraud Probability: 0.72
  • Default Threshold: 0.50
  • If probability is above 0.50, predict fraud

Changing the threshold can affect precision, recall, and F1 score. A threshold that maximizes F1 may not be the same as the one that maximizes accuracy.

Threshold Tradeoff

Lowering or raising the threshold changes the balance between precision and recall.

Lower Threshold
  • More positive predictions
  • Higher recall
  • Lower precision
Higher Threshold
  • Fewer positive predictions
  • Higher precision
  • Lower recall

F1 score helps find a balance. The best threshold should match the real-world cost of mistakes.

Common Mistakes

Common Mistakes
  • Confusing F1 score with accuracy
  • Thinking F1 only measures correctness
  • Ignoring precision and recall separately
  • Using F1 when one error type matters much more
  • Ignoring the confusion matrix
  • Ignoring class-level performance
  • Comparing F1 scores without knowing the dataset
  • Assuming high F1 means the model is safe to deploy

Summary

Key Takeaways
  • F1 score balances precision and recall.
  • Precision measures how many predicted positives were actually positive.
  • Recall measures how many actual positives were found.
  • F1 score formula is 2 × (precision × recall) / (precision + recall).
  • F1 score is useful when classes are imbalanced.
  • F1 score is useful when both false positives and false negatives matter.
  • F1 score is often better than accuracy for rare positive classes.
  • F1 score does not directly use true negatives.
  • Macro, micro, and weighted F1 are used for multi-class problems.
  • F1 score should be interpreted with precision, recall, and the confusion matrix.

Practice Prompt

A model has a precision of 0.75 and a recall of 0.60.

Calculate the F1 score. Then explain what the score means and why it is useful to look at F1 score instead of accuracy alone.

Need Help?

Ask the AI if you need help understanding F1 score, precision, recall, harmonic mean, confusion matrices, thresholds, macro F1, micro F1, or weighted F1.