sliding window of M-by-N shape numpy.ndarray

后端 未结 6 754
日久生厌
日久生厌 2020-11-28 22:20

I have a numpy array of shape (6,2)

[[00,01],
 [10,11],
 [20,21],
 [30,31],
 [40,41],
 [50,51]]

I need a sliding window with step size 1 an

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 23:03

    The solution is

    np.lib.stride_tricks.as_strided(a, shape=(4,6), strides=(8,4)).

    Using strides is intuitive when you start thinking in terms of pointers/addresses.

    The as_strided() method has 3 arguments.

    1. data
    2. shape
    3. strides

    data is the array on which we would operate.

    To use as_strided() for implementing sliding window functions, we must compute the shape of the output beforehand. In the question, (4,6) is the shape of output. If the dimensions are not correct, we end up reading garbage values. This is because we are accessing data by moving the pointer by a couple of bytes (depending on data type).

    Determining the correct value of strides is essential to get expected results. Before calculating strides, find out the memory occupied by each element using arr.strides[-1]. In this example, the memory occupied by one element is 4 bytes. Numpy arrays are created in row major fashion. The first element of the next row is right next to the last element of the current row.

    Ex:

    0 , 1 | 10, 11 | ...
    

    10 is right next to 1.

    Imagine the 2D array reshaped to 1D (This is acceptable as the data is stored in a row-major format). The first element of each row in the output is the odd indexed element in the 1D array.

    0, 10, 20, 30, ..
    

    Therefore, the number of steps in memory we need to take to move from 0 to 10, 10 to 20, and so on is 2 * mem size of element. Each row has a stride of 2 * 4bytes = 8. For a given row in the output, all the elements are adjacent to each other in our imaginary 1D array. To get the next element in a row, just take one stride equal to the size of an element. The value of column stride is 4 bytes.

    Therefore, strides=(8,4)

    An alternate explanation: The output has a shape of (4,6). Column stride 4. So, the first row elements start at index 0 and have 6 elements each spaced 4 bytes apart. After the first row is collected, the second row starts 8 bytes away from the starting of the current row. The third row starts 8 bytes away from the starting point of the second row and so on.

    The shape determines the number of rows and columns we need. strides define the memory steps to start a row and collect a column element

提交回复
热议问题