Galle, LK--:--5/6 sites live

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.

7 May 20264 min readSelf Guide · Stage 13#Python & Data Basics#Data Pre-processing#Machine learning

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

ExamplePython
# importing the dependencies
import numpy as np
import pandas as pd
ExamplePython
# loading the dataset to pandas DataFrame
credit_card_data = pd.read_csv('/content/credit_data.csv')
ExamplePython
# first 5 rows of the dataframe
credit_card_data.head()
TimeV1V2V3V4V5V6…
00.0-1.359807-0.0727812.5363471.378155-0.3383210.462388…
10.01.1918570.2661510.1664800.4481540.060018-0.082361…
21.0-1.358354-1.3401631.7732090.379780-0.5031981.800499…
31.0-0.966272-0.1852261.792993-0.863291-0.0103091.247203…
42.0-1.1582330.8777371.5487180.403034-0.4071930.095921…
ExamplePython
credit_card_data.tail()
TimeV1V2V3V4V5V6…
284802172786.0-11.88111810.071785-9.834783-2.066656-5.364473-2.606837…
284803172787.0-0.732789-0.0550802.035030-0.7385890.8682291.058415…
284804172788.01.919565-0.301254-3.249640-0.5578282.6305153.031260…
284805172788.0-0.2404400.5304830.7025100.689799-0.3779610.623708…
284806172792.0-0.533413-0.1897330.703337-0.506271-0.012546-0.649617…
ExamplePython
# distribution of the two classes
credit_card_data['Class'].value_counts()

Output

Output
0    284315
1       492
Name: Class, dtype: int64

This is Highly Imbalanced Dataset

0 --> Legit Transactions

1 --> Fraudulent Transactions

ExamplePython
# separating the legit and fraudulent transactions
legit = credit_card_data[credit_card_data.Class == 0]
fraud = credit_card_data[credit_card_data.Class == 1]
ExamplePython
print(legit.shape)
print(fraud.shape)

Output

Output
(284315, 31)
(492, 31)

Under-sampling

Build a sample dataset containing similar distribution of Legit & Fraudulent Transactionds

Number of Fraudulent Transactions --> 492

ExamplePython
legit_sample = legit.sample(n=492)
ExamplePython
print(legit_sample.shape)

Output

Output
(492, 31)

Concatenate the Two DataFrames

ExamplePython
new_dataset = pd.concat([legit_sample, fraud], axis = 0)
ExamplePython
new_dataset.head()
TimeV1V2V3V4V5V6…
1399024862.01.1547410.0602360.8231570.849650-0.523112-0.288493…
11250472680.0-0.3710481.0464551.177242-0.1253340.252280-0.488885…
915613061.0-1.0181110.9334781.8437351.2797050.600320-0.971149…
238704149784.01.433162-2.885140-1.843543-1.454722-1.2407280.086185…
193795130293.02.036488-0.185607-1.4315610.1272590.320670-0.207806…
ExamplePython
new_dataset.tail()
TimeV1V2V3V4V5V6…
279863169142.0-1.9278831.125653-4.5183311.749293-1.566487-2.010494…
280143169347.01.3785591.289381-5.0042471.4118500.442581-1.326536…
280149169351.0-0.6761431.126366-2.2137000.468308-1.120541-0.003346…
281144169966.0-3.1138320.585864-5.3997301.817092-0.840618-2.943548…
281674170348.01.9919760.158476-2.5834410.4086701.151147-0.096695…
ExamplePython
new_dataset['Class'].value_counts()

Output

Output
1    492
0    492
Name: Class, dtype: int64

Keep going: ← Lesson 17 · Lesson 19 → · Track it in the Self Guide