[英]Numpy elementwise greater (for each element in another array)
I struggle to write find the best "question" so please feel free to suggest another title.我很难写找到最好的“问题”,所以请随时提出另一个标题。
Lets say I have a=np.array([5,3,2,4])
and b=np.array([1,2])
- I want to get a list of list (or np.arrays) with the value of a>b[i]
ie it can be written as a list comprehension假设我有a=np.array([5,3,2,4])
和b=np.array([1,2])
- 我想用a>b[i]
值,即它可以写成列表推导式
[a[i]>p for p in b]
which returns [a[i]>p for p in b]
返回
[np.array([True,True,True,True]), np.array([True,True,False,True])]
. [np.array([True,True,True,True]), np.array([True,True,False,True])]
。 Since I have a rather large data set I hoped that there was a numpy function to do such, or is list comprehension the better way here?由于我有一个相当大的数据集,我希望有一个 numpy function 这样做,还是列表理解在这里更好?
You can use numpy
broadcasting for this, you just need to add an extra dimension into each of your arrays:您可以为此使用numpy
广播,您只需在每个 arrays 中添加一个额外的维度:
>>> a[None,:] > b[:,None]
array([[ True, True, True, True],
[ True, True, False, True]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.