Handling imbalanced Dataset
රෝගීන්ගෙන් 99%ක්ම රෝගයක් නෑ නම්, හැම වෙලාවෙම "healthy" කියලා predict කරන model එකකට accuracy 99%ක් තියෙනවා, ඒත් මුළුමනින්ම useless — මෙතන accuracy එකෙන් විතරක් බැලුවොත් රැවටෙනවා.
If 99% of patients don't have a disease, a model that always predicts "healthy" is 99% accurate and completely useless — accuracy alone lies to you here.
Lesson 18 of 64 · Part 1 · Python & Data Basics · Data Pre-processing · bilingual notes (English + සිංහල)
Why this matters. If 99% of patients don't have a disease, a model that always predicts "healthy" is 99% accurate and completely useless — accuracy alone lies to you here.
සිංහලෙන්: රෝගීන්ගෙන් 99%ක්ම රෝගයක් නෑ නම්, හැම වෙලාවෙම "healthy" කියලා predict කරන model එකකට accuracy 99%ක් තියෙනවා, ඒත් මුළුමනින්ම useless — මෙතන accuracy එකෙන් විතරක් බැලුවොත් රැවටෙනවා.
What you'll use this for. Any real medical, fraud, or churn dataset (several projects later in this course) is imbalanced by nature — this lesson is what makes those models trustworthy.
සිංහලෙන්: සැබෑ medical, fraud, churn dataset ඕනම එකක් (course එකේ පස්සේ එන projects ගොඩක්) ස්වභාවයෙන්ම imbalanced — ඒ models විශ්වාස කරන්න පුළුවන් කරන්නෙ මේ පාඩම.
Key principles
- Oversampling and undersampling fix the training data, not the truth. Techniques like SMOTE create more examples of the rare class so the model is forced to actually learn its pattern, instead of ignoring it.
Oversampling සහ undersampling fix කරන්නෙ training data එක, ඇත්තම truth එක නෙවෙයි
Walkthrough
Imbalanced Dataset:
A dataset with an unequal class distribution
# importing the dependencies
import numpy as np
import pandas as pd# loading the dataset to pandas DataFrame
credit_card_data = pd.read_csv('/content/credit_data.csv')# first 5 rows of the dataframe
credit_card_data.head()| Time | V1 | V2 | V3 | V4 | V5 | V6 | … | |
|---|---|---|---|---|---|---|---|---|
| 0 | 0.0 | -1.359807 | -0.072781 | 2.536347 | 1.378155 | -0.338321 | 0.462388 | … |
| 1 | 0.0 | 1.191857 | 0.266151 | 0.166480 | 0.448154 | 0.060018 | -0.082361 | … |
| 2 | 1.0 | -1.358354 | -1.340163 | 1.773209 | 0.379780 | -0.503198 | 1.800499 | … |
| 3 | 1.0 | -0.966272 | -0.185226 | 1.792993 | -0.863291 | -0.010309 | 1.247203 | … |
| 4 | 2.0 | -1.158233 | 0.877737 | 1.548718 | 0.403034 | -0.407193 | 0.095921 | … |
credit_card_data.tail()| Time | V1 | V2 | V3 | V4 | V5 | V6 | … | |
|---|---|---|---|---|---|---|---|---|
| 284802 | 172786.0 | -11.881118 | 10.071785 | -9.834783 | -2.066656 | -5.364473 | -2.606837 | … |
| 284803 | 172787.0 | -0.732789 | -0.055080 | 2.035030 | -0.738589 | 0.868229 | 1.058415 | … |
| 284804 | 172788.0 | 1.919565 | -0.301254 | -3.249640 | -0.557828 | 2.630515 | 3.031260 | … |
| 284805 | 172788.0 | -0.240440 | 0.530483 | 0.702510 | 0.689799 | -0.377961 | 0.623708 | … |
| 284806 | 172792.0 | -0.533413 | -0.189733 | 0.703337 | -0.506271 | -0.012546 | -0.649617 | … |
# distribution of the two classes
credit_card_data['Class'].value_counts()Output
0 284315
1 492
Name: Class, dtype: int64This is Highly Imbalanced Dataset
0 --> Legit Transactions
1 --> Fraudulent Transactions
# separating the legit and fraudulent transactions
legit = credit_card_data[credit_card_data.Class == 0]
fraud = credit_card_data[credit_card_data.Class == 1]print(legit.shape)
print(fraud.shape)Output
(284315, 31)
(492, 31)Under-sampling
Build a sample dataset containing similar distribution of Legit & Fraudulent Transactionds
Number of Fraudulent Transactions --> 492
legit_sample = legit.sample(n=492)print(legit_sample.shape)Output
(492, 31)Concatenate the Two DataFrames
new_dataset = pd.concat([legit_sample, fraud], axis = 0)new_dataset.head()| Time | V1 | V2 | V3 | V4 | V5 | V6 | … | |
|---|---|---|---|---|---|---|---|---|
| 13990 | 24862.0 | 1.154741 | 0.060236 | 0.823157 | 0.849650 | -0.523112 | -0.288493 | … |
| 112504 | 72680.0 | -0.371048 | 1.046455 | 1.177242 | -0.125334 | 0.252280 | -0.488885 | … |
| 9156 | 13061.0 | -1.018111 | 0.933478 | 1.843735 | 1.279705 | 0.600320 | -0.971149 | … |
| 238704 | 149784.0 | 1.433162 | -2.885140 | -1.843543 | -1.454722 | -1.240728 | 0.086185 | … |
| 193795 | 130293.0 | 2.036488 | -0.185607 | -1.431561 | 0.127259 | 0.320670 | -0.207806 | … |
new_dataset.tail()| Time | V1 | V2 | V3 | V4 | V5 | V6 | … | |
|---|---|---|---|---|---|---|---|---|
| 279863 | 169142.0 | -1.927883 | 1.125653 | -4.518331 | 1.749293 | -1.566487 | -2.010494 | … |
| 280143 | 169347.0 | 1.378559 | 1.289381 | -5.004247 | 1.411850 | 0.442581 | -1.326536 | … |
| 280149 | 169351.0 | -0.676143 | 1.126366 | -2.213700 | 0.468308 | -1.120541 | -0.003346 | … |
| 281144 | 169966.0 | -3.113832 | 0.585864 | -5.399730 | 1.817092 | -0.840618 | -2.943548 | … |
| 281674 | 170348.0 | 1.991976 | 0.158476 | -2.583441 | 0.408670 | 1.151147 | -0.096695 | … |
new_dataset['Class'].value_counts()Output
1 492
0 492
Name: Class, dtype: int64Keep going: ← Lesson 17 · Lesson 19 → · Track it in the Self Guide
Finished this one?
Part of Self Guide · Stage 13: ML 1 — Python & Data Basics. Progress is saved in this browser.



