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?
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
                        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)
                        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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With