说我有一个序列s,我想从中选择n个随机子序列,每个子序列的长度为l,并存储在矩阵中.有没有更多的方式来做到这一点 s = np.arange(0, 1000)n = 5l = 10i = np.random.randint(0, len(s)-10, 5)ss = np.array(
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]
相关:
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