Python:"使用a.any()或a.all()"穿越numpy.ndarray

时间:2016-08-04 21:43:43

标签: python numpy for-loop ambiguity

在像这样的numpy.ndarray中:

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]])

我想计算有多少单元格超过给定值,让我们说0.5,并将那些单元格设置为0.0。这就是我的工作:

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)

但是,我收到此错误:ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(),指向我检查if myarray[i][j]<value的行。

为什么会发生这种情况以及如何解决?真值是什么?

3 个答案:

答案 0 :(得分:5)

通常,您可以比较两个数字以获得真值。例如:

elem = 5
if elem < 6:
    # do something

相当于:

if true:
    # do something

但是,您无法将数组与值进行比较。例如:

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

相反,您可以获得任何或所有元素是否满足条件的真值。例如:

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

我在上面运行了你的示例代码并发现没有错误,所以我不确定是什么问题。但这是你应该注意的。考虑打印您正在比较的元素,看它是否是一个数组。

此外,这是做你想做的事情的一种较短的方式:

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

答案 1 :(得分:1)

是的,我认为你的numpy.array有一个额外的括号,或者它包含另一个数组。

尝试手动将数组设置为

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]])

代码正常工作

但设置:

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]]])

产生了类似的错误

答案 2 :(得分:1)

无论您遇到什么错误,都可以做到:

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

获得您想要的结果