简体   繁体   English

ValueError:分类指标无法处理多类和多标记指标目标的混合

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

I have Multi class labeled text classification problem with 2000 different labels. 我有多个标记的文本分类问题,有2000个不同的标签。 Doing classification using LSTM with Glove Embedding. 使用LSTM和Glove Embedding进行分类。

  1. Label Encoder of target variable 标签目标变量的编码器
  2. LSTM layer with Embedd Layer 带有嵌入层的LSTM层
  3. Error metric is F2 score 错误指标是F2分数

LabelEncoded target variable: LabelEncoded目标变量:

le = LabelEncoder()  
le.fit(y)
train_y = le.transform(y_train)
test_y = le.transform(y_test)

LSTM network is like below with Glove Embeddings LSTM网络就像下面的Glove Embeddings

np.random.seed(seed)
K.clear_session()
model = Sequential()
model.add(Embedding(max_features, embed_dim, input_length = X_train.shape[1],
         weights=[embedding_matrix]))#,trainable=False
model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(num_classes, activation='softmax'))
model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')
print(model.summary())
My error metric is F1 score. 我的错误指标是F1得分。 I build below function for Error metric 我为Error指标构建了以下函数
ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets

Model fit is 模型适合

 model.fit(X_train, train_y, validation_data=(X_test, test_y),epochs=10, batch_size=64, callbacks=[metrics]) 

Getting below error after 1st epoch: 在第1纪元后获得以下错误:

 ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets 

Can you please tell me where I did mistake in my code? 你能告诉我在我的代码中哪里出错吗?

I tried a lot to resolve my self, but I did not get any clue. 我尝试了很多来解决我的自我,但我没有得到任何线索。 Can you please help me on this 你能帮我解决这个问题吗?

F1 score, recall and precision are metrics for binary classification for using it in a multiclass/multilabel problem you need to add a parameter to your function f1_score , recall_score and precision_score . F1得分,回忆和精确度是二元分类的度量标准,用于在多类/多标记问题中使用它,您需要在函数f1_scorerecall_scoreprecision_score添加参数。

Try with this : 试试这个:

_val_f1 = f1_score(val_targ, val_predict, average='weighted')
_val_recall = recall_score(val_targ, val_predict, average='weighted')
_val_precision = precision_score(val_targ, val_predict, average='weighted')

Find more information for the average parameter here : 在此处查找有关平均参数的更多信息:
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html

Your problem is caused by presence of continuous values in val_predict in this line of code 您的问题是由此代码行中val_predict中存在连续值引起的

_val_f1 = f1_score(val_targ, val_predict)

You should round your predictions in val_predict before calculating f1_score. 在计算f1_score之前,您应该在val_predict中对预测进行舍入。

Example solution: 示例解决方案

 _val_f1 = f1_score(val_targ,np.round(val_predict))

Want to mention: If you want to change the threshold of the round function (0.5 default.) you can add or subtract values in [0,1] interval: 想提及:如果要更改圆函数的阈值(默认值为0.5),可以在[0,1]区间中添加或减去值:

>>> a = np.arange(0,1,0.1)
>>> print(a, abs(np.round(a-0.1)), sep='\n')
>>> print(a, abs(np.round(a+0.3)), sep='\n')

[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
array([0.  0.  0.  0.  0.  0.  1.  1.  1.  1.])

[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
array([0., 0., 0., 1., 1., 1., 1., 1., 1., 1.])

Hope that helps! 希望有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何处理 ValueError:分类指标无法处理多标签指标和多类目标错误的混合 - how to handle ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets error 如何修复 ValueError:分类指标无法处理模型的多类和多标签指标目标的混合? - How to fix ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets for model? ValueError:分类指标无法在 ROC 曲线计算中处理多类和多标签指标目标的混合 - ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets in ROC curve calculation 错误:分类指标无法处理多类多输出和多标记指标目标的混合 - Error: Classification metrics can't handle a mix of multiclass-multioutput and multilabel-indicator targets 混淆矩阵错误“分类指标无法处理多标签指标和多类目标的混合” - confusion matrix error "Classification metrics can't handle a mix of multilabel-indicator and multiclass targets" 分类指标无法处理多类和多标签指标目标的混合 - Classification metrics can't handle a mix of multiclass and multilabel-indicator targets ValueError:分类指标无法处理多标签指标和连续多输出目标错误的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets error ValueError:分类指标无法处理多标签指标和连续多输出目标的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets ValueError:分类指标无法处理多标签指标和连续多输出目标 sklearn 的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets sklearn ValueError:分类指标无法处理多标签指标和二进制目标的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and binary targets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM