🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #214

Imbalanced Classification Metrics

Beyond precision, recall and F1 (already covered in Classification Metrics), two additional metrics — Balanced Accuracy and the Matthews Correlation Coefficient — are specifically built to stay honest on imbalanced data.

Balanced Accuracy

\[ \text{Balanced Accuracy} = \frac{\text{Recall}_{\text{class 0}} + \text{Recall}_{\text{class 1}}}{2} \]

Instead of counting every sample equally (which lets the majority class dominate, exactly like plain accuracy), balanced accuracy averages each class's own recall — treating both classes' performance as equally important, regardless of how many samples each contains.

Worked Example

Confusion matrix: \(TP=30, FN=20\) (50 actual fraud cases), \(FP=50, TN=900\) (950 actual legitimate cases).

\[ \text{Accuracy} = \frac{30+900}{1000} = 0.93 \] \[ \text{Recall}_{\text{fraud}} = \frac{30}{50} = 0.6, \qquad \text{Recall}_{\text{legit}} = \frac{900}{950} \approx 0.947 \] \[ \text{Balanced Accuracy} = \frac{0.6+0.947}{2} \approx \mathbf{0.774} \]

Plain accuracy (0.93) looks excellent; balanced accuracy (0.774) tells a much more honest story — the model is only catching 60% of actual fraud, a fact plain accuracy almost completely hides behind the huge, easy-to-get-right majority class.

from sklearn.metrics import balanced_accuracy_score, accuracy_score

y_true = [1]*50 + [0]*950
y_pred = [1]*30 + [0]*20 + [1]*50 + [0]*900

print(accuracy_score(y_true, y_pred))            # 0.93
print(balanced_accuracy_score(y_true, y_pred))    # 0.774

Matthews Correlation Coefficient (MCC)

\[ MCC = \frac{TP \times TN - FP \times FN}{\sqrt{(TP+FP)(TP+FN)(TN+FP)(TN+FN)}} \]

MCC uses all four confusion matrix cells simultaneously and produces a value between -1 (total disagreement) and +1 (perfect prediction), with 0 meaning no better than random — widely regarded as one of the single most balanced, hard-to-game classification metrics, especially for imbalanced data.

\[ MCC = \frac{(30)(900) - (50)(20)}{\sqrt{(80)(50)(950)(920)}} = \frac{27000-1000}{\sqrt{3{,}496{,}000{,}000}} = \frac{26000}{59127} \approx \mathbf{0.440} \]
from sklearn.metrics import matthews_corrcoef

print(matthews_corrcoef(y_true, y_pred))   # 0.440

MCC's moderate value (0.44) — neither near 0 (useless) nor near 1 (excellent) — accurately reflects a model that's meaningfully better than chance but still missing a real chunk of the minority class, a more nuanced picture than accuracy's misleadingly high 0.93.

Why These Metrics Resist the Imbalance Trap

MetricWhy It's Robust to Imbalance
Balanced AccuracyAverages per-class recall — the majority class can't dominate the average just by being larger
MCCUses all four confusion matrix cells in a single formula — a model that ignores the minority class can't score well, unlike plain accuracy

Practical Use Cases

  • Reporting a single, hard-to-game summary metric for genuinely imbalanced classification problems
  • Comparing models fairly when class balance differs across datasets or over time

Common Mistakes

  • Still reporting only plain accuracy as the headline number, even after computing these more honest metrics elsewhere in the analysis.
  • Interpreting MCC on the same [0,1] scale as accuracy — remember it can be negative, indicating predictions worse than random.

Interview Relevance

Q: "Why is MCC considered more reliable than F1-score for some imbalanced problems?" F1 only combines precision and recall, ignoring the true negative count entirely; MCC incorporates all four confusion matrix values (TP, TN, FP, FN) in one balanced formula, making it harder for a model to score well by exploiting a specific error pattern that F1 alone might not fully penalize.

Practice Question

Given \(TP=8, FN=2, FP=5, TN=85\) (10 actual positives, 90 actual negatives), compute balanced accuracy and compare it to plain accuracy for this confusion matrix.

Want to practice evaluating models on real imbalanced datasets? CodingNow 2.0's Data Science course covers this through hands-on, business-framed projects.

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Imbalanced Classification Metrics – FAQs

Quick answers about learning Imbalanced Classification Metrics in Machine Learning.

This free note from CodingNow 2.0 explains Imbalanced Classification Metrics in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Imbalanced Classification Metrics, is 100% free with no signup required.
With focused practice, most students grasp Imbalanced Classification Metrics in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now