Search code examples
pythonkerasnlpevaluation

ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets


I am trying to predict a model using Independent variable (Arabic Sentence) and Dependent variables (Multiclass but using One Hot encoding technique. I used Tokenizer Technique for Train and test set

The Model:

model = Sequential()

model.add(Embedding(num_words,32,input_length=max_length))
model.add(LSTM(64,dropout=0.1))
model.add(Dense(4,activation='sigmoid'))

model.compile(loss='binary_crossentropy',optimizer=optimizer, metrics=['accuracy'])

# some code here

model.fit(train_padded,y_train,epochs=1, validation_data=(test_padded,y_test))

The problem is when I use score = f1_score(y_test, ynew, average='weighted') as evaluation. It shows the following error:

ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets

ynew and y_test values are the following:

ynew= array([2, 1, 3, ..., 3, 0, 1]`, dtype=int64)

y_test = array([[0, 0, 1, 0],
       [0, 1, 0, 0],
       [0, 0, 0, 1],
       ...,
       [0, 0, 0, 1],
       [1, 0, 0, 0],
       [0, 1, 0, 0]], dtype=uint8)

Solution

  • Both arguments of f1_score() must be in the same format: either one-hot encoding or label encoding. You cannot pass two differently encoded arguments. Use one of the following options.

    Option 1: You could convert ynew to one-hot encoding.

    # one-hot encode ynew, before calculating f1_score
    ynew = keras.utils.to_categorical(ynew)
    f1_score(y_test, ynew, average='weighted')
    

    Option 2: You could convert y_new to one-hot encoding using LabelBinarizer.

    from sklearn.preprocessing import LabelBinarizer
    # one-hot encode ynew, before calculating f1_score
    ynew = LabelBinarizer().fit_transform(ynew)
    f1_score(y_test, ynew, average='weighted')
    

    Option 3: You could convert y_test from one-hot encoding to label encoding.

    import numpy as np
    # label encode y_test, before calculating f1_score
    y_test = np.argmax(y_test, axis=1)
    f1_score(y_test, ynew, average='weighted')