'ValueError: Unknown label type: 'unknown' Keras to_categorical

I have a problem with this code. I have an error saying: ValueError: Unknown label type: 'unknown'.

First I changed RandomUnderSampler(ratio='majority') to RandomUnderSampler(sampling_strategy='majority')

then with the line: X_trainRus, Y_trainRus = random_under_sampler.fit_sample(X_trainFlat, y_train) I changed random_under_sampler.fit_resample

The final code is:

from tensorflow.keras.utils import to_categorical

#One-Hot-Encode y_train and y_test
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

#One-Hot-Encode y_train and y_test
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

X_trainShape = X_train.shape[1]*X_train.shape[2]*X_train.shape[3]
X_testShape = X_test.shape[1]*X_test.shape[2]*X_test.shape[3]
X_trainFlat = X_train.reshape(X_train.shape[0], X_trainShape)
X_testFlat = X_test.reshape(X_test.shape[0], X_testShape)

from imblearn.under_sampling import RandomUnderSampler
random_under_sampler = RandomUnderSampler(sampling_strategy='majority') #change ratio to sampling_strategy
X_trainRus, Y_trainRus = random_under_sampler.fit_resample(X_trainFlat, y_train)
X_testRus, Y_testRus = random_under_sampler.fit_resample(X_testFlat, y_test)

# One-hot-encoding
Y_trainRusHot = to_categorical(Y_trainRus, num_classes = 2)
Y_testRusHot = to_categorical(Y_testRus, num_classes = 2)

#np.unique(Y_trainRus, return_counts=True) #checking the number of samples in each class to make sure RandomUnderSampling worked

for i in range(len(X_trainRus)):
    height, width, channels = 50,50,3
    X_trainRusReshaped = X_trainRus.reshape(len(X_trainRus),height,width,channels)

for i in range(len(X_testRus)):
    height, width, channels = 50,50,3
    X_testRusReshaped = X_testRus.reshape(len(X_testRus),height,width,channels)

I don't know what to change next to remove this error ValueError: Unknown label type: 'unknown'



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source