bundles / numpy 2.4.4 / numpy / lib / stride_tricks / sliding_window_view
_ArrayFunctionDispatcher
numpy.lib.stride_tricks:sliding_window_view
Signature
def sliding_window_view ( x , window_shape , axis = None , * , subok = False , writeable = False ) Summary
Create a sliding window view into the array with the given window shape.
Extended Summary
Also known as rolling or moving window, the window slides across all dimensions of the array and extracts subsets of the array at all window positions.
Parameters
x: array_likeArray to create the sliding window view from.
window_shape: int or tuple of intSize of window over each axis that takes part in the sliding window. If
axisis not present, must have same length as the number of input array dimensions. Single integersiare treated as if they were the tuple(i,).axis: int or tuple of int, optionalAxis or axes along which the sliding window is applied. By default, the sliding window is applied to all axes and
window_shape[i]will refer to axisiofx. Ifaxisis given as atuple of int,window_shape[i]will refer to the axisaxis[i]ofx. Single integersiare treated as if they were the tuple(i,).subok: bool, optionalIf True, sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default).
writeable: bool, optionalWhen true, allow writing to the returned view. The default is false, as this should be used with caution: the returned view contains the same memory location multiple times, so writing to one location will cause others to change.
Returns
view: ndarraySliding window view of the array. The sliding window dimensions are inserted at the end, and the original dimensions are trimmed as required by the size of the sliding window. That is,
view.shape = x_shape_trimmed + window_shape, wherex_shape_trimmedisx.shapewith every entry reduced by one less than the corresponding window size.
Notes
For many applications using a sliding window view can be convenient, but potentially very slow. Often specialized solutions exist, for example:
filtering functions in scipy.ndimage
moving window functions provided by bottleneck.
As a rough estimate, a sliding window approach with an input size of N and a window size of W will scale as O(N*W) where frequently a special algorithm can achieve O(N). That means that the sliding window variant for a window size of 100 can be a 100 times slower than a more specialized version.
Nevertheless, for small window sizes, when no custom algorithm exists, or as a prototyping and developing tool, this function can be a good solution.
Examples
import numpy as np from numpy.lib.stride_tricks import sliding_window_view x = np.arange(6) x.shape v = sliding_window_view(x, 3) v.shape v✓
i, j = np.ogrid[:3, :4] x = 10*i + j x.shape x shape = (2,2) v = sliding_window_view(x, shape) v.shape✓
v
✗v = sliding_window_view(x, 3, 0) v.shape v✓
v = sliding_window_view(x, (2, 3), (1, 1)) v.shape✓
v
✗x = np.arange(7) sliding_window_view(x, 5)[:, ::2]✓
x = np.arange(7) sliding_window_view(x, 3)[::2, :]✓
x = np.arange(6) x.shape v = sliding_window_view(x, 3) v.shape v moving_average = v.mean(axis=-1) moving_average✓
v = sliding_window_view(x, 3) v[0,1] = 10✓
x = np.arange(6) # reset x for the second example v = sliding_window_view(x, 3, writeable=True) v[0,1] = 10 x v✓
See also
- broadcast_to
broadcast an array to a given shape.
- lib.stride_tricks.as_strided
A lower-level and less safe routine for creating arbitrary views from custom shape and strides.
Aliases
-
numpy.lib._stride_tricks_impl.sliding_window_view