Comparing values in a numpy array – all() and any()

import numpy as np

# create a 2 dimentional array and put some values in
myData = np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]])

print (myData.shape)
print (myData[0,1])
print (myData[1,1])

if (myData[0,1]>myData[1,1]):
    print ("yes")
else:
    print ("no")
(2, 3) 
2.2
5.5
no

The above is self-explanatory, we are comparing two specific elements in the array. But if we don’t specify specific elements to compare, we receive an error. Take the following code:

import numpy as np

myData = np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]])

print (myData.shape)
print (myData[0,:])
print (myData[1,:])

if (myData[0,:]>myData[1,:]):
    print ("yes")
else:
    print ("no")
(2, 3) 
[1.1 2.2 3.3]
[4.4 5.5 6.6]

We see a value error when we try to do the above, as we are not evaluation 1 element against another element. We are trying to evaluate a range against another range.

a.any() and a.all()

What is a.any() and a.all().

According to the documenation:

a.any():
Test whether any array element along a given axis evaluates to True.

a.all():
Test whether all array elements along a given axis evaluate to True.

Let’s try using this.

import numpy as np

myData = np.array([[1.1, 9.2, 2.3], [4.4, 5.5, 6.6]])

print (myData.shape)
print (myData[0,:])
print (myData[1,:])

if (myData[0,:]>myData[1,:]).any():
    print ("yes")
else:
    print ("no")
(2, 3) 
[1.1 9.2 2.3]
[4.4 5.5 6.6]
yes

By using any() above, we are saying if any of the elements in the first row are greater than any of matching elements in the second row, return “yes”.

To be clear, what it’s asking is effectively 3 questions.

  1. Is 1.1 greater than 4.4?
  2. Is 9.2 greater than 5.5?
  3. Is 2.3 greater than 6.6?

As you can see, question 2 would return true whilst 1 & 3 would return false. Because we are using any(), the if statement returns true.

As you can see, 9.2 is greater than the corresponding element that it’s being tested against, so it returns true (prints “yes”).

It now becomes more obvious what all() will do.

Using the same array the following code returns “No”.

import numpy as np

myData = np.array([[1.1, 9.2, 2.3], [4.4, 5.5, 6.6]])

print (myData.shape)
print (myData[0,:])
print (myData[1,:])

if (myData[0,:]>myData[1,:]).all():
    print ("yes")
else:
    print ("no")
(2, 3) 
[1.1 9.2 2.3]
[4.4 5.5 6.6]
no

Using all(), the 3 questions we asked above all need to be true, as below.

import numpy as np

myData = np.array([[14.1, 9.2, 6.600001], [4.4, 5.5, 6.6]])

print (myData.shape)
print (myData[0,:])
print (myData[1,:])

if (myData[0,:]>myData[1,:]).all():
    print ("yes")
else:
    print ("no")
(2, 3) 
[14.1 9.2 6.600001]
[4.4 5.5 6.6]
yes

You can see from the above that every element of the first data is greater than every corresponding element in the second data.

Further example using .all()

jamesData = np.array([[0.3, 0.9, 0.42], [4.4, 5.5, 6.6]])

if (jamesData [0,0]>jamesData [0,1:2]).all():
    print ("0.3 is greater than 0.9 and 0.42")
else:
    print ("0.3 is NOT greater than 0.9 and 0.42")
 0.3 is NOT greater than 0.9 and 0.42 

Leave a Reply