# How can i implement an confucion matrix?

im trying to do a research but i need to make a confusion matrix how can i do that on this model?

https://www.kaggle.com/code/stpeteishii/race-classify-densenet201

Sorry im so so new to everything.

New contributor
Ayberk İlbak is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• Can you please explain what the link has to do with your question? It would also be helpful to know whether you try to understand the concept of confusion matrices or whether you are looking for a concrete implementation. 2 days ago
• im looking for concrete implementation. The link is for i want to add that model to a confusion matrix but i couldnt manage to figure it out sadly 2 days ago

Without having read too much your code, a confusion matrix states how many elements from class $$y_1, y_2, y_3..$$ have been associated by the model to class $$y_1, y_2, y_3..$$

So, for n classes, the matrix size is nxn.

Each row of the matrix should represent $$y_i$$, while each column should represent $$y'_i$$, so each row should represent the actual class, while each column the predicted class.

This image took from Wikipedia represents a confusion matrix. I don't know why, but some cells are left empty. Maybe empty means 0.

As you see, the main diagonal numbers represent the number of true predictions for each class $$y_i$$.

To implement a confusion matrix with a library, here's the following code that may help you:

from sklearn.metrics import confusion_matrix

y_actu = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2]
confusion_matrix(y_actu, y_pred)


Output in this case is a 3x3 array:

array([[3, 0, 0],
[0, 1, 2],
[2, 1, 3]])


source

New contributor
Iya Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• Thank you i will check it but is there anything that i could peek up like directories to implement an confusion matrix Mar 14 at 21:13
• Answer updated, I think that's acceptable now Mar 14 at 21:16

Confusion matrices are supported by scikit-learn (see also lya Lees answer). In cell 1, you can import it via

from sklearn.metrics import classification_report, log_loss, accuracy_score, confusion_matrix


Then in Cell [19] (or in a later cell), you can call it similar to classification_report:

print(confusion_matrix(ground,pred))