Diwank Singh commited on
Commit
9ce5e9c
1 Parent(s): 41f5e2c

Add balancing script

Browse files

Signed-off-by: Diwank Singh <diwank.singh@gmail.com>

Files changed (1) hide show
  1. balance.py +14 -0
balance.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Balance dataset by oversampling using SMOTEN (ideal for categorical data)
2
+ # (https://imbalanced-learn.org/stable/references/generated/imblearn.over_sampling.SMOTEN.html)
3
+ from imblearn.over_sampling import SMOTEN
4
+
5
+ ros = SMOTEN(n_jobs=14) # Resampled on a 16-core, 60GB RAM machine
6
+
7
+ df_balanced_text, df_balanced_labels = ros.fit_resample(train_df[["text_a", "text_b"]], train_df["labels"])
8
+ df_balanced_text["labels"] = df_balanced_labels
9
+
10
+ print(f"""Balanced dataset size multiplier:
11
+ {len(df_balanced_labels) / len(train_df)}""") # 4.07
12
+
13
+ # Need to cache the balanced data
14
+ df_balanced_text.to_hdf("./balanced.h5", "data", complevel=9)