2

I have been looking online and couldn't find an answer to what I am looking for. I have a 3 class confusion matrix as well as its cost matrix. I know how to do it for two classes but for three I am unsure of how to apply this formula.

enter image description here

Confusion matrix:                 Cost matrix:
            23  4  0                     -1 5 10
             6 13  3                      5 0 10
             9  2 20                    100 5  0

1 Answers1

4

To obtain the cost you simply have to multiply each term in your confusion matrix by its cost and then sum the terms.

if your confusion matrix is:

$$ \begin{matrix} 23 & 4 & 0\\ 6 & 13 & 3\\ 9 & 2 & 20\\ \end{matrix} $$

and cost matrix is $$\begin{matrix} -1 & 5 & 10\\ 5 & 0 & 10\\ 100 & 5 & 0\\ \end{matrix} $$

Then the total cost is: $-1*23 + 4*5 +0*10 + 6*5 + 13*0 + 3*10 + 9*100 + 2*5 + 20*0 = 967$

With python-numpy you can calculate this easily:

import numpy as np
confusion_matrix = np.array([[23, 4, 0], [6, 13, 3], [9, 2, 20]])
cost_matrix = np.array([[-1, 5, 10], [5, 0, 10], [100, 5, 0]])
# Apply element-wise multiplication:
total_cost = np.multiply(confusion_matrix, cost_matrix).sum()
Udi
  • 99