If you look at this:

>>> y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
>>> y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
>>> confusion_matrix(y_true, y_pred, labels=["ant", "bird", "cat"])
array([[2, 0, 0],
       [0, 0, 1],
       [1, 0, 2]])

I suppose fist row of array means "predicted ant" and first column is "actually is ant" second column is "actually is bird" etc.

So first row first col 2 i read like "predicted ant, is ant", first row second col 0 i read as "precited ant is bird" is 0 which fits, and third column is "predicted ant is cat" is 0 but should be 1.

What i am doing wrong while understanding the confusion matrix.

Another example is this

>>> from sklearn.metrics import confusion_matrix
>>> y_true = [2, 0, 2, 2, 0, 1]
>>> y_pred = [0, 0, 2, 2, 0, 2]
>>> confusion_matrix(y_true, y_pred)
array([[2, 0, 0],
       [0, 0, 1],
       [1, 0, 2]])

Where is not even clear, what is the order of classes.

Source: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html

edit: Unless it is swapped. First row is "is ant" not "predicted ant". Only that on wikipedia the system is that row is the prediction.

  • $\begingroup$ Did you see this line in the docs you linked to? "Returns: ... Confusion matrix whose i-th row and j-th column entry indicates the number of samples with true label being i-th class and prediced label being j-th class". And also "Wikipedia and other references may use a different convention for axes". $\endgroup$
    – NotThatGuy
    Nov 2, 2020 at 1:57

1 Answer 1


You just confused the actual and predicted. Every row represents actual values of distinct elements in your array and columns represent predicted values of them. That is,

enter image description here

  • First row: There are 2 ants, and 2 samples are predicted as ant.
  • Seconds row: There are 1 bird and 1 sample is predicted as cat.
  • Third row: There are 3 cats, 1 sample is predicted as ant, 2 samples are predicted as cat.
  • $\begingroup$ yes, only that on wikpiadie the rows are the predictions en.wikipedia.org/wiki/Confusion_matrix $\endgroup$
    – luky
    Nov 1, 2020 at 17:31
  • $\begingroup$ Yes, axes of actual and predicted are always used interchangeably in the confusion matrix (My observations say it depends on the field). So it is always better to read the documentation first. $\endgroup$ Nov 1, 2020 at 17:41

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.