จัดการข้อมูล Imbalanced ใน Scikit-learn

Weerasak Thachai
EspressoFX Notebook
1 min readDec 22, 2016

Classification Algorithms ส่วนใหญ่จะให้ผลลัพธ์ที่น่าพอใจก็ต่อเมื่อจำนวนของกลุ่มตัวอย่าง (Dataset) ของแต่ละกลุ่มมีจำนวนพอๆ กัน หากชุดข้อมูลมีความเบ้สูงคือจำนวนชุดข้อมูลแต่ละชุดมีปริมาณที่แตกต่างกันมากหรืออาจเรียกว่าไม่สมดุล (Imbalanced) การประมวลผลข้อมูลที่ไม่สมดูลจึงเป็นสิ่งที่ท้าทายว่าเราจะทำอย่างไรเพื่อทำให้ข้อมูลเหล่านี้ ประมวลผลได้มีประสิทธิภาพที่ดีขึ้น

เทคนิคในการจัดการกับข้อมูลที่ไม่สมดุลแบ่งเป็น 3 วิธีหลักๆ

  • Sampling methods เป็นวิธีการสุ่มตัวอย่างซึ่งเป็นวิธีทางสถิติ มีจุดประสงค์เพื่อทำให้ข้อมูลแต่ละกลุ่มมีประมาณที่สมดุลกัน โดยแบ่งเป็น 2 วิธีหลักๆ คือ
  • Under-sampling สุ่มลดจำนวนข้อมูลกลุ่มหลัก (Major class) ให้พอๆ กับข้อมูลกลุ่มน้อย (Minor class)
  • Over-sampling สุ่มเพิ่มจำนวนข้อมูลกลุ่มน้อย (Minor class) ขึ้นให้พอๆ กับข้อมูลกลุ่มหลัก (Major class)
  • Cost-sensitive methods หลักการคือพิจารณาจากค่าความผิดพลาดจากการแบ่งกลุ่ม (Misclassifiying examles)
  • Kernel-based methods หลักการนี้คือการย้ายตำแหน่งข้อมูลที่ไม่สามารถแบ่งกลุ่มได้ในระนาบปกติ โดยการเพิ่มมิติให้สูงขึ้นจนสามารถแบ่งกลุ่มข้อมูลออกจากกันได้

ใน SKlearn ไม่ได้มีเครื่องสำหรับจัดการข้อมูล Imbalanced โดยเฉพาะดังนี้ต้องติดตั้ง imbalanced-learn ก่อน

git clone https://github.com/scikit-learn-contrib/imbalanced-learn.git
cd imbalanced-learn
python setup.py instal

ยกตัวอย่างเฉพาะการทำ Sampling methods ส่วนวิธีอื่นๆ หากมีเวลาจะยกตัวอย่างมาเขียนอีกครั้ง

ตัวอย่าง Over-sampling ด้วย Random over-sampling

"""
====================
Random over-sampling
====================
"""
from sklearn.datasets import make_classification
from imblearn.over_sampling import RandomOverSampler
# Generate the dataset
X, y = make_classification(n_classes=2, class_sep=2, weights=[0.1, 0.9],
n_informative=3, n_redundant=1, flip_y=0,
n_features=20, n_clusters_per_class=1,
n_samples=5000, random_state=10)
# Apply the random over-sampling
ros = RandomOverSampler()
X_resampled, y_resampled = ros.fit_sample(X, y)
ตัวอย่าง Over-sampling ด้วย Synthetic Minority Over-sampling (SMOTE)"""
=============================
SMOTE regular and svm methods
=============================
"""
from sklearn.datasets import make_classification
from imblearn.over_sampling import SMOTE
# Generate the dataset
X, y = make_classification(n_classes=2, class_sep=2, weights=[0.1, 0.9],
n_informative=3, n_redundant=1, flip_y=0,
n_features=20, n_clusters_per_class=1,
n_samples=5000, random_state=10)
# Apply SMOTE (regular, svm)
sm = SMOTE(kind='regular') #SMOTE regular method
#sm = SMOTE(kind='svm') #SMOTE svm method

X_resampled, y_resampled = sm.fit_sample(X, y)
X_res_vis = pca.transform(X_resampled)
ตัวอย่าง Under-sampling ด้วย Random under-sampling"""
=====================
Random under-sampling
=====================
"""
from sklearn.datasets import make_classification
from imblearn.under_sampling import RandomUnderSampler
# Generate the dataset
X, y = make_classification(n_classes=2, class_sep=2, weights=[0.1, 0.9],
n_informative=3, n_redundant=1, flip_y=0,
n_features=20, n_clusters_per_class=1,
n_samples=5000, random_state=10)
# Apply the random under-sampling
rus = RandomUnderSampler()
X_resampled, y_resampled = rus.fit_sample(X, y)
นอกจากนี้ยังมีอีกหลายวิธี ลองศึกษาได้ที่อ้างอิงจาก

--

--