可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
What is the simplest way to compare two numpy arrays for equality (where equality is defined as: A = B iff for all indices 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?
回答1:
(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
回答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.)
回答3:
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.
回答4:
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([True if x[i]==y[i] else False for i in range(len(x))]) > True