機器學習-類別不平衡-上下采樣(Upsampling and Downsampling)

Section I: Brief Introduction on Upsampling/Downsampling

Class imbalance is a quite common problem when working with real-world data-samples from one class or multiple classes are over-represented in a dataset. Intuitively, we can think of several domains where this may occur, such as spam filtering, fraud detection, or screening for diseases.

Here, be warned that one way to deal with imbalanced class proportions during model fitting is a assign a larger penalty to wrong predictions on the minority class . Via scikit-learn, adjusting such a penalty is as convenient as setting the class_weight parameter to class_weight=“balanced”, which is implemented for most classifiers.

FROM
Sebastian Raschka, Vahid Mirjalili. Python機器學習第二版. 南京:東南大學出版社,2018.

Section II: Code and Analyses

第一部分:代碼

from sklearn import datasets
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.utils import resample
import warnings
warnings.filterwarnings("ignore")

#Section 1: Load Breast data, i.e., Benign and Malignant
breast=datasets.load_breast_cancer()
X=breast.data
y=breast.target
print("Originally, the number of each class: for class 0, %d, for class 1, %d" % \
      (np.bincount(y)[0],np.bincount(y)[1]))

#Section 2: Upsample samples with replacement for minority class
print("Number of class 0 samples before: ",X[y==0].shape[0])

X_upsampled,y_upsampled=resample(X[y==0],
                                 y[y==0],
                                 replace=True,
                                 n_samples=X[y==1].shape[0],
                                 random_state=1)
print("Number of class 0 samples after: ",X_upsampled.shape[0])

#Section 2: Downsample samples with replacement for minority class
print("Number of class 1 samples before: ",X[y==1].shape[0])

X_downsampled,y_downsampled=resample(X[y==1],
                                 y[y==1],
                                 replace=True,
                                 n_samples=X[y==0].shape[0],
                                 random_state=1)
print("Number of class 1 samples after: ",X_downsampled.shape[0])

第二部分:結果

Originally, the number of each class: for class 0, 212, for class 1, 357
Number of class 0 samples before:  212
Number of class 0 samples after:  357
Number of class 1 samples before:  357
Number of class 1 samples after:  212

參考文獻
Sebastian Raschka, Vahid Mirjalili. Python機器學習第二版. 南京:東南大學出版社,2018.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章