Understanding the use of any() and all() in numpy arrays
Asked Answered
A

3

2

What's the difference between the following:

a = np.array([2,3,4])
b = np.array([2,7,8])

if a.any() == b.all():
   print('yes')

and

a = np.array([2,3,4])
b = np.array([2,7,8])

if a.any() == b.any():
   print('yes')

In both situations, 'yes' is printed.

Archdeacon answered 11/6, 2018 at 15:39 Comment(4)
Those are meant more for boolean arrays. What's the intended operation?Olatha
For integers, only 0 counts as False. Since your examples don't have any 0s, both all and any return True.Guillemot
The thing to understand here might be that you are never actually comparing anything from a with anything in b. The two expressions, a.any() and b.all() are evaluated on their own (and as explained in the other comments, both evaluate to True), so what you end up checking is True == True, which is also True.Auricle
Thank you for your response. As a follow up, how would I check if at least one element in a is the same as b, and how would I check if ALL elements in a are the same as b (in the same sequence, therefore the same array). I suppose I could subtract them and then use any() and all()?Archdeacon
B
3

any() and all() are intended for boolean arrays. any() returns True if there's any values that are equal to True in the array. all() returns True if all values in the array are equal to True. For integers/floats the functionality is similar, except that they return True if the value 0 is not found in the array. In your example, since both a.any() and a.all() will return True, it follows that a.any() == a.all().

Try executing the following code to see how it works in practice.

a = np.asarray([1,2,3])
b = np.asarray([-1,0,1])
c = np.asarray([True, False])

print(a.any())
print(a.all())

print(b.any())
print(b.all())

print(c.any())
print(c.all())
Barbarism answered 11/6, 2018 at 16:4 Comment(1)
I did not realize any() and all() only referred to boolean arrays. Thank you for your response!Archdeacon
T
1

On 1D numpy arrays of integers like yours, any will give you True if and only if some element is non-zero, whereas all will give you True if and only if all elements are non-zero.

So your first snippet of code translates into:
"Print yes if the answer to the question 'Is there some non-zero element in a?' is the same as the answer to 'Are all elements of b non-zero'?".

and the second into:
"Print yes if the answer to the question 'Is there some non-zero element in a?' is the same as the answer to 'Is there some non-zero element in b?'".

Taiwan answered 11/6, 2018 at 16:5 Comment(0)
Z
1

I think the original post was due to a misunderstanding in combining logical operation (such as ==) and methods np.any() and np.all() when comparing 2 tables. Following the answer from @user2653663, I thought it is worth to complete it by the following example:

    import numpy as np
    a = np.asarray([1,2,3])
    b = np.asarray([1,0,1])
    print((a == b).any())
    print((a == b).all())
    True
    False

The first print will return True as the compare operation "cell by cell" found at least 1 equal cell in both tables; which is in our case a[0] == b[0]. As the second returned False because not all cells are equals. You can easily visualize why by doing:

print(a == b)
[ True False False]
Zebrawood answered 9/11, 2023 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.