【问题标题】:Oversampling multiclass data failing using ADASYN algorithm使用 ADASYN 算法对多类数据进行过采样失败
【发布时间】:2020-12-29 22:57:50
【问题描述】:

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

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

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

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

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

但这显然没有发生。

【问题讨论】:

    标签: python python-3.x machine-learning scikit-learn imblearn


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2018-01-13
      • 1970-01-01
      • 2019-05-09
      • 2021-11-07
      • 1970-01-01
      • 2020-12-31
      • 2018-07-09
      • 1970-01-01
      • 2019-09-16
      相关资源
      最近更新 更多