Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

classification_report: labels and target_names

I have following output from a classification report:

             precision    recall  f1-score   support

          0     0.6772    0.5214    0.5892       491
          1     0.8688    0.9273    0.8971      1678

avg / total     0.8254    0.8354    0.8274      2169

The true labels in the dataset are s and p.

Question: How do I know which label is "0" and which is "1"? Or: How can I assign the labels via labels= or target_names= in the correct order?

like image 298
Christopher Avatar asked Jan 29 '18 01:01

Christopher


3 Answers

It will be arranged in alphabetic order if not specified otherwise. So most probably it will be:

0 -> 'p'

1 -> 's'

Anyways, if you pass the actual labels, they should be displayed as it is. For example:

y_true = ['p', 's', 'p', 's', 'p']
y_pred = ['p', 'p', 's', 's', 'p']

print(classification_report(y_true, y_pred))

Output:
             precision    recall  f1-score   support

          p       0.67      0.67      0.67         3
          s       0.50      0.50      0.50         2

avg / total       0.60      0.60      0.60         5

So no need to do anything. But if you have changed the labels then you can pass them in the target_names param to display in the report.

Let's say you have converted 'p' to 0 and 's' to 1, then your code becomes:

y_true = [0, 1, 0, 1, 0]
y_pred = [0, 0, 1, 1, 0]

# Without the target_names
print(classification_report(y_true, y_pred))

          0       0.67      0.67      0.67         3
          1       0.50      0.50      0.50         2

avg / total       0.60      0.60      0.60         5

#With target_names
print(classification_report(y_true, y_pred, target_names=['p', 's']))

          p       0.67      0.67      0.67         3
          s       0.50      0.50      0.50         2

avg / total       0.60      0.60      0.60         5
like image 109
Vivek Kumar Avatar answered Nov 14 '22 05:11

Vivek Kumar


If you are using a sklearn.preprocess.LabelEncoder to encode raw labels, you can use inverse_transform to get the original labels

target_strings = label_encoder.inverse_transform(np.arange(num_classes))
metrics.classification_report(dev_gold, dev_predicted, target_names=target_strings)
like image 6
Chaitanya Shivade Avatar answered Nov 14 '22 05:11

Chaitanya Shivade


You can use classes_ attribute of a classifier to get a list of labels and they are array indexed.

classes_ : array of shape = [n_classes] or a list of such arrays

The classes labels (single output problem), or a list of arrays of class labels (multi-output problem).

like image 2
CtheSky Avatar answered Nov 14 '22 05:11

CtheSky