gpt4 book ai didi

python - 如何处理ValueError : Classification metrics can't handle a mix of multilabel-indicator and multiclass targets error

转载 作者:行者123 更新时间:2023-12-04 15:11:08 24 4
gpt4 key购买 nike

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

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]

我该如何处理错误???

最佳答案

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

要做到这一点,你可以简单地做:

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

关于python - 如何处理ValueError : Classification metrics can't handle a mix of multilabel-indicator and multiclass targets error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65181653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com