[英]how can I extract multiple random sub-sequences from a numpy array
say I have a sequence s
and I'd like to select n
random sub sequences from it each with length l
and store in a matrix. 假设我有一个序列
s
,我想从中选择n
随机子序列,每个序列的长度为l
并存储在矩阵中。 Is there a more numpy way of doing that than 有没有比这更麻木的方式了
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])
We can leverage np.lib.stride_tricks.as_strided
based scikit-image's view_as_windows
for efficient patch extraction, like so - 我们可以利用基于
np.lib.stride_tricks.as_strided
的scikit-image's view_as_windows
进行有效的补丁提取,如下所示-
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]
Related : 相关:
NumPy Fancy Indexing - Crop different ROIs from different channels NumPy花式索引-从不同渠道收获不同的投资回报率
Take N first values from every row in NumPy matrix that fulfill condition 从满足条件的NumPy矩阵的每一行中获取N个第一值
Selecting Random Windows from Multidimensional Numpy Array Rows 从多维numpy数组行中选择随机窗口
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.