当我想要获取预测准确率时遇到了这个错误,我尝试了所有可能的方法和所有堆栈问题,但最终还是无法解决这个错误…
带有错误的代码片段是:
author_pred1 = model1.predict([ThreeGramTest, ThreeGramTest, ThreeGramTest,ThreeGramTest])print("class prediction without argmax:",author_pred1)author_pred1=np.argmax(author_pred1, axis=1)# Evaluateprint("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 hereprecision, 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)