Search code examples
pythonconfusion-matrix

How to change the order of confusion matrix values


I used the following code to create confusion matrix:

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(df['diabetes'], df['predictions'])
cm

It shows the output as following:

enter image description here

But I want to get the following output:

enter image description here

What should I do?


Solution

  • Assuming the labels and predictions are 0s and 1s, inverting all of the values like this should give the desired behavior

    from sklearn.metrics import confusion_matrix
    import numpy as np
    
    cm = confusion_matrix(np.abs(df['diabetes']-1), np.abs(df['predictions']-1))
    cm