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

2024-10-03 19:33:13 发布

您现在位置:Python中文网/ 问答频道 /正文

当我想获得预测精度时,我遇到了这个错误,我尝试了所有可能的方法和所有堆栈问题,但最终我无法解决错误…
带有bug的snippest代码是:

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]

我如何处理这些虫子


Tags: testsupportdatatypenpclassprecisionauthor
1条回答
网友
1楼 · 发布于 2024-10-03 19:33:13

发生此错误的原因是,您正在向accuracy_score传递一个二维矩阵(TestAuthorHot是标签的二维热矩阵)accuracy_score只接受1D向量,因此需要将TestAuthorHot转换为1D,以便与author_pred1(即1D)匹配

要做到这一点,您只需执行以下操作:

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

相关问题 更多 >