How does numpy.lib.stride_tricks.sliding_window_view work?
The sliding_window_view() function of the NumPy module creates a window of specified size within the array. The window slides over the array values and extracts the subarrays, allowing us to efficiently process the overlapping elements in the array. This is often called a sliding, rolling, or moving window. It’s particularly valuable for tasks that require analyzing local neighborhoods or patterns within a larger dataset.
Syntax
sliding_window_view(my_array, window_shape, axis=None, *, subok=False, writeable=False)
Parameters
The sliding_window_view function takes the following parameters:
my_array: It’s an input array having n dimensions from which we intend to extract subsets.window_shape: It defines the size of the sliding window, representing the window’s dimensions along each axis. Thewindow_shapemust be less than or equal to the shape of themy_arrayinput array. Thewindow_shapeis specified as a tuple of integers or a single integer. Each integer in the tuple represents the size of the sliding window along its corresponding axis.axis: It’s an optional parameter that determines the axis or axes along which the sliding window operates.For 2D arrays,
axis=0moves the window row-wise andaxis=1moves the window column-wise.For 3D arrays,
axis=0moves the sliding window along the first dimension, which represents the depth or slices of the data.axis=1andaxis=2move the sliding window row-wise and column-wise, respectively.
subok: It’s also an optional parameter. If the value is set toTrue, the function retains the original array’s subclass type in the output. In case ofFalse(its default value), it returns a standard NumPy array.writeable: It’s also an optional parameter. If the value is set toTrue, the output is writable, allowing us to modify the data within the view. If the value is set toFalse(its default value), the output is read-only by default.
Return value
The function returns an n-dimensional array, which represents the sliding view of the input array.
Code example
import numpy as nparr = np.array([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]])window_shape = (2, 2)window_view = np.lib.stride_tricks.sliding_window_view(arr, window_shape)for window in window_view:print(window)
Explanation
Line 3: We create a 2D array.
Line 7: We specify the sliding window size.
Line 9: We apply the
sliding_window_viewfunction to create the sliding window view, and the result is assigned to thewindow_viewvariable.Lines 11–12: We iterate over the
window_viewand print its value.
The np.lib.stride_tricks.sliding_window_view function is applicable to n-dimensional arrays, offering the flexibility to tailor the window size and axis selection to suit specific requirements. One can experiment with the window_view by changing the dimensions of arr and window_shape and value of axis parameter to observe the resultant output variations.
Uses of sliding window
Sliding windows are used in various fields and have real-world applications across different domains. Some real-world applications that require sliding windows are given below:
Pattern matching: Sliding windows enable efficient and optimized searching for a pattern within a larger text or data sequence by moving a fixed-size window through the data and minimizing unnecessary character comparisons.
Feature extraction: Sliding windows can be used to extract fixed-size feature vectors from variable-length sequences, making them suitable for machine learning models.
Time series forecasting: Sliding windows can be employed to prepare input data for time series forecasting models by segmenting the time series into training examples.
Object detection: In computer vision, sliding windows are employed to detect objects in images at different locations and scales.
Free Resources