Return to Snippet

Revision: 70528
at March 29, 2016 17:44 by ManishChandraReddy


Initial Code
(A==B).all()
test if all values of array (A==B) are True.

Edit (from dbaupp's answer and yoavram's comment)

It should be noted that:

this solution can have a strange behavior in a particular case: if either A or B is empty and the other one contains a single element, then it return True. For some reason, the comparison A==B returns an empty array, for which the all operator returns True.
Another risk is if A and B don't have the same shape and aren't broadcastable, then this approach will raise an error.
In conclusion, the solution I proposed is the standard one, I think, but if you have a doubt about A and B shape or simply want to be safe: use one of the specialized functions:

np.array_equal(A,B)  # test if same shape, same elements values
np.array_equiv(A,B)  # test if broadcastable shape, same elements values
np.allclose(A,B,...) # test if same shape, elements have close enough values

Initial URL
http://stackoverflow.com/questions/10580676/comparing-two-numpy-arrays-for-equality-element-wise

Initial Description
Compare two arrays wholly. Useful for setting them in regions.

Initial Title
Array total comparision

Initial Tags


Initial Language
Python