简体   繁体   English

Numpy 元素更大(对于另一个数组中的每个元素)

[英]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.

相关问题 对于一个已排序的 numpy 数组中的每个元素,如何减去另一个已排序数组中的最小较大值 - For each element in one sorted numpy array, how to subtract the smallest greater value in another sorted array 最接近值的元素(元素方式,numpy 数组) - Closest element to a value (Elementwise, numpy array) 计算 numpy 数组中每个元素的出现次数,其中元素在元素上与另一个数组相等? - Count the occuarences of each elements in numpy array, where elements are elementwise equal with another array? 如何将一个 numpy 数组元素除以另一个较低维度的 numpy 数组 - How to divide a numpy array elementwise by another numpy array of lower dimension 检查numpy数组中的每个元素是否在另一个数组中 - Check if each element in a numpy array is in another array 比较一个numpy数组和另一个数组的每个元素 - Compare a numpy array to each element of another one 将一个数组的每一行与numpy中另一个数组的每个元素相乘 - Multiply each row of one array with each element of another array in numpy Numpy - 对于 array1 中的每个元素 X,在 array2 中找到大于 X 的第一个元素的索引 - Numpy - for each element X in array1 find index of first element greater than X in array2 Numpy 数组,但每个 1 是一个元素 - Numpy array but each 1 is an element Numpy 用另一个矩阵元素替换矩阵 - Numpy replace matrix elementwise with another matrix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM