0

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

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)))

注意,glass.csv来自这个链接

目的是平衡以下类别的不平衡:

(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.

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

ADASYN 文档指出: 'not majority': resample all classes but the majority class;

但这显然没有发生。

4

1 回答 1

1

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

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)
于 2020-10-09T12:33:46.240 回答