tf recall precision not working for class_id 0

51 views Asked by At

So, I am quite puzzled. I have the following arrays:

import tensorflow as tf
import numpy as np
y_true = np.array(
    [1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
     1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1,
     1, 1, 1, 1, 1])
y_pred = np.array(
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     1, 1, 1, 1, 1])

You can see how the prediction got NO class label. Therefore, it should have a Recall of 0 (for class 0). So I compute it like this:

m = tf.keras.metrics.Recall(class_id=0)
m.update_state(y_true=y_true, y_pred=y_pred)
m.result().numpy()

And it returns 1.0! As if it is still giving me the Recall for class 1. So my question is, how can I get the right value? This question is also valid for Precision. However, precision also gives 1 which is no the case even for class 1.

For reference, the expected results are:

from sklearn.metrics import precision_score, recall_score

recall_score(y_true=y_true, y_pred=y_pred, pos_label=0)
precision_score(y_true=y_true, y_pred=y_pred, pos_label=0)
0

There are 0 answers