【问题标题】:how can I extract multiple random sub-sequences from a numpy array如何从 numpy 数组中提取多个随机子序列
【发布时间】: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])

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    我们可以利用基于scikit-image's view_as_windowsnp.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

    【讨论】:

    • 太棒了,解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    相关资源
    最近更新 更多