Here is my code:

score = metrics.f1_score(y_test[0:], y_pred, pos_label=list(set(y_test)))

And here are my dimensions/shapes, which I print before executing the score line (the line producing the error), and they get printed:

Original df shape:  (6944, 13)
x_train (Training Features) Shape: (4860, 12)
y_train (Training Labels) Shape: (4860,)
x_test (Testing Features) Shape: (2084, 12)
y_test (Testing Labels) Shape: (2084,)
features length: 38
Accuracy: 0.731765834933
y_pred shape:  (2084,)
[1 1 1 ..., 1 0 0]
y_test shape:  (2084,)
1330    1
2543    1
many other 0,1 values here! Deleted for the post clarity
3025    0
5776    1 

I am getting following error:

ValueError                                Traceback (most recent call last)
<ipython-input-440-9059291258bf> in <module>()
     44 from sklearn import metrics
---> 46 score = metrics.f1_score(y_test[0:], y_pred, pos_label=list(set(y_test)))
     48 #print(y_pred)
ValueError: all the input arrays must have same number of dimensions

So I am giving wrong dimensions to metrics.f1_score function. How could I pass the y_test and y_pred in the right form?


1 Answer 1


I think you misinterpreted the meaning of pos_label. It is used for choosing the positive label; its default value is set to $1$, which means the method calculate precision, recall and therefore f1 score by assuming class 1 as positive class, and class 0 as negative class. You seem to interpret it as possible labels, because you input unique class labels in the test set. Just remove pos_label argument from your call.

  • $\begingroup$ But with the dimensions (and shapes) I have, is this part ( y_test[0:], y_pred, ) right? $\endgroup$
    – ZelelB
    Nov 25, 2018 at 20:37
  • $\begingroup$ yes, it seems right. You also don't need 0: in y_test. Just input y_test. $\endgroup$
    – gunes
    Nov 25, 2018 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.