How Gauss would compute a Confusion matrix for their classification model
A confusion matrix
is a practical and conceptually simple tool to evaluate a classification model.
So we need to honour it with a simple way to compute it, like Gauss in the past,
without the magic of would do
it with simple linear algebra operations:from sklearn.metrics import confusion_matrix
A confusion matrix is the matrix multiplication by the true and predicted labels, both encoding as one-hot vectors.
If we have the true labels of 4 observations in vector , and 3 different classes (i.e. 0, 1 and 2), their one-hot encoding will be:
Some classification model gives us the predicted label for each observation in the vector , by the same logic above, the one-hot encoding will be:
We have everything to compute the confusion matrix and, it will be . So again,A confusion matrix is the matrix multiplication by the true and predicted labels, both encoding as one-hot vectors.
As you notice, the confusion matrix summarizes the information correctly of both vectors.
- The sum of the diagonal elements tells us that two observations were correctly classified by the model
- The model correctly classified one observation for class
0 - The model didn't assign any label to class
1, producing two errors - The model confuses two class'
1-observations, one with a2and the other with a0, look at the second row
Now with import numpy as np
We need two steps to compute our confusion matrix.
First, we need a way to transform a vector with k-classes into their one-hot-encoding
version, v_one_hot = one_hot_econding(v):
'''Return the one-hot encoding vector for k-classes label vector'''
= .
return
Second, compute the confusion matrix, , for k-classes; there are many ways of doing it with numpy as
you can see in the following code. Below I used the canonical notation to name the
true labels (y) and the predicted ones (y_pred):
# 1st option: Using the matrix multiplication '@' operator
. @
# 2nd option: Using np.dot()
# 3rd option: Using np.matmul()
And we are done! Of course, you can always get your confusion matrix from your favourite store ;)
That's the way computer talks to each other.