We have data frame which contains actual value and prediction value, we have to compute confusion matrix.
Here is the code for it-
def compute_confusion_matrix(true, pred):
K = len(np.unique(true)) # Number of classes
result = np.zeros((K, K))
for i in range(len(true)):
result[true[i]][pred[i]] += 1
return result
actual = np.array(df1['y'])
predicted = np.array(df1['Class'])
result = compute_confusion_matrix(actual,predicted)
print(result)
But i am getting following error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-29-5795bf5f37e5> in <module>
36 predicted = np.array(df1['Class'])
37
---> 38 result = compute_confusion_matrix(actual,predicted)
39
40 print(result)
<ipython-input-29-5795bf5f37e5> in compute_confusion_matrix(true, pred)
29
30 for i in range(len(true)):
---> 31 result[true[i]][pred[i]] += 1
32
33 return result
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
There is a problem with your input arrays, because:
result=compute_confusion_matrix(np.array([0,0,1,0,1,1,0,1,1]),
np.array([0,1,1,0,1,0,0,1,1]))
print(result)
will print:
array([[3., 1.],
[1., 4.]])