6

Lets say I have two numpy arrays A and B:

A = [[1,2,3],
     [4,5,6]]
B = [[3,2,3],
     [6,5,6]]

I would like to (quickly) count the number of elements that are unequal between the two arrays. In the case above, the answer would be 2.

Is there nice way to do this?

James Atwood
  • 4,289
  • 2
  • 17
  • 17

2 Answers2

4

From the related question you can also reverse the logic and do:

np.count_nonzero(A != B)

Which actually seems to be quite a bit more efficient.

Community
  • 1
  • 1
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
3
a = np.array(A)
b = np.array(B)
print((a != b).sum())
Alex
  • 18,484
  • 8
  • 60
  • 80