使用 ADASYN 算法对多类数据进行过采样失败

Oversampling multiclass data failing using ADASYN algorithm

我在下面有一个非常基本的脚本来演示这个问题:

from imblearn.over_sampling import ADASYN
import pandas as pd, numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split


data = pd.read_csv('glass.csv')
classes = data.values[:, -1]
data = data.iloc[:, :-1]

adasyn = ADASYN(sampling_strategy='not majority', random_state=8, n_neighbors=3)

new_data, new_classes = adasyn.fit_resample(data, classes)

X_train, X_test, y_train, y_test = train_test_split(new_data, new_classes, test_size = 0.20)

rfc = RandomForestClassifier()
rfc.fit(X_train, y_train)
print("Score: {}".format(rfc.score(X_test, y_test)))

Note, glass.csv comes from this link

目的是平衡以下 class 失衡:

(214, 10)
Class=1, Count=70, Percentage=32.710%
Class=2, Count=76, Percentage=35.514%
Class=3, Count=17, Percentage=7.944%
Class=5, Count=13, Percentage=6.075%
Class=6, Count=9, Percentage=4.206%
Class=7, Count=29, Percentage=13.551%

拥有相等(或接近相等)的样本。然而,运行 上面的代码产生:

ValueError: No samples will be generated with the provided ratio settings.

ADASYNsampling_strategy 更改为 minority 成功地对 minority class、6 进行了过采样,并将其带到 74 样本,但仍然使剩余的 class 不平衡。因此,我正在寻找一种使用 ADASYN 所有少数 classes 完全过采样的方法。

ADASYN documentation states: 'not majority': resample all classes but the majority class;

但这显然没有发生。

为了解决这个问题,我所做的是对除两个主要多数 类 之外的所有样本进行重新采样,并通过以下方式继续这样做:

adasyn = ADASYN(sampling_strategy='minority', random_state=8, n_neighbors=3)

new_data = data
new_classes = classes

for i in range(len(classes)-2):
    new_data, new_classes = adasyn.fit_resample(data, classes)