close

[Solved] ValueError: pos_label=1 is not a valid label: array([‘neg’, ‘pos’], dtype=’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ValueError: pos_label=1 is not a valid label: array([‘neg’, ‘pos’], dtype='<U3′) in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ValueError: pos_label=1 is not a valid label: array([‘neg’, ‘pos’], dtype='<U3′) Error Occurs?

Today I get the following error ValueError: pos_label=1 is not a valid label: array([‘neg’, ‘pos’], dtype='<U3′) in python.

How To Solve ValueError: pos_label=1 is not a valid label: array([‘neg’, ‘pos’], dtype='<U3′) Error ?

  1. How To Solve ValueError: pos_label=1 is not a valid label: array(['neg', 'pos'], dtype='

    To Solve ValueError: pos_label=1 is not a valid label: array(['neg', 'pos'], dtype='<U3') Error When you face this error it means the values of your target variable are not the expected one for recall_score(), which by default are 1 for positive case and 0 for negative case [This also applies to precision_score()]

  2. ValueError: pos_label=1 is not a valid label: array(['neg', 'pos'], dtype='

    To Solve ValueError: pos_label=1 is not a valid label: array(['neg', 'pos'], dtype='<U3') Error When you face this error it means the values of your target variable are not the expected one for recall_score(), which by default are 1 for positive case and 0 for negative case [This also applies to precision_score()]

Solution 1

recall_average = recall_score(Y_test, y_predict, average="binary", pos_label="neg")

Use "neg" or "pos" as pos_label and this error won’t raise again.

Solution 2

When you face this error it means the values of your target variable are not the expected one for recall_score(), which by default are 1 for positive case and 0 for negative case [This also applies to precision_score()]

From the error you mentioned:

pos_label=1 is not a valid label: array(['neg', 'pos']

It is clear that values for your positive scenarios is pos instead of 1 and for the negative neg instead of 0.

Then you have to options to fix this mismatch:

  • Changing the value default in the recall_score() to consider positive scenarios when pos appears with:
recall_average = recall_score(Y_test, y_predict, average="binary", pos_label='pos') 
  • Changing the values of the target variable in your dataset to be 1 or 0
Y_test = Y_test.map({'pos': 1, 'neg': 0}).astype(int)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read