I need to define a function that generates a confusion matrix. So I have two vectors, y_label
and y_predict
, the element values of which are either 0, 1, 2. The goal of the function is to create a count of labels:
| 0 | 1 | 2 |
--------------
0 | | | |
--------------
1 | | | |
--------------
2 | | | |
--------------
For example, cm[0,1]
should contain counts of elements where y_label[i] = 0 and y_predict[i] = 1, for every i.
So far, this is what I've done:
def get_confusion_matrix(y_label, y_fit):
cm = np.ndarray([3,3])
for i in range(3):
for j in range(3):
cm[i, j] = ....
return cm
Of course, I can easily do multiple-level for
loops to count, but I want to avoid that if there are short cuts in Python / numpy.
I'm thinking also of making y_label
and y_predict
merged to become an array of tuples, then using dict-zip technique, similar to here:
How to count the occurrence of certain item in an ndarray in Python?
But the solution is still a bit hazy on my head. Please confirm if this is also possible.