【发布时间】:2018-08-01 21:59:03
【问题描述】:
假设我有一个序列s,我想从中选择n 随机子序列,每个子序列的长度为l,并存储在一个矩阵中。有没有比
s = np.arange(0, 1000)
n = 5
l = 10
i = np.random.randint(0, len(s)-10, 5)
ss = np.array([s[x:x+l] for x in i])
【问题讨论】:
假设我有一个序列s,我想从中选择n 随机子序列,每个子序列的长度为l,并存储在一个矩阵中。有没有比
s = np.arange(0, 1000)
n = 5
l = 10
i = np.random.randint(0, len(s)-10, 5)
ss = np.array([s[x:x+l] for x in i])
【问题讨论】:
我们可以利用基于scikit-image's view_as_windows 的np.lib.stride_tricks.as_strided 进行有效的补丁提取,就像这样 -
from skimage.util.shape import view_as_windows
# Get sliding windows (these are simply views)
w = view_as_windows(s, l)
# Index with indices, i for desired output
out = w[i]
相关:
NumPy Fancy Indexing - Crop different ROIs from different channels
Take N first values from every row in NumPy matrix that fulfill condition
Selecting Random Windows from Multidimensional Numpy Array Rows
【讨论】: