EZ LearnEZ Learn
Courses
EZ Board
Dashboard
All articles
ml-for-cyber-securitymlcsprecisionrecall

Precision, Recall, and F1: Metrics Built for Imbalance

Precision asks: of everything flagged malicious, how much actually was? Recall asks: of everything actually malicious, how much did the model catch? A real Random Forest's precision-recall curve on this unit's own 5 percent task scored an average precision of 0.54, against a random classifier's 0.05.

Ashish Revar25 October 20265 min read1 views

ML concept: Reading a Confusion Matrix

Every classification metric in this unit is computed from four counts: true positives (TP, correctly flagged malicious), false positives (FP, legitimate wrongly flagged), true negatives (TN, correctly passed), and false negatives (FN, malicious wrongly passed). Unit 2's confusion matrix was built from exactly these four counts.

MetricFormulaWhat it answers
PrecisionTP / (TP + FP)Of everything flagged malicious, how much actually was? High precision means few false alarms.
RecallTP / (TP + FN)Of everything actually malicious, how much did the model catch? High recall means few misses.
F1-score2 · (Precision · Recall) / (Precision + Recall)The harmonic mean of precision and recall, a single number balancing both.

These are the three metrics every security classifier should be reported with, alongside accuracy, never instead of it, but never only accuracy either.

precision_recall_f1.py

from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score

y_pred = model.predict(X_test)
print(f"Accuracy:  {accuracy_score(y_test, y_pred):.3f}")
print(f"Precision: {precision_score(y_test, y_pred):.3f}")
print(f"Recall:    {recall_score(y_test, y_pred):.3f}")
print(f"F1-score:  {f1_score(y_test, y_pred):.3f}")

Caution: Precision and Recall Trade Against Each Other

Lowering a classifier's decision threshold catches more true positives (raising recall) but also lets in more false positives (lowering precision), and raising the threshold does the reverse. There is no threshold that maximises both at once; the right balance depends on the cost of a miss versus the cost of a false alarm in your specific deployment, a judgement call no metric makes for you.

Tool in focus: Precision-Recall Curve, Not Just ROC

Unit 2 used the ROC curve (true positive rate against false positive rate). On severely imbalanced data, ROC can look deceptively good, because the false positive rate stays low even when the raw count of false positives is large, relative to the small number of true positives. A precision-recall curve, plotting precision against recall across thresholds, is the more honest picture when the positive class is rare, exactly the situation fraud and intrusion detection are almost always in.

precision_recall_curve.py

from sklearn.metrics import precision_recall_curve, average_precision_score

proba = model.predict_proba(X_test)[:, 1]
precision, recall, thresholds = precision_recall_curve(y_test, proba)
ap = average_precision_score(y_test, proba)
print(f"Average precision: {ap:.3f}")
RecallPrecisionRandom classifier (AP = 0.05)Random Forest (AP = 0.54)
A real Random Forest's precision-recall curve on a task where only 5% of examples are positive. The dotted line is what a random classifier would score; the gap between the two curves is the model's genuine, measured contribution

Try it yourself: Find Your Own Operating Point

Using the thresholds array from the code above, find the threshold that gives at least 90% recall, then report the precision at that exact threshold. This is the real workflow a fraud or intrusion-detection team uses to set a production decision threshold: start from the recall the business can tolerate, then see what precision, and therefore what false-alarm rate, that recall costs.

Related to this topic

Continue learning

Reference material

eBook

ML for Cyber Security eBook

v1.0

Open resource

Cheatsheet

ML for Cyber Security Cheatsheet

v1.0

Open resource

MCQ Bank

ML for Cyber Security MCQ Bank

v1.0

Open resource

Question Bank

ML for Cyber Security Question Bank

v1.0

Open resource

Discussion

Loading comments…
More articlesTest your knowledge
EZ MentorAI · Free

Precision, Recall, and F1: Metrics Built for Imbalance

Hi! I'm EZ Mentor

Your AI tutor for
Precision, Recall, and F1: Metrics Built for Imbalance

Sign in to ask, it's free.

EZ Mentor is AI-powered · Answers may not be perfect · 10 free questions / day

EZ Learn

Structured learning across Reverse Engineering, Cloud Security and Research Methodology, plus games and practice tools. Free, and open to anyone.

Learn

  • Courses
  • EZ Articles
  • EZ Watch
  • EZ Bytes
  • EZ Talks
  • Community

Library & Exams

  • All Resources
  • eBooks & PDFs
  • Question Banks
  • Cheatsheets
  • All Exams
  • EZ Board

EZ Arena

  • EZ Games
  • Byte Rush
  • Firewall Defence
  • 3D Case Files
  • Live Quiz
  • ML Challenges

EZ Cyber Hygiene

  • Hygiene Journey
  • Cyber Safety Check
  • दिशा - Hygiene
  • सजग - Awareness
  • अभ्यास - Practice
  • Scammed? Call 1930

Platform

  • About
  • Contact
  • Events
  • Verify Certificate
  • Terms of Use
  • Disclaimer

© 2026 EZ Learn by EpochZero. Educational content for learning purposes.

Platform Lead: Ashish Revar