[英]how can I extract multiple random sub-sequences from a numpy array
假設我有一個序列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])
我們可以利用基於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]
相關:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.