Skip to content

Commit f8b2c6f

Browse files
committed
add imbalance learning tutorial
1 parent 7b60b6a commit f8b2c6f

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

Diff for: machine-learning/imbalance-learning/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# [Imbalance Learning With Imblearn and Smote Variants Libraries in Python](https://www.thepythoncode.com/article/handling-imbalance-data-imblearn-smote-variants-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- Download and extract `creditcard.csv` from this [Kaggle link](https://www.kaggle.com/mlg-ulb/creditcardfraud)
5+
- Then, you're free to run `imb_learning.py` as you wish.

Diff for: machine-learning/imbalance-learning/imb_learning.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import numpy as np
2+
import pandas as pd
3+
from imblearn.under_sampling import RandomUnderSampler,EditedNearestNeighbours,InstanceHardnessThreshold,TomekLinks
4+
from imblearn.over_sampling import RandomOverSampler
5+
import smote_variants as sv
6+
7+
8+
df=pd.read_csv("creditcard.csv")
9+
y=df["Class"]
10+
X=df.drop(["Time","Class"],axis=1)
11+
print(y.value_counts())
12+
13+
14+
under=RandomUnderSampler()
15+
X_und,y_und=under.fit_resample(X,y)
16+
print(len(X_und[X_und==1])==len(X_und[X_und==0]))
17+
18+
19+
over=RandomOverSampler()
20+
X_und,y_und=over.fit_resample(X,y)
21+
print(len(X_und[X_und==1])==len(X_und[X_und==0]))
22+
23+
24+
under_samp_models=[EditedNearestNeighbours(),InstanceHardnessThreshold(),TomekLinks()]
25+
for under_samp_model in under_samp_models:
26+
X_und,y_und=under_samp_model.fit_resample(X,y)
27+
print(X_und.shape)
28+
29+
svs=[sv.kmeans_SMOTE(),sv.Safe_Level_SMOTE(),sv.SMOTE_Cosine()]
30+
for over_sampler in svs:
31+
X_over_samp, y_over_samp= over_sampler.sample(X, y)
32+
print(X_over_samp.shape)

Diff for: machine-learning/imbalance-learning/requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
numpy
2+
pandas
3+
imblearn
4+
smote-variants

0 commit comments

Comments
 (0)