Comparing two NumPy arrays for equality, element-wise

541,314

Solution 1

(A==B).all()

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

Note: maybe you also want to test A and B shape, such as A.shape == B.shape

Special cases and alternatives (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, 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

Solution 2

The (A==B).all() solution is very neat, but there are some built-in functions for this task. Namely array_equal, allclose and array_equiv.

(Although, some quick testing with timeit seems to indicate that the (A==B).all() method is the fastest, which is a little peculiar, given it has to allocate a whole new array.)

Solution 3

If you want to check if two arrays have the same shape AND elements you should use np.array_equal as it is the method recommended in the documentation.

Performance-wise don't expect that any equality check will beat another, as there is not much room to optimize comparing two elements. Just for the sake, i still did some tests.

import numpy as np
import timeit

A = np.zeros((300, 300, 3))
B = np.zeros((300, 300, 3))
C = np.ones((300, 300, 3))

timeit.timeit(stmt='(A==B).all()', setup='from __main__ import A, B', number=10**5)
timeit.timeit(stmt='np.array_equal(A, B)', setup='from __main__ import A, B, np', number=10**5)
timeit.timeit(stmt='np.array_equiv(A, B)', setup='from __main__ import A, B, np', number=10**5)
> 51.5094
> 52.555
> 52.761

So pretty much equal, no need to talk about the speed.

The (A==B).all() behaves pretty much as the following code snippet:

x = [1,2,3]
y = [1,2,3]
print all([x[i]==y[i] for i in range(len(x))])
> True

Solution 4

Let's measure the performance by using the following piece of code.

import numpy as np
import time

exec_time0 = []
exec_time1 = []
exec_time2 = []

sizeOfArray = 5000
numOfIterations = 200

for i in xrange(numOfIterations):

    A = np.random.randint(0,255,(sizeOfArray,sizeOfArray))
    B = np.random.randint(0,255,(sizeOfArray,sizeOfArray))

    a = time.clock() 
    res = (A==B).all()
    b = time.clock()
    exec_time0.append( b - a )

    a = time.clock() 
    res = np.array_equal(A,B)
    b = time.clock()
    exec_time1.append( b - a )

    a = time.clock() 
    res = np.array_equiv(A,B)
    b = time.clock()
    exec_time2.append( b - a )

print 'Method: (A==B).all(),       ', np.mean(exec_time0)
print 'Method: np.array_equal(A,B),', np.mean(exec_time1)
print 'Method: np.array_equiv(A,B),', np.mean(exec_time2)

Output

Method: (A==B).all(),        0.03031857
Method: np.array_equal(A,B), 0.030025185
Method: np.array_equiv(A,B), 0.030141515

According to the results above, the numpy methods seem to be faster than the combination of the == operator and the all() method and by comparing the numpy methods the fastest one seems to be the numpy.array_equal method.

Solution 5

Usually two arrays will have some small numeric errors,

You can use numpy.allclose(A,B), instead of (A==B).all(). This returns a bool True/False

Share:
541,314
clstaudt
Author by

clstaudt

Updated on April 20, 2021

Comments

  • clstaudt
    clstaudt about 3 years

    What is the simplest way to compare two NumPy arrays for equality (where equality is defined as: A = B iff for all indices i: A[i] == B[i])?

    Simply using == gives me a boolean array:

     >>> numpy.array([1,1,1]) == numpy.array([1,1,1])
    
    array([ True,  True,  True], dtype=bool)
    

    Do I have to and the elements of this array to determine if the arrays are equal, or is there a simpler way to compare?

  • yoavram
    yoavram over 11 years
    you're right, except that if one of the compared arrays is empty you'll get the wrong answer with (A==B).all(). For example, try: (np.array([1])==np.array([])).all(), it gives True, while np.array_equal(np.array([1]), np.array([])) gives False
  • Aidan Kane
    Aidan Kane over 9 years
    I just discovered this performance difference too. It's strange because if you have 2 arrays that are completely different (a==b).all() is still faster than np.array_equal(a, b) (which could have just checked a single element and exited).
  • Bernhard
    Bernhard almost 8 years
    np.array_equal also works with lists of arrays and dicts of arrays. This might be a reason for a slower performance.
  • Wilfred Hughes
    Wilfred Hughes almost 8 years
    You almost always want np.array_equal IME. (A==B).all() will crash if A and B have different lengths. As of numpy 1.10, == raises a deprecation warning in this case.
  • Vikhyat Agarwal
    Vikhyat Agarwal over 6 years
    You should use a larger array size that takes at least a second to compile to increase the experiment accuracy.
  • Juh_
    Juh_ almost 6 years
    You've got a good point, but in the case I have a doubt on the shape I usually prefer to directly test it, before the value. Then the error is clearly on the shapes which have a completely different meaning than having different values. But that probably depends on each use-case
  • Vincenzooo
    Vincenzooo over 5 years
    another risk is if the arrays contains nan. In that case you will get False because nan != nan
  • Juh_
    Juh_ over 5 years
    Good to point it out. However, I think this is logical because nan!=nan implies that array(nan)!=array(nan).
  • loved.by.Jesus
    loved.by.Jesus over 5 years
    Thanks a lot for the function allclose, that is what I needed for numerical calculations. It compares the equality of vectors within a tolerance. :)
  • Admin
    Admin about 5 years
    I do not understand this behavior: import numpy as np H = 1/np.sqrt(2)*np.array([[1, 1], [1, -1]]) #hadamard matrix np.array_equal(H.dot(H.T.conj()), np.eye(len(H))) # checking if H is an unitary matrix or not H is an unitary matrix, so H x H.T.conj is an identity matrix. But np.array_equal returns False
  • Juh_
    Juh_ over 4 years
    This should be put in another question. But the answer is simple, the values of the H * H.T.conj is close but not equals to an id matrix due to numeric precision: use np.allclose
  • Or Groman
    Or Groman over 4 years
    Does this also reproduce when order of comparison is changed? or reiniting A and B to random each time? This difference might also be explained from memory caching of A and B cells.
  • To마SE
    To마SE over 4 years
    There's no meaningful difference between these timings.
  • EliadL
    EliadL almost 4 years
    Note that np.array_equiv([1,1,1], 1) is True. This is because: Shape consistent means they are either the same shape, or one input array can be broadcasted to create the same shape as the other one.
  • Milan
    Milan about 3 years
  • julliet
    julliet over 2 years
    Could you please explain the situation when I need to compare values of one array array1 to the values of array2 and array3, so that: each value of array1 have to be tested if she falls inbetween value of array2 and array3. At the same time I have to test each array1 value through all pairs of array2 and array3, but this pairs must be on the same position