close

[Solved] Facing ValueError: Target is multiclass but average=’binary’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Facing ValueError: Target is multiclass but average=’binary’ 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 Facing ValueError: Target is multiclass but average=’binary’ Error Occurs?

Today I get the following error Facing ValueError: Target is multiclass but average=’binary’ in python.

How To Solve Facing ValueError: Target is multiclass but average=’binary’ Error ?

  1. How To Solve Facing ValueError: Target is multiclass but average='binary' Error ?

    To Solve Facing ValueError: Target is multiclass but average='binary' Error Replace 'micro' with any one of the above options except 'binary'. Also, in the multiclass setting, there is no need to provide the 'pos_label' as it will be anyways ignored.

  2. Facing ValueError: Target is multiclass but average='binary'

    To Solve Facing ValueError: Target is multiclass but average='binary' Error Replace 'micro' with any one of the above options except 'binary'. Also, in the multiclass setting, there is no need to provide the 'pos_label' as it will be anyways ignored.

Solution 1

You need to add the 'average' param. According to the documentation:

average : string, [None, ‘binary’ (default), ‘micro’, ‘macro’, ‘samples’, ‘weighted’]

This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data:

Do this:

print("Precision Score : ",precision_score(y_test, y_pred, 
                                           pos_label='positive'
                                           average='micro'))
print("Recall Score : ",recall_score(y_test, y_pred, 
                                           pos_label='positive'
                                           average='micro'))

Replace 'micro' with any one of the above options except 'binary'. Also, in the multiclass setting, there is no need to provide the 'pos_label' as it will be anyways ignored.

Update for comment:

Yes, they can be equal. Its given in the user guide here:

Note that for “micro”-averaging in a multiclass setting with all labels included will produce equal precision, recall and F, while “weighted” averaging may produce an F-score that is not between precision and recall.

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