inneb
inneb

Reputation: 1110

Saving confusion matrix

Is there any possibility to save the confusion matrix which is generated by sklearn.metrics?

I would like to save multiple results of different classification algorithms in an array or maybe a pandas data frame so I can show which algorithm works best.

print('Neural net: \n',confusion_matrix(Y_test, Y_pred), sep=' ')

How could I save the generated confusion matrix within a loop? (I am training over a set of 200 different target variables)

array[i] = confusion_matrix(Y_test,Y_pred) 

I run into some definition problems here [array is not defined whereas in the non [i] - version it runs smoothly]

Additionally, I am normalizing the confusion matrix. How could I print out the average result of the confusion matrix after the whole loop? (average of the 200 different confusion matrices)

I am not that fluent with python yet.

Upvotes: 2

Views: 3583

Answers (2)

ashukid
ashukid

Reputation: 353

First getting to array not defined problem. In python list is declared as :

array=[]

Since size of list is not given during declaration, no space is allocated. Hence we can't assign values the place which is not allocated.

array[i]=some value, but no space is allocated for array

So if you know the required size of array,fill zeroes during declaration and the use array this way or use array.append() method inside the loop.

Now for saving confusion matrix: Since confusion matrix returns 2-D array and you need to save multiple such arrays, use 3-D array for saving the value.

import numpy as np
matrix_result=np.zeroes((200,len(y_pred),len(y_pred)))
for i in range(200):
    matrix_result[i]=confusion_matrix(X_pred,y_pred)

For averaging

   matrix_result_average=matrix_result.mean(axis=0)

Upvotes: 1

user2124834
user2124834

Reputation:

I'm not sure what you mean by training over a set of target variables (please elaborate), but here is a start at averaging over confusion matrices, using numpy.

First an empty result matrix is created, which is three-dimensional and the size of 200 stacked confusion matrices. These are then filled in one-by-one in the for-loop. Finally the resulting matrix is averaged along the dimension of the targets, resulting in the average confusion matrix.

import numpy as np

N = len(Y_pred)
result = np.zeros((len(targets), N, N))
for i, target in enumerate(targets):
    result[i] = confusion_matrix(Y_test, Y_pred) # do someting with target?
print(result.mean(axis=0))

Upvotes: 0

Related Questions