-
Imbalanced Classification기계학습이론과실습 2022. 4. 25. 13:27
특정 클래스에 대한 관측치의 수가 상대적으로 더 많거나 적은 경우, minority class에 대해서 recall과 precision 값이 좋지 못하다. 그런데 사기, 질병, 기계 고장 등 많은 경우 majority class가 아닌 minority class가 주요 관심 대상이다. 따라서 minority class에 대한 recall, precision, F1, AUC 값을 높이기 위해 학습 데이터에 대해 클래스 간 데이터 수의 균형을 맞추거나 비용함수 내 클래스 간 가중치를 다르게 둘 필요가 있다.
- 데이터 수를 맞추기 위한 첫 번째 방법은 minority class의 데이터를 더 수집하는 것이다.
- 이것이 어려울 경우 over-sampling 혹은 under-sampling 방법을 사용할 수 있다. over-sampling의 경우 이미 존재하는 데이터 포인트를 복사하여 사용하는 것이며, under-sampling의 경우 majority data 일부를 삭제하는 것이다.
- over-sampling의 경우 과적합 문제를 야기할 수 있으며, 기존에 존재하는 데이터 포인트를 그대로 copy & paste 하는 것은 새로운 정보를 추가하지 못한다. 따라서 SMOTE(Synthetic Minority Oversampling Technique)나 최신 기술인 Border-line SMOTE, ADASYN(Adaptive Synthetic Sampling)을 사용하여 새로운 데이터를 추가함으로 해당 문제를 일부분 완화할 수 있다.
- 샘플링 방법을 사용하지 않을 경우 cost penalized model(cost sensitive training)을 사용할 수 있으며, minority class 예측을 실패하는 경우 추가 cost를 부여할 수 있다.
* SMOTE
아래 과정을 반복함
1) minority class의 데이터 포인트 x1 무작위로 선택
2) 해당 데이터 포인트와 유사도가 가장 큰 관측치 K개 선택
3) r1 = x1+(gap*diff), r2 = x1+(gap*diff) ... rk = x1+(gap*diff)
gap = randomly chosen value between 0 to 1diff = x1 - k1
import imblearn
from imblearn.over_sampling import SMOTE
smt = SMOTE()
X_new, y_new = smt.fit_resample(X, y)* SMOTE with under-sampling
from imblearn.under_sampling import RandomUnderSampler
smt1 = SMOTE(sampling_strategy=0.1) #10% of data points with majority class
undersample = RandomUnderSampler(sampling_strategy=0.5) # minority class가 majority의 50%가 되도록X_new1, y_new1 = smt1.fit_resample(X, y)
X_new2, y_new2 = undersample.fit_resample(X_new1, y_new1)
그러나 SMOTE는 한 선에서 여러 개의 데이터 포인트를 추출함으로 feature space를 잘 반영하지 못한다.
또한, majority data와 섞인 minority data를 만들어냄으로 많은 noisy data points를 생성하게 된다.
* Borderline SMOTE
모든 minority data points가 아니라 borderline에 있는 minority data points를 사용함
from imblearn.over_sampling import BorderlineSMOTE
bdsmt = BorderlineSMOTE()
X_border, y_border = bdsmt.fit_resample(X, y)
* ADASYN
minority class 관측치 주변에 majority class 관측치가 많을 수록 더 많이 oversampling 하는 방법
from imblearn.over_sampling import ADASYN
adasyn = ADASYN()
X_ads, y_ads = adasyn.fit_resample(X, y)
* cost sensitive training
weights = {0:1.0, 1:2.0}
lr_cost_sensitive = LogisticRegression(penalty='none', class_weight=weights)lr_cost_sensitive.fit(X_train1, y_train1)
'기계학습이론과실습' 카테고리의 다른 글
Course Intro (0) 2022.05.04 Naive Bayes (0) 2022.05.03 Document Classification (0) 2022.04.25 Hyper-parameter Tuning (0) 2022.04.20 Logistic Regression (0) 2022.04.18