Based on the MLS-C01 exam guideReport an error
Once you've trained a model, you have to answer "is this model good enough" with numbers, and that number is not accuracy alone. Every binary classification metric is derived from one four-cell table.
Predicted: Positive Predicted: Negative
Actual: Positive TP (true positive) FN (false negative)
Actual: Negative FP (false positive) TN (true negative)| Cell | Meaning | Other name | Real-world name |
|---|---|---|---|
| TP | Correctly predicted a positive as positive | — | Caught fraud as fraud |
| TN | Correctly predicted a negative as negative | — | Let a normal transaction through |
| FP | Wrongly predicted a negative as positive | Type I error | False alarm |
| FN | Missed a positive by calling it negative | Type II error | Miss |
"Positive" is usually set as the rare/important class we want to detect (fraud, disease, churn). Which cell is expensive decides the metric choice.
| Domain | When FP occurs | When FN occurs | Which is costlier |
|---|---|---|---|
| Cancer screening | Unnecessary detailed tests, anxiety | Missing the patient puts life at risk | FN |
| Credit card fraud detection | Blocking a legitimate payment, customer complaints | Fraud loss occurs | FN (usually) |
| Spam filter | An important email lost to the spam folder | A few spam emails in the inbox | FP |
| Product recommendation | Trust drops from an off-target recommendation | A good product isn't shown | FP |
| Predictive maintenance | Inspection cost for healthy equipment | A failure left unattended → line stoppage | FN |
On the exam, metric-selection questions usually boil down to "which costs more, FP or FN." If FN — missing a disease — is fatal, prioritize recall; if FP — classifying a normal email as spam — is fatal, prioritize precision. The key is training yourself to translate the business context into one cell of the confusion matrix.
Accuracy = (TP + TN) / (TP + TN + FP + FN)It is the proportion of correct predictions out of all predictions. It's excellent when classes are balanced, but powerless on imbalanced data.
Example: 1% fraud transactions, 99% normal
A dummy model that predicts every transaction as "normal"
→ Accuracy = 99% (yet it catches not a single fraud)That 99% is no evidence at all that the model is useful. Why this happens is revealed immediately by the confusion matrix.
[1% fraud, 10,000 samples / "all normal" dummy model]
Predicted: fraud Predicted: normal
Actual: fraud TP = 0 FN = 100
Actual: normal FP = 0 TN = 9,900
Accuracy = (0 + 9,900) / 10,000 = 99.0% ← looks great
Recall = 0 / (0 + 100) = 0.0% ← the reality
Precision = 0 / (0 + 0) = undefined⚠️ Pitfall: When a passage says "accuracy is 99.5%, so why is it a problem," the answer is almost always "extreme imbalance where predicting only the majority class gives that value." You must compare against the class proportion, not the accuracy figure itself.
Precision = TP / (TP + FP) ← the share that are truly positive among predicted positives
Recall = TP / (TP + FN) ← the share caught among actual positives| Metric | Denominator | Reading direction | What it reduces | Representative situation |
|---|---|---|---|---|
| Precision | Predicted positives (TP+FP) | A column of the confusion matrix | FP | Spam filter, product recommendation |
| Recall | Actual positives (TP+FN) | A row of the confusion matrix | FN | Cancer diagnosis, fraud detection |
| Specificity | Actual negatives (TN+FP) | The negative-side row | FP | Keeping normal as normal |
TN / (TN + FP), and 1 − specificity is the FPR.With precision and recall, raising one usually lowers the other. If you lower the threshold and classify more as Positive, recall goes up but precision drops.
Lower threshold ──────────────────────────────→ Raise threshold
More Positive predictions Fewer Positive predictions
Recall ↑ Precision ↓ Precision ↑ Recall ↓
FN down, FP up FP down, FN up
Extreme: all Positive Extreme: all Negative
Recall = 1.0, Precision = positive rate Recall = 0, Precision = undefinedIf you remember the denominators of precision and recall, you won't get confused. Precision's denominator is "predicted positives (TP+FP)" — column-wise accuracy based on predictions. Recall's denominator is "actual positives (TP+FN)" — row-wise hit rate based on actuals. In other words, precision measures the reliability of predictions, and recall measures the completeness of detection.
F1 = 2 × (Precision × Recall) / (Precision + Recall)It merges precision and recall into one number, but uses the harmonic mean rather than the arithmetic mean. The harmonic mean is more sensitive to the smaller of the two values, so if only one is high and the other is low, the score gets cut sharply.
Precision=0.9, Recall=0.1
Arithmetic mean = 0.5 (looks plausible)
F1 = 2 × (0.9 × 0.1) / (1.0) = 0.18 (reveals the reality)F_β = (1+β²) × P × R / (β²×P + R).| Metric | β | Side emphasized | When |
|---|---|---|---|
| F1 | 1 | Balance | FP and FN costs are similar |
| F2 | 2 | Recall | Detection that must not miss (disease, fraud) |
| F0.5 | 0.5 | Precision | When false positives are expensive (spam, recommendations) |
[Fraud detection / 10,000 samples, 100 actual frauds]
Predicted: fraud Predicted: normal
Actual: fraud TP = 60 FN = 40
Actual: normal FP = 240 TN = 9,660
Accuracy = (60 + 9,660) / 10,000 = 97.2%
Precision = 60 / (60 + 240) = 20.0%
Recall = 60 / (60 + 40) = 60.0%
Specificity = 9,660 / (9,660 + 240) = 97.6%
F1 = 2 × (0.20 × 0.60) / 0.80 = 30.0%| Situation | Class balance | Priority metric | Why accuracy can't be trusted |
|---|---|---|---|
| Fraud detection (0.5% positive) | Extreme imbalance | Recall → F1/F2 | Predicting all normal still gives 99.5% |
| Medical diagnostic screening | Imbalanced | Recall (sensitivity) | Low prevalence inflates accuracy |
| Spam filter | Moderate | Precision → F0.5 | Loss of legitimate emails barely registers in accuracy |
| Recommender system | Imbalanced | Precision (top K) | Most items are unseen, so TN dominates |
| Churn prediction | Imbalanced | Balance of recall + precision (F1) | Low churn rate makes accuracy meaningless |
| Image multiclass (balanced) | Balanced | Accuracy, Macro-F1 | (If balanced, accuracy is also valid) |
| Quality inspection (defect detection) | Extreme imbalance | Recall | If the defect rate is under 1%, accuracy is powerless |
When looking at a metric, you must compare it with "the score a do-nothing model gets." Let the positive rate be p.
| Dummy strategy | Accuracy | Precision | Recall | F1 |
|---|---|---|---|---|
| Predict all Negative | 1 − p (99% if p=0.01) | Undefined | 0 | 0 |
| Predict all Positive | p | p | 1.0 | 2p/(1+p) |
| Random prediction (probability p) | Roughly 1 − 2p(1−p) | p | p | p |
⚠️ Pitfall: The baseline for accuracy is not 0.5 but the majority class proportion. In a problem with p=0.01, an accuracy of 98% is a model worse than the dummy. In contrast, the baselines for precision, recall, and F1 are as low as the level of p, so even on imbalanced data the margin of improvement shows through.
You need to evaluate a classification model
├─ Are the classes roughly balanced? (minority class ≳ 20%)
│ ├─ Yes → accuracy can be used (+ F1 as a supplement)
│ └─ No ↓
├─ Is the cost of FN (misses) overwhelming? (disease, fraud, defects)
│ ├─ Yes → prioritize recall, with a precision floor as a constraint (consider F2 too)
│ └─ No ↓
├─ Is the cost of FP (false positives) overwhelming? (spam, recommendation, auto-blocking)
│ ├─ Yes → prioritize precision (consider F0.5 too)
│ └─ No ↓
└─ Both matter about equally → F1 (Macro-F1 for multiclass)
With three or more classes, you have to decide how to combine per-class precision/recall.
| Method | Computation | Characteristic |
|---|---|---|
| Macro | Simple average of per-class metrics | Treats minority classes equally → when minority classes matter under imbalance |
| Micro | Sum all TP/FP/FN, then compute | Large classes dominate → performance on an all-instances basis |
| Weighted | Average weighted by class sample count | Reflects class proportions |
[Class A: 9,000 / Class B: 500 / Class C: 50]
Macro : (F1_A + F1_B + F1_C) / 3 ← C carries the same weight as A
Weighted : F1 weighted 9000:500:50 ← A effectively decides the result
Micro : sum all TP·FP·FN and compute once ← A dominates here tooIf a passage says "I want to see minority-class performance equally too" and you pick Weighted, it's wrong. Weighted weights by sample count, so the majority class still dominates the result. Equal treatment is Macro only.
Built-in algorithms and Automatic Model Tuning emit these metrics to CloudWatch and the training logs. An example of setting the tuning objective metric to F1 for XGBoost binary classification:
from sagemaker.tuner import HyperparameterTuner
tuner = HyperparameterTuner(
estimator=xgb_estimator,
objective_metric_name="validation:f1", # imbalanced data → optimize f1
objective_type="Maximize",
hyperparameter_ranges=ranges,
max_jobs=20,
max_parallel_jobs=3,
)Setting validation:f1 as the objective metric instead of validation:accuracy prevents the incident where the tuner ignores the imbalance and picks a model that only raises accuracy.
An evaluation metric is not just a scorecard after training ends; it is the compass of hyperparameter tuning. If you set objective_metric_name wrong, the tuner optimizes in exactly the wrong direction. So "what is the right objective metric for this business problem" is a modeling decision you must make before training.
| Passage clue | Answer to pick | Reason |
|---|---|---|
| "Positives are 0.5% yet accuracy is 99.5%" | Accuracy can't be trusted → precision/recall/F1 | A value you get just by predicting the majority class |
| "Missing a patient puts life at risk" | Recall | FN is in the denominator |
| "Great harm if a legitimate email goes to the spam folder" | Precision | FP is in the denominator |
| "We must care about both FP and FN" | F1 score | Balances both via the harmonic mean |
| "I want to emphasize recall more" | F2 (F-beta, β>1) | The larger β is, the more recall is weighted |
| "5 classes, reflect minority classes equally" | Macro average | Simple average of per-class metrics |
| "I'm curious about performance on an all-instances basis" | Micro average | Sum TP/FP/FN, then compute |
| "What should the tuning objective metric be" (imbalanced binary) | validation:f1, Maximize | An accuracy objective ignores the imbalance |
| "Precision 0.95, recall 0.10" | F1 ≈ 0.18, most positives missed | The harmonic mean is sensitive to the lower side |
Tomorrow we cover the ROC curve and PR curve, which visualize the precision-recall trade-off as you adjust the threshold, and AUC.
TN/(TN+FP).Click a choice to reveal the answer and explanation.
Question 1
In a credit card fraud detection model, fraudulent transactions are 0.5% of the total. A model recorded 99.5% accuracy. What is the biggest reason this accuracy can't be trusted?
Question 2
You are evaluating a cancer screening model. Missing a patient (positive) by calling them normal (FN) is the most dangerous. Which metric should take priority?
Question 3
A model has precision 0.95 and recall 0.10. Roughly what is the F1 score that reflects the two in balance, and what does it imply?
Question 4
In a spam filter, sending a legitimate email to the spam folder (FP) does the most harm to users. Which metric should take priority when evaluating the model?
Question 5
In a multiclass problem with 5 classes where some classes have very few samples, you want to evaluate by reflecting minority-class performance equally. What averaging method is appropriate?