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())
Answer from user2653663 on Stack Overflowany() 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())
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?'".
If you take a look at the result of valeur <= 0.6, you can see what’s causing this ambiguity:
>>> valeur <= 0.6
array([ True, False, False, False], dtype=bool)
So the result is another array that has in this case 4 boolean values. Now what should the result be? Should the condition be true when one value is true? Should the condition be true only when all values are true?
That’s exactly what numpy.any and numpy.all do. The former requires at least one true value, the latter requires that all values are true:
>>> np.any(valeur <= 0.6)
True
>>> np.all(valeur <= 0.6)
False
There is one more way you can get this
import numpy as np
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
c = np.array([1,2,3,4])
print((a == b ).all()) #False
print((a == c ).all()) # True
print((a == b ).any()) #False
print((a == c ).any()) #True
print((a > 3 ).all()) #False
Say I have two np arrays of the same size in, a and b. Sometimes writing something like print(a == b) gives an array like [True True False True ...] going through and pairing each element, but sometimes I get an error saying use a.any() or a.all(). I can't figure out the rationale behind when I get each result.