0

In order to compute confusion matrix (not the accuracy) loop over the predicted and true labels may be needed. How to perform that in a numpy manner, if next code does not give needed result?

>> a = np.zeros((5, 5))
>> indices = np.array([
      [0, 0], 
      [2, 2],
      [4, 4],
      [0, 0],
      [2, 2],
      [4, 4],
   ])
np.add.at(a, indices, 1)
>> a
>> array([
   [4., 4., 4., 4., 4.],
   [0., 0., 0., 0., 0.],
   [4., 4., 4., 4., 4.],
   [0., 0., 0., 0., 0.],
   [4., 4., 4., 4., 4.]
])

# Wanted 
>> array([
   [2., 0., 0., 0., 0.],
   [0., 0., 0., 0., 0.],
   [0., 0., 2., 0., 0.],
   [0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 2.]
])
A.Ametov
  • 1,790
  • 1
  • 13
  • 20

1 Answers1

1

Docs say If first operand has multiple dimensions, indices can be a tuple of array like index objects or slice objects.

Using next tupling wanted result is reached.

np.add.at(a, (indices[:, 0], indices[:, 1]), 1)
A.Ametov
  • 1,790
  • 1
  • 13
  • 20