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

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

我正在尝试使用自变量(阿拉伯语句子)和因变量(多类但使用 One Hot 编码技术)来预测模型。我将 Tokenizer 技术用于训练和测试集

模特:

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

问题是当我使用 score = f1_score(y_test, ynew, average='weighted') 作为评估时。它显示以下错误:

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

ynewy_test 值如下:

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)

f1_score() 的两个参数必须采用相同的格式:one-hot 编码或标签编码。您不能传递两个不同编码的参数。使用以下选项之一。

选项 1:您可以将 ynew 转换为 one-hot 编码。

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

选项 2:您可以使用 LabelBinarizer 将 y_new 转换为 one-hot 编码。

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

选项 3:您可以将 y_test 从 one-hot 编码转换为标签编码。

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