'Two-dimensional DataArray Confusion Matrix does not work in Python (with Scikit), why?

I have two xarray.DataArray's with only zeroes (0) and ones (1) in it. It is a raster with one truth tested data and the second one with predicted data. I need to make a Confusion Matrix. I'm using the Scikit confusion matrix codes but unfortunately it does not work. It seems like the confusion matrix does not detect the '1' values in the DataArrays and returns a zero in precision, recall and f1-score for the value 1.

I manually checked if there are any 1's in both DataArrays, there must be more than 200. So that is not the problem.

print(y_test)

array([[0, 0, 0, ..., 0, 0, 0],
   [0, 0, 0, ..., 0, 0, 0],
   [0, 0, 0, ..., 0, 0, 0],
   ...,
   [0, 0, 0, ..., 0, 0, 0],
   [0, 0, 0, ..., 0, 0, 0],
   [0, 0, 0, ..., 0, 0, 0]])

yes, we now only see zeroes, but in the full DataArray there are clearly zones of ones as well.

and

print(y_pred)

array([[0, 0, 0, ..., 0, 0, 0],
   [0, 0, 0, ..., 0, 0, 0],
   [0, 0, 0, ..., 0, 0, 0],
   ...,
   [0, 0, 0, ..., 1, 0, 0],
   [1, 0, 0, ..., 1, 1, 0],
   [1, 0, 0, ..., 1, 1, 0]])

then I'm starting my confusion matrix process

from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.metrics import multilabel_confusion_matrix

# confusion matrix
matrix = confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))
print('Confusion matrix : \n',matrix)

# outcome values order in sklearn
tp, fn, fp, tn = confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1),labels=[0,1]).reshape(-1)
print('Outcome values : \n', tp, fn, fp, tn)

# classification report for precision, recall f1-score and accuracy
matrix = classification_report(y_test.argmax(axis=1), y_pred.argmax(axis=1),labels=[0,1], 
target_names=target_names)
print('Classification report : \n',matrix)

outcome:

Confusion matrix : 
[[986 200  84 ...   0   0   0]
[  0   0   0 ...   0   0   0]
[  0   0   0 ...   0   0   0]
  ...
[  0   0   0 ...   0   0   0]
[  0   0   0 ...   0   0   0]
[  6   0   0 ...   0   0   0]]
Outcome values : 
  986 200 0 0
Classification report : 
           precision    recall  f1-score   support

           0       0.90      0.49      0.63      2016
           1       0.00      0.00      0.00         0

   micro avg       0.75      0.49      0.59      2016
   macro avg       0.45      0.24      0.32      2016
weighted avg       0.90      0.49      0.63      2016

Is the problem that I'm using a two dimensional DataArray? Can I solve this problem? Or is it something else? Thanks a lot.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source