artemis
artemis

Reputation: 7261

Oversampling multiclass data failing using ADASYN algorithm

I have a very basic script below to demo the problem:

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

The intention would be to balance the below class imbalances:

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

To have equal (or near equal) samples. However, running the code above produces:

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

Changing ADASYN's sampling_strategy to minority successfully oversamples the minority class, 6, and brings it to 74 samples, but still leaves the remaining classes imbalanced. Thus, I am looking for a way to completely oversample all minority classes using ADASYN.

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

But that clearly is not happening.

Upvotes: 1

Views: 2391

Answers (1)

artemis
artemis

Reputation: 7261

To fix this, what I did was resampled all but the two major majority classes, and continued to do so via:

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)

Upvotes: 2

Related Questions