从满足条件的NumPy矩阵的每一行中获取N个第一值

从满足条件的NumPy矩阵的每一行中获取N个第一值,第1张

从满足条件的NumPy矩阵的每一行中获取N个第一值

方法1

这是一个

broadcasting
-

def takeN_le_per_row_broadcasting(a, b, N=3): # a, b : 1D, 2D arrays respectively    # First col indices in each row of b with <= corresponding one in a    idx = (b <= a[:,None]).argmax(1)    # Get all N ranged column indices    all_idx = idx[:,None] + np.arange(N)    # Finally advanced-index with those indices into b for desired output    return b[np.arange(len(all_idx))[:,None], all_idx]

方法#2

NumPy Fancy Indexing - Crop different ROIs from differentchannels
的解决方案启发,我们可以利用
np.lib.stride_tricks.as_strided
高效的补丁提取,例如-

from skimage.util.shape import view_as_windowsdef takeN_le_per_row_strides(a, b, N=3): # a, b : 1D, 2D arrays respectively    # First col indices in each row of b with <= corresponding one in a    idx = (b <= a[:,None]).argmax(1)    # Get 1D sliding windows for each element off data    w = view_as_windows(b, (1,N))[:,:,0]    # Use fancy/advanced indexing to select the required ones    return w[np.arange(len(idx)), idx]


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5650458.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存