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.
sliding_window_view(my_array, window_shape, axis=None, *, subok=False, writeable=False)
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. The window_shape
must be less than or equal to the shape of the my_array
input array. The window_shape
is 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=0
moves the window row-wise and axis=1
moves the window column-wise.
For 3D arrays, axis=0
moves the sliding window along the first dimension, which represents the depth or slices of the data. axis=1
and axis=2
move the sliding window row-wise and column-wise, respectively.
subok
: It’s also an optional parameter. If the value is set to True
, the function retains the original array’s subclass type in the output. In case of False
(its default value), it returns a standard NumPy array.
writeable
: It’s also an optional parameter. If the value is set to True
, the output is writable, allowing us to modify the data within the view. If the value is set to False
(its default value), the output is read-only by default.
The function returns an n-dimensional array, which represents the sliding view of the input array.
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)
Line 3: We create a 2D array.
Line 7: We specify the sliding window size.
Line 9: We apply the sliding_window_view
function to create the sliding window view, and the result is assigned to the window_view
variable.
Lines 11–12: We iterate over the window_view
and 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.
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