繁体   English   中英

如何处理 ValueError:分类指标无法处理多标签指标和多类目标错误的混合

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

当我想获得预测准确性时出现此错误,我尝试了所有可能的方法和所有堆栈问题,但最后我无法解决错误......
带有错误的代码片段是:

author_pred1 = model1.predict([ThreeGramTest, ThreeGramTest, ThreeGramTest,ThreeGramTest])
print("class prediction without argmax:",author_pred1)
author_pred1=np.argmax(author_pred1, axis=1)
# Evaluate
print("test data one hot lable", TestAuthorHot)
print("class prediction with argmax:",author_pred1)
# author_pred1 = author_pred1.astype("int64")
print("type of prediction output",type(author_pred1))
print("type of test data", type(TestAuthorHot))
print(np.array(np.unique(author_pred1, return_counts=True)).T)
print(np.array(np.unique(TestAuthorHot, return_counts=True)).T)
# accuracy = accuracy_score(TestAuthorHot, author_pred1.round(), normalize=False)# the bug is here
precision, recall, f1, support = score(TestAuthorHot, author_pred1)
ave_precision = np.average(precision, weights=support / np.sum(support))
ave_recall = np.average(recall, weights=support / np.sum(support))

要知道形状,数据的值是:

class prediction without argmax: [[3.9413989e-02 8.4685171e-03 2.7781539e-03 ... 5.0324947e-03
  6.2263450e-07 3.1461464e-10]
 [1.1533947e-02 4.0361892e-02 1.4060171e-02 ... 4.7175577e-05
  1.4333490e-01 2.0528505e-07]
 [4.5363868e-06 3.1557463e-03 1.4047540e-02 ... 1.3272668e-03
  4.6724287e-07 5.9454552e-10]
 ...
 [1.9417159e-04 1.7364822e-02 2.9031632e-03 ... 5.0036388e-04
  1.3315305e-04 9.0704253e-07]
 [1.8054984e-09 2.9453583e-08 2.3744430e-08 ... 2.7137769e-03
  7.7114571e-08 4.9026494e-10]
 [7.8946296e-06 5.9516740e-05 8.2868773e-10 ... 3.1905161e-04
  2.5262805e-06 2.0384558e-09]]
test data one hot lable [[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 1 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 1 ... 0 0 0]]
class prediction with argmax: [ 7 37 37 ... 39  4  4]

我该如何处理这些错误???

发生错误是因为您将 2D 矩阵传递给accuracy_scoreTestAuthorHot是标签的 2D one-hot 矩阵)。 accuracy_score仅接受一维向量,因此您需要将TestAuthorHot转换为一维以便与author_pred1 (即一维)匹配

为此,您可以简单地执行以下操作:

accuracy_score(np.argmax(TestAuthorHot, axis=1), author_pred1)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM