如何逐个比较numpy数组元素考虑元素的位置?

时间:2022-02-21 01:54:22

I want to compare two numpy array one element by one element taking consider of the position. For example

我想比较两个numpy数组一个元素,一个元素考虑这个位置。例如

[1, 2, 3]==[1, 2, 3]  -> True

[1, 2, 3]==[2, 1, 3]  -> False

I tried the following

我尝试以下

    for index in range(list1.shape[0]):
        if list1[index] != list2[index]:
            return False
    return True

But I got the following error

但是我得到了下面的错误

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

However the following is not the correct usage of .any or .all

然而,以下不是.any或.all的正确用法

 numpy.any(numpy.array([1,2,3]), numpy.array([1,2,3]))


 numpy.all(numpy.array([1,2,3]), numpy.array([1,2,3]))

As it returns

当它返回

 TypeError: only length-1 arrays can be converted to Python scalars

I am very confused, can someone explain what I am doing wrong

我很困惑,有人能解释一下我做错了什么吗

Thanks

谢谢

2 个解决方案

#1


2  

You can pass an array of booleans to all, for example:

您可以将布尔值数组传递给所有人,例如:

>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 1, 3])
>>> a == b
array([False, False,  True], dtype=bool)
>>> np.all(a==b) # also works with all for 1D arrays
False

Note that the built-in all is much faster than np.all for small arrays (and np.array_equal is slower still):

注意,内置的all比np要快得多。所有这些都适用于小数组(和np)。array_equal还是慢):

>>> timeit.timeit("all(a==b)", setup="import numpy as np; a = np.array([1, 2, 3]); b = np.array([2, 1, 3])")
0.8798369040014222
>>> timeit.timeit("np.all(a==b)", setup="import numpy as np; a = np.array([1, 2, 3]); b = np.array([2, 1, 3])")
9.980971871998918
>>> timeit.timeit("np.array_equal(a, b)", setup="import numpy as np; a = np.array([1, 2, 3]); b = np.array([2, 1, 3])")
13.838635700998566

but will not work correctly with multidimensional arrays:

但不能正确地使用多维数组:

>>> a = np.arange(9).reshape(3, 3)
>>> b = a.copy()
>>> b[0, 0] = 42
>>> all(a==b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> np.all(a==b)
False

For larger arrays, np.all is fastest:

对于较大的数组,np。最快都是:

>>> timeit.timeit("np.all(a==b)", setup="import numpy as np; a = np.arange(1000); b = a.copy(); b[999] = 0")
13.581198551000853
>>> timeit.timeit("all(a==b)", setup="import numpy as np; a = np.arange(1000); b = a.copy(); b[999] = 0")
30.610838356002205
>>> timeit.timeit("np.array_equal(a, b)", setup="import numpy as np; a = np.arange(1000); b = a.copy(); b[999] = 0")
17.95089965599982

#2


3  

You can also use array_equal:

您还可以使用array_equal:

In [11]: a = np.array([1, 2, 3])

In [12]: b = np.array([2, 1, 3])

In [13]: np.array_equal(a, a)
Out[13]: True

In [14]: np.array_equal(a, b)
Out[14]: False

This ought to be more efficient since you don't need to keep the temporary a==b...

这应该会更有效,因为您不需要保留临时的a= b…


Note: a little about performance, for larger arrays you want to be using np.all rather than all. array_equal performs about the same unless the arrays differ early, then it is much faster as it can fail early:

注意:关于性能,对于更大的数组,您希望使用np。而不是所有。array_equal的性能几乎相同,除非数组在早期有所不同,否则它会更快,因为它可以在早期失败:

In [21]: a = np.arange(100000)

In [22]: b = np.arange(100000)

In [23]: c = np.arange(1, 100000)

In [24]: %timeit np.array_equal(a, a)  # Note: I expected this to check is first, it doesn't
10000 loops, best of 3: 183 µs per loop

In [25]: %timeit np.array_equal(a, b)
10000 loops, best of 3: 189 µs per loop

In [26]: %timeit np.array_equal(a, c)
100000 loops, best of 3: 5.9 µs per loop

In [27]: %timeit np.all(a == b)
10000 loops, best of 3: 184 µs per loop

In [28]: %timeit np.all(a == c)
10000 loops, best of 3: 40.7 µs per loop

In [29]: %timeit all(a == b)
100 loops, best of 3: 3.69 ms per loop

In [30]: %timeit all(a == c) # ahem!
# TypeError: 'bool' object is not iterable

#1


2  

You can pass an array of booleans to all, for example:

您可以将布尔值数组传递给所有人,例如:

>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 1, 3])
>>> a == b
array([False, False,  True], dtype=bool)
>>> np.all(a==b) # also works with all for 1D arrays
False

Note that the built-in all is much faster than np.all for small arrays (and np.array_equal is slower still):

注意,内置的all比np要快得多。所有这些都适用于小数组(和np)。array_equal还是慢):

>>> timeit.timeit("all(a==b)", setup="import numpy as np; a = np.array([1, 2, 3]); b = np.array([2, 1, 3])")
0.8798369040014222
>>> timeit.timeit("np.all(a==b)", setup="import numpy as np; a = np.array([1, 2, 3]); b = np.array([2, 1, 3])")
9.980971871998918
>>> timeit.timeit("np.array_equal(a, b)", setup="import numpy as np; a = np.array([1, 2, 3]); b = np.array([2, 1, 3])")
13.838635700998566

but will not work correctly with multidimensional arrays:

但不能正确地使用多维数组:

>>> a = np.arange(9).reshape(3, 3)
>>> b = a.copy()
>>> b[0, 0] = 42
>>> all(a==b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> np.all(a==b)
False

For larger arrays, np.all is fastest:

对于较大的数组,np。最快都是:

>>> timeit.timeit("np.all(a==b)", setup="import numpy as np; a = np.arange(1000); b = a.copy(); b[999] = 0")
13.581198551000853
>>> timeit.timeit("all(a==b)", setup="import numpy as np; a = np.arange(1000); b = a.copy(); b[999] = 0")
30.610838356002205
>>> timeit.timeit("np.array_equal(a, b)", setup="import numpy as np; a = np.arange(1000); b = a.copy(); b[999] = 0")
17.95089965599982

#2


3  

You can also use array_equal:

您还可以使用array_equal:

In [11]: a = np.array([1, 2, 3])

In [12]: b = np.array([2, 1, 3])

In [13]: np.array_equal(a, a)
Out[13]: True

In [14]: np.array_equal(a, b)
Out[14]: False

This ought to be more efficient since you don't need to keep the temporary a==b...

这应该会更有效,因为您不需要保留临时的a= b…


Note: a little about performance, for larger arrays you want to be using np.all rather than all. array_equal performs about the same unless the arrays differ early, then it is much faster as it can fail early:

注意:关于性能,对于更大的数组,您希望使用np。而不是所有。array_equal的性能几乎相同,除非数组在早期有所不同,否则它会更快,因为它可以在早期失败:

In [21]: a = np.arange(100000)

In [22]: b = np.arange(100000)

In [23]: c = np.arange(1, 100000)

In [24]: %timeit np.array_equal(a, a)  # Note: I expected this to check is first, it doesn't
10000 loops, best of 3: 183 µs per loop

In [25]: %timeit np.array_equal(a, b)
10000 loops, best of 3: 189 µs per loop

In [26]: %timeit np.array_equal(a, c)
100000 loops, best of 3: 5.9 µs per loop

In [27]: %timeit np.all(a == b)
10000 loops, best of 3: 184 µs per loop

In [28]: %timeit np.all(a == c)
10000 loops, best of 3: 40.7 µs per loop

In [29]: %timeit all(a == b)
100 loops, best of 3: 3.69 ms per loop

In [30]: %timeit all(a == c) # ahem!
# TypeError: 'bool' object is not iterable