Python: "Use a.any() or a.all()" while traversing a numpy.ndarray

15,380

Solution 1

Normally, you can compare two numbers to get a truth value. For example:

elem = 5
if elem < 6:
    # do something

is equivalent to:

if true:
    # do something

However, you can't compare an array to a value. For example:

elem = [5,7]
if elem < 6:
    # this doesn't make sense

Instead, you can get the truth value of whether any or all elements satisfy the condition. For example:

elem = np.array([5,7])
if np.any(elem<6):
    # this is true, because 5 < 6
if np.all(elem<6):
    # this isn't true, because 7 > 6

I ran your example code above and found no error, so I'm not sure what the issue is. But this is what you should look out for. Consider printing the element you are comparing to see if it is an array.


Also, this is a shorter way of doing what you want to do:

myarray = np.array( putarrayhere )
count = sum(myarray >= value)

Solution 2

Yeah I think your numpy.array has an extra bracket or it is encompassing another array.

Tried Manually set array as

myarray=np.array([[ 0.47174344,  0.45314669,  0.46395022,  0.47440382,  0.50709627,0.53350065,  0.5233444 ,  0.49974663,  0.48721607,  0.46239652, 0.4693633 ,  0.47263569,  0.47591957,  0.436558  ,  0.43335574,0.44053621,  0.42814804,  0.43201894,  0.43973886, 0.44125302, 0.41176999],[ 0.46509004,  0.46221505,  0.48824086,  0.50088744,  0.53040384,0.53592231,  0.49710228,  0.49821022,  0.47720381,  0.49096272,0.50438366,  0.47173162,  0.48813669,  0.45032002,  0.44776794,0.43910269,  0.43326132,  0.42064458,  0.43472954,  0.45577299,0.43604956]])

and the code works

but setting:

myarray=np.array([[[ 0.47174344,  0.45314669,  0.46395022,  0.47440382,  0.50709627,0.53350065,  0.5233444 ,  0.49974663,  0.48721607,  0.46239652, 0.4693633 ,  0.47263569,  0.47591957,  0.436558  ,  0.43335574,0.44053621,  0.42814804,  0.43201894,  0.43973886, 0.44125302, 0.41176999],[ 0.46509004,  0.46221505,  0.48824086,  0.50088744,  0.53040384,0.53592231,  0.49710228,  0.49821022,  0.47720381,  0.49096272,0.50438366,  0.47173162,  0.48813669,  0.45032002,  0.44776794,0.43910269,  0.43326132,  0.42064458,  0.43472954,  0.45577299,0.43604956]]])

yielded similar errors

Solution 3

Regardless the error you can simply do:

myarray[myarray<value]=0
np.count_nonzero(myarray)

to get your desired result

Share:
15,380

Related videos on Youtube

FaCoffee
Author by

FaCoffee

NHL Winnipeg Jets fan - GO JETS GO! Getting a lot of valuable help here. For DOWN VOTERS: if you are going to down vote, tell the recipient why you are doing so - this way he/she can actually make improvements and gain confidence. Improductive critics should be banned.

Updated on June 04, 2022

Comments

  • FaCoffee
    FaCoffee almost 2 years

    In a numpy.ndarray like this one:

    myarray=
    array([[ 0.47174344,  0.45314669,  0.46395022,  0.47440382,  0.50709627,
             0.53350065,  0.5233444 ,  0.49974663,  0.48721607,  0.46239652,
             0.4693633 ,  0.47263569,  0.47591957,  0.436558  ,  0.43335574,
             0.44053621,  0.42814804,  0.43201894,  0.43973886,  0.44125302,
             0.41176999],
           [ 0.46509004,  0.46221505,  0.48824086,  0.50088744,  0.53040384,
             0.53592231,  0.49710228,  0.49821022,  0.47720381,  0.49096272,
             0.50438366,  0.47173162,  0.48813669,  0.45032002,  0.44776794,
             0.43910269,  0.43326132,  0.42064458,  0.43472954,  0.45577299,
             0.43604956]])
    

    I want to count how many cells exceed a given value, let's say 0.5, and set those that don't to 0.0. This is what I do:

    count=0 
    value=0.5
    for i in range(myarray.shape[0]):
       for j in range(myarray.shape[1]):
           if myarray[i][j]<value:
              myarray[i][j]=0 
           elif myarray[i][j]>=value:     
              count=count+1 
    percentage=round(100*count/(myarray.shape[0]*myarray.shape[1]),2)
    

    However, I get this error: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(), pointing at the line where I check if myarray[i][j]<value.

    Why does this happen and how to fix it? What is the truth value?

    • user2357112
      user2357112 almost 8 years
      Sounds like your array isn't the shape you think it is.
    • Asish M.
      Asish M. almost 8 years
      I was able to run the above snippet without any errors
    • gora
      gora almost 8 years
      No errors. Same script as yours. Got the percentage value as 16.