Skip to content

    Confusion Matrix, Precision & Recall

    Machine Learning · Beginner

    Definition

    A confusion matrix counts TP, FP, FN and TN; precision and recall are computed from it.

    Accuracy alone misleads on imbalanced data.

    Diagram

    TP 40 | FP 10FN 20 | TN 30

    Flowchart

    Count TP, FP, FN, TNPrecision = TP/(TP+FP)Recall = TP/(TP+FN)F1 = harmonic mean

    Example: Numerical

    tp, fp, fn = 40, 10, 20
    p, r = tp / (tp + fp), tp / (tp + fn)
    print(p, round(r, 3), round(2 * p * r / (p + r), 3))

    Output: 0.8 0.667 0.727

    Key points

    • High recall matters for disease detection
    • High precision matters for spam filtering

    Interview questions

    When prefer recall?

    When missing a positive is costly, such as cancer screening.

    More Machine Learning topics