猿问

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

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


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

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

(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,并将其带入样本,但仍然使其余类不平衡。因此,我正在寻找一种使用 ADASYN对所有少数类别进行完全过采样的方法。sampling_strategyminorityminority674

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

但这显然没有发生。


慕的地8271018
浏览 227回答 1
1回答

千巷猫影

为了解决这个问题,我所做的是对除两个主要多数类之外的所有类进行重新采样,并继续通过以下方式进行:adasyn = ADASYN(sampling_strategy='minority', random_state=8, n_neighbors=3)new_data = datanew_classes = classesfor i in range(len(classes)-2):    new_data, new_classes = adasyn.fit_resample(data, classes)
随时随地看视频慕课网APP

相关分类

Python
我要回答