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?