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

Viewed 130

I got this error when I want to get the prediction accuracy and I try every possible way and all stack issues but finally I can not solve the bugs...
the code snippest with bug is:

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))

to know the shapes, the value of data is:

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]

how can I handle the bugs???

1 Answers

The error happens because you are passing to accuracy_score a 2D matrix (TestAuthorHot is a 2D one-hot matrix of labels). accuracy_score accepts only 1D vectors, so you need to transform TestAuthorHot into 1D in order to match it with author_pred1 (which is 1D)

To do this u can simply do:

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