I'm trying to create a confusion matrix in NumPy, so I initialize a zero-filled matrix with size n x n being n the number of classes in my classifier.
After that I iterate over every output (which is a word, or number, or whatever, but is contained in self.clases__), get the indices in the set of clases that correspond to the real label and the output, and try to increase the value at that position by 1.
This results in an IndexError exception, here's the output for a simple example:
In[99]: e.score([[0.1,0.5,0.7,0.8],[0.4,0.5,0.1,0.1,0.9],[0.1,0.9],
[0.1,0.5,0.8,0.2,0.9]],['Clase 1','Clase 2','Clase 2','Clase 1'])
Clase 1 -> 0
Clase 1 -> 0
(2, 2)
Clase 1 -> 0
Clase 2 -> 1
(2, 2)
Traceback (most recent call last):
File "<ipython-input-99-2b9291b14506>", line 2, in <module>
e.score([[0.1,0.5,0.7,0.8],[0.4,0.5,0.1,0.1,0.9],[0.1,0.9],
[0.1,0.5,0.8,0.2,0.9]],['Clase 1','Clase 2','Clase 2','Clase 1'])
File "redes.py", line 156, in score
conf_matrix[i][j] += 1
IndexError: index 1 is out of bounds for axis 0 with size 1
Here's the code:
def score(self,X,Y):
salidas_obtenidas = self.clasifica(X)
conf_matrix = zeros((len(self.clases__),len(self.clases__)),dtype=int)
indexador = array(range(0,len(self.clases__)))
for k,obt in enumerate(salidas_obtenidas):
i = indexador[self.clases__ == obt]
j = indexador[self.clases__ == Y[k]]
print("%s -> %d" % (obt,i))
print("%s -> %d" % (Y[k],j))
print(conf_matrix.shape)
conf_matrix[i][j] += 1
return conf_matrix
The thing that has me stumped is that the shape printed is obviously big enough to be indexed, and yet it raises the exception. I use the numpy classes array and zeros.
Also, to my even greater confusion, conf_matrix doesn't update, if I print it at any point in the execution, it just prints a zero-filled matrix, even though before it raises the exception, it should have at least a position with 1 in it.
That makes me suspect it's not actually accessing the real matrix, but I have no idea why it wouldn't.
I'm using numpy version 1.14.2, and Python 3.6.
Any help is appreciated!
EDIT
I added some code so that hopefully someone can reproduce the error without needing the rest of the code:
import numpy as np
salidas_obtenidas = np.array(['Clase 1', 'Clase 2', 'Clase 1', 'Clase 2'])
salidas_obtenidas = salidas_obtenidas.reshape(len(salidas_obtenidas),1)
clases = np.array(['Clase 1','Clase 2'],dtype='<U7')
Y = ['Clase 1','Clase 2','Clase 2','Clase 1']
conf_matrix = np.zeros((len(clases),len(clases)),dtype=int)
indexador = np.array(range(0,len(clases)))
for k,obt in enumerate(salidas_obtenidas):
i = indexador[clases == obt]
j = indexador[clases == Y[k]]
print("%s -> %d" % (obt,i))
print("%s -> %d" % (Y[k],j))
print(conf_matrix.shape)
conf_matrix[i][j] += 1
print(conf_matrix)