Selecting Random Windows from Multidimensional Numpy Array Rows

前端 未结 2 1628
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 08:12

I have a large array where each row is a time series and thus needs to stay in order.

I want to select a random window of a given size for each row.

Example

2条回答
  •  遥遥无期
    2020-11-27 08:41

    In the return statement, change the slicing to advanced indexing, also you need to fix the sampling code a little bit:

    def select_random_windows(arr, window_size):
        offsets = np.random.randint(0, arr.shape[1]-window_size+1, size=arr.shape[0])
        return arr[np.arange(arr.shape[0])[:,None], offsets[:,None] + np.arange(window_size)]
    
    select_random_windows(arr, 3)
    #array([[ 4,  5,  6],
    #       [ 7,  8,  9],
    #       [17, 18, 19],
    #       [25, 26, 27],
    #       [31, 32, 33],
    #       [39, 40, 41]])
    

提交回复
热议问题