ValueError:分类指标无法处理多标签指标和多类目标的混合
nlp 218
原文标题 :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
ynewandy_testvalues 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)
回复
我来回复-
devidduma 评论
该回答已被采纳!
两个参数 off1_score() 必须采用相同的格式:单热编码或标签编码。您不能传递两个不同编码的参数。使用以下选项之一。
选项 1:您可以将 new 转换为 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 converty_newto one-hot encoding。
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:您可以将 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')
2年前