Search code examples
pythonscikit-learnconfusion-matrixmultilabel-classification

Is this confusion matrix correct?


I'm trying to classify animal sounds. I'm using CNN for this purpose. I'm comparing the classes with each other, hence, some training will be done with 2 classes. Once I get the confusion matrix with these training, I encounter something like this:confusion matrix

As can be seen, it seems the model predicts everything correctly. On the other hand, when I raise the threshold from 0.5 to 0.8, I see that there are some misclassification. This is actually what I expected, but I confused that when I get the accuracy, precision, recall and f1 score, I see a value with lower than 1, and all are the same, as in the picture. However, I expect all of them as 1 because there is no misclassification. Am I wrong? Can you please explain? Thx in advance.

Note that my data has 2 class and each of which has 1200 records. I trained them with a CNN model. 20% of the data is used as test, rest of them are training.

Code is below:


from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import confusion_matrix as cm
predictions = model.predict(X_test)
y_pred = (predictions > 0.5)
print(predictions.shape, y_pred.shape)
predictions = np.argmax(predictions, axis=1)
y_pred = np.argmax(y_pred, axis=-1)
cm(y_pred, y_test)



Solution

  • The problem is that you computed confusion matrix between prediction and itself, instead of prediction and y_test.

    It should be

    cm(y_pred, y_test)