[problem solving] target is multiclass but average =’binary ‘. Please choose another average setting

Today, when compiling Python code, we encountered the following error:
target is multiclass but average =’binary ‘. Please choose another average setting, one of [none,’ micro ‘,’ macro ‘,’ weighted ‘]

The original code is as follows, is to require a data set of F1 (precision and recall combined into a single index).

from sklearn.metrics import precision_score, recall_score

precision_score(y_train, y_train_pred)

terms of settlement

Average =’micro’is added to the original code.

from sklearn.metrics import precision_score, recall_score

precision_score(y_train, y_train_pred, average='micro')

The average parameter defines the calculation method of the index. In binary classification, the average parameter is binary by default; in multi classification, the optional parameters are micro, macro, weighted and samples.

None: returns the score of each class. Otherwise, this determines the average type of execution on the data.

Binary: only report the result POS of the specified class_ label。 Only if targets (Y_ Only when {true, PRED}) is binary.

Micro: global indicators are calculated by calculating total true positives, false negatives and false positives. That is to say, all classes are calculated together (to be specific to precision), and then the TP sum of all classes is divided by the sum of TP and FN of all classes. Therefore, precision and recall in micro method are equal to accuracy.

Macro: calculate the indicators of each tag to find their unweighted average. This does not take into account label imbalance. In other words, the precision of each class is calculated first, and then the arithmetic average is calculated.

Weighted: calculate the indicators of each tag, find their average value, and weight by support (the number of real instances of each tag). This would change the “macro” to address the label imbalance; it could result in an F-score that is not between precision and recall.

Samples: calculate the indicators of each instance and find their average value (only meaningful for different multi label classification)_ score)。

reference resources: https://blog.csdn.net/datongmu_ yile/article/details/81750737

Read More: