0
$\begingroup$

I have two arrays: the first one with all the correct labels (they are all set to zero since each sample belong to the same class) and another one with all the labels predicted by my neural network. What I want do to is plot a simple confusion matrix to show the results. This is my code:

test_labels = [0, 0, 0, ...., 0] #all set to zero
predict_labels = [0, 0, 1, ...., 1]
matrix = confusion_matrix(test_labels, predict_labels)
fig = plt.figure()
colors = ['orange', 'green']
labels = ["Normal", "Anomaly"]
sns.heatmap(matrix, xticklabels=labels, yticklabels=labels, cmap=colors, annot=True, fmt="d")
plt.title("Confusion matrix")
plt.ylabel("Actual")
plt.xlabel("Predicted")
plt.close()

The problem is that when I run this code I get the following warnings:

UndefinedMetricWarning: Recall and F-score are ill-defined and being set to 0.0 in labels with no true samples. Use zero_division parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result))

RuntimeWarning: invalid value encountered in true_divide recall = tps / tps[-1]

UndefinedMetricWarning: No positive samples in y_true, true positive value should be meaningless UndefinedMetricWarning)

How can I solve it?

$\endgroup$
2
$\begingroup$

If you insist on giving no positive examples it will have the same problem. As the error message states, the tool wants to compute the recall. The recall metric is given by:

$$recall = \frac{tp}{tp+fn}$$

$tp$ (true positive) is equal with the number of positive cases predicted as positive, which is $0$ in your case.

$fn$ (false negatives) is the number of positive cases predicted as negative, which again is $0$.

What you ask is to compute $\frac{0}{0}$ which is undefined. You can add positive cases also or you should study the tool (I'm not too familiar with Python stack)and perhaps you find an option to not compute recall metrics for you.

$\endgroup$
0
$\begingroup$

If you want the warning messages not to be shown just do:

import warnings
warnings.filterwarnings("ignore")
$\endgroup$
1
  • $\begingroup$ The warning itself it's not a problem, I just wanna understand if I did a mistake writing the code $\endgroup$ – Fabio Apr 5 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.