如何使用过滤器函数修复数组布尔错误 [英] How to fix array boolean error using filter function
问题描述
我正在尝试使用过滤器来解决布尔错误
I am trying to use solve the Boolean error using filter
我使用了一个过滤器数组来解决迭代数组的布尔问题.它只适用于一个简单的列表,但是当它仅用于从数组中获取大于零的那些数字时,它再次显示错误.填充数组的方法是从标准正态分布中提取样本.
I used a filter array to solve the Boolean problem of iterating arrays. It worked for a simple list, however it is again showing error when used to take only those numbers which are greater than zero from an array. The method used to populate the array is drawing samples from a standard normal distribution.
arr2 = np.array(list(filter(lambda x:x>0,rand_num)))
arr2
<ipython-input-80-af65f7c09d82> in <module>
1 rand_num = np.random.randn(5,5)
----> 2 arr2 = np.array(list(filter(lambda x:x>0,rand_num)))
3 arr2
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
推荐答案
您已经创建了一个二维浮点数组:
You have created a 2d array of floats:
In [60]: rand_num = np.random.randn(5,5)
In [61]: rand_num
Out[61]:
array([[ 1.89811694, 0.44414858, -2.52994217, -0.17974148, -0.91167712],
[ 0.06534556, 0.04677172, -0.81580021, 0.08053772, -0.55459303],
[ 0.41316473, -0.35859064, 1.28860476, -0.22666389, 0.97562048],
[ 0.29465373, 0.71143579, -0.55552921, 0.37660919, 0.31482962],
[ 0.2768353 , -1.32999438, 0.0594767 , 1.50255302, 0.08658897]])
我们可以选择带有布尔掩码的> 0:
We can select the ones that are >0 with a boolean mask:
In [62]: rand_num>0
Out[62]:
array([[ True, True, False, False, False],
[ True, True, False, True, False],
[ True, False, True, False, True],
[ True, True, False, True, True],
[ True, False, True, True, True]])
In [63]: rand_num[rand_num>0]
Out[63]:
array([1.89811694, 0.44414858, 0.06534556, 0.04677172, 0.08053772,
0.41316473, 1.28860476, 0.97562048, 0.29465373, 0.71143579,
0.37660919, 0.31482962, 0.2768353 , 0.0594767 , 1.50255302,
0.08658897])
对数组进行布尔索引会生成一个1d数组-因为每一行的True值数量都可以变化.
Boolean indexing of a array produces a 1d array - because each row can vary in the number of True values.
filter
像map
一样在数组的第一维上迭代:
filter
like map
iterates on the first dimension of the array:
In [64]: list(map(lambda x:x>0, rand_num))
Out[64]:
[array([ True, True, False, False, False]),
array([ True, True, False, True, False]),
array([ True, False, True, False, True]),
array([ True, True, False, True, True]),
array([ True, False, True, True, True])]
列表理解形式中的同一件事:
same thing in list comprehension form:
In [65]: [x>0 for x in rand_num]
Out[65]:
[array([ True, True, False, False, False]),
array([ True, True, False, True, False]),
array([ True, False, True, False, True]),
array([ True, True, False, True, True]),
array([ True, False, True, True, True])]
请注意,迭代的每个元素如何都是一个形状为(5,)的numpy数组.这就是filter
的原因.它期望一个简单的True/False布尔值,而不是一个数组. Python if
和or
具有相同的问题. (实际上,我认为是numpy
拒绝将多项目数组传递给期望标量的Python函数,而是引发了这种歧义性错误.)
Notice how each element of the iteration is a numpy array of shape (5,). That's what the filter
is choking on. It expects a simple True/False boolean, not an array. Python if
and or
have the same problem. (Actually I think it's numpy
that's refusing to pass the multi-item array to the Python function that expects the scalar, and instead raises this ambiguity error.)
您可以将filter
应用于rand_num
的每一行:
You could apply the filter
to each row of rand_num
:
In [66]: [list(filter(lambda x: x>0, row)) for row in rand_num]
Out[66]:
[[1.898116938827415, 0.4441485849428062],
[0.06534556093009064, 0.04677172433407727, 0.08053772013844711],
[0.41316473050686314, 1.2886047644946972, 0.9756204798856322],
[0.2946537313273924,
0.711435791237748,
0.3766091899348284,
0.31482961532956577],
[0.27683530300005493,
0.05947670354791034,
1.502553021817318,
0.0865889738396504]]
这些数字与Out[63]
中的数字相同,但按行划分-每个项中的项数不同.
These are the same numbers as in Out[63]
, but split up by row - with a different number of items in each.
@Willem Van Onsem的遮罩数组格式中的内容相同:
The same thing in the @Willem Van Onsem's masked array format:
In [69]: np.ma.masked_array(rand_num, mask=rand_num <= 0)
Out[69]:
masked_array(
data=[[1.898116938827415, 0.4441485849428062, --, --, --],
[0.06534556093009064, 0.04677172433407727, --,
0.08053772013844711, --],
[0.41316473050686314, --, 1.2886047644946972, --,
0.9756204798856322],
[0.2946537313273924, 0.711435791237748, --, 0.3766091899348284,
0.31482961532956577],
[0.27683530300005493, --, 0.05947670354791034, 1.502553021817318,
0.0865889738396504]],
mask=[[False, False, True, True, True],
[False, False, True, False, True],
[False, True, False, True, False],
[False, False, True, False, False],
[False, True, False, False, False]],
fill_value=1e+20)
这篇关于如何使用过滤器函数修复数组布尔错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!