cls*_*udt 203 python arrays numpy elementwise-operations
比较两个numpy数组是否相等的最简单的方法是什么(其中相等定义为:对于所有索引i,A = B iff A[i] == B[i]
)?
简单地使用==
给我一个布尔数组:
>>> numpy.array([1,1,1]) == numpy.array([1,1,1])
array([ True, True, True], dtype=bool)
Run Code Online (Sandbox Code Playgroud)
我是否必须and
使用此数组的元素来确定数组是否相等,还是有更简单的比较方法?
Juh*_*uh_ 308
(A==B).all()
Run Code Online (Sandbox Code Playgroud)
测试数组(A == B)的所有值是否为True.
编辑(来自dbaupp的回答和yoavram的评论)
应当指出的是:
A.shape == B.shape
或A
为空而另一个包含单个元素,则返回B
.由于某种原因,比较True
返回一个空数组,A==B
运算符返回该数组all
.True
和A
不具有相同的形状并且不可播放,那么这种方法将引发错误.最后,我提出的解决方案是标准之一,但是我认为,如果你有一个关于怀疑B
和A
形状或只是想安全:使用专门的功能之一:
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
Run Code Online (Sandbox Code Playgroud)
huo*_*uon 85
该(A==B).all()
解决方案是很整齐,但也有完成这个任务的一些内置的功能.即array_equal
,allclose
和array_equiv
.
(虽然,一些快速测试timeit
似乎表明该(A==B).all()
方法是最快的,这有点奇怪,因为它必须分配一个全新的数组.)
fun*_*unk 15
让我们使用下面的代码测量性能.
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)
Run Code Online (Sandbox Code Playgroud)
产量
Method: (A==B).all(), 0.03031857
Method: np.array_equal(A,B), 0.030025185
Method: np.array_equiv(A,B), 0.030141515
Run Code Online (Sandbox Code Playgroud)
根据上面的结果,numpy方法似乎比==运算符和all()方法的组合更快,并且通过比较numpy方法,最快的方法似乎是numpy.array_equal方法.
use*_*754 13
如果要检查两个阵列是否具有相同的shape
AND elements
,则应使用np.array_equal
文档中建议的方法.
性能方面并不期望任何平等检查会击败另一个,因为没有太多优化空间
comparing two elements
.只是为了这个缘故,我还是做了一些测试.
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
Run Code Online (Sandbox Code Playgroud)
所以非常平等,不需要谈论速度.
其(A==B).all()
行为与以下代码片段相似:
x = [1,2,3]
y = [1,2,3]
print all([x[i]==y[i] for i in range(len(x))])
> True
Run Code Online (Sandbox Code Playgroud)
现在使用np.array_equal
. 来自文档:
np.array_equal([1, 2], [1, 2])
True
np.array_equal(np.array([1, 2]), np.array([1, 2]))
True
np.array_equal([1, 2], [1, 2, 3])
False
np.array_equal([1, 2], [1, 4])
False
Run Code Online (Sandbox Code Playgroud)