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

Imbalanced Data

Imbalanced data is any classification dataset where one class vastly outnumbers another — fraud, disease, defects, churn — and it breaks the usual assumptions most ML workflows quietly rely on, starting with accuracy itself.

The Reference Dataset for This Section

1000 transactions: 950 legitimate (class 0), 50 fraudulent (class 1) — a 5% positive rate, a realistic order of magnitude for many real fraud/defect/disease problems.

Why Accuracy Lies Here — The Reminder

As covered in Accuracy, a model that predicts "not fraud" for every single transaction achieves 95% accuracy on this dataset while catching zero fraud — a completely useless model with an impressive-looking headline number. This single fact is why every technique in this section exists.

Graphical Intuition

950 Class 0 (legit) 50 Class 1 (fraud)

The minority class bar is barely visible next to the majority — exactly why a model can ignore it almost entirely and still score well on accuracy.

The Three Families of Fixes

ApproachHow It WorksFull Note
ResamplingChange the training data's class balance directlyOversampling, Undersampling, SMOTE
AlgorithmicMake the model itself penalize minority-class mistakes moreClass Weights
EvaluationUse metrics that aren't fooled by imbalance in the first placeImbalanced Classification Metrics

Minimal Working Example

from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y   # ALWAYS stratify on imbalanced data
)

model = LogisticRegression(class_weight="balanced")   # one of the simplest fixes
model.fit(X_train, y_train)
print(classification_report(y_test, model.predict(X_test)))

Practical Use Cases

  • Fraud detection, disease screening, manufacturing defect detection, churn prediction
  • Any classification problem where the class you actually care about is the rare one

Common Mistakes

  • Reporting accuracy as the headline metric without checking class balance first.
  • Forgetting stratify=y in the train/test split, risking an even more skewed distribution in one of the splits.
  • Applying resampling to the test set — test data should always reflect the real, naturally imbalanced distribution the model will face in production.

Interview Relevance

Q: "Your fraud model has 95% accuracy on a dataset that's 95% legitimate transactions. What's your first question?" What's the recall on the fraud class specifically — a model could be achieving that accuracy by predicting "not fraud" for nearly everyone, catching little to no actual fraud, which the accuracy number alone would completely hide.

Practice Question

You're building a manufacturing defect detector where only 2% of items are defective. Name three distinct approaches (one from each family above) you'd consider, and explain the tradeoff each involves.

Want hands-on practice with real imbalanced datasets? CodingNow 2.0's Data Science course covers fraud/defect detection projects with genuinely imbalanced data.

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 Data – FAQs

Quick answers about learning Imbalanced Data in Machine Learning.

This free note from CodingNow 2.0 explains Imbalanced Data 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 Data, is 100% free with no signup required.
With focused practice, most students grasp Imbalanced Data 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