sliding_window_view¶
- sliding_window_view(x, window_shape, axis=None, *, subok=False, writeable=False)[source][source]¶
Create a sliding window view into the array with the given window shape.
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 (
ArrayLike) – Array to create the sliding window view from.window_shape (
intortupleofint) – Size of window over each axis that takes part in the sliding window. Ifaxisis not present, must have same length as the number of input array dimensions. Single integersiare treated as if they were the tuple(i,).axis (
intortupleofint, optional) – Axis or axes along which the sliding window is applied. By default, the sliding window is applied to all axes andwindow_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, optional) – If True, sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default).writeable (
bool, optional) – When 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 – Sliding 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.- Return type:
ndarray
See also
lib.stride_tricks.as_stridedA lower-level and less safe routine for creating arbitrary views from custom shape and strides.
broadcast_tobroadcast an array to a given shape.
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.ndimageMoving window functions provided by bottleneck.
As a rough estimate, a sliding window approach with an input size of
Nand a window size ofWwill scale asO(N*W)where frequently a special algorithm can achieveO(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 ieeg.arrays.reshape import sliding_window_view >>> x = np.arange(6) >>> x.shape (6,) >>> v = sliding_window_view(x, 3) >>> v.shape (4, 3) >>> v array([[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5]])
This also works in more dimensions, e.g.
>>> i, j = np.ogrid[:3, :4] >>> x = 10*i + j >>> x.shape (3, 4) >>> x array([[ 0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23]]) >>> shape = (2,2) >>> v = sliding_window_view(x, shape) >>> v.shape (2, 3, 2, 2) >>> v array([[[[ 0, 1], [10, 11]], [[ 1, 2], [11, 12]], [[ 2, 3], [12, 13]]], [[[10, 11], [20, 21]], [[11, 12], [21, 22]], [[12, 13], [22, 23]]]])
The axis can be specified explicitly:
>>> v = sliding_window_view(x, 3, 0) >>> v.shape (1, 4, 3) >>> v array([[[ 0, 10, 20], [ 1, 11, 21], [ 2, 12, 22], [ 3, 13, 23]]])
The same axis can be used several times. In that case, every use reduces the corresponding original dimension:
>>> v = sliding_window_view(x, (2, 3), (1, 1)) >>> v.shape (3, 1, 2, 3) >>> v array([[[[ 0, 1, 2], [ 1, 2, 3]]], [[[10, 11, 12], [11, 12, 13]]], [[[20, 21, 22], [21, 22, 23]]]])
Combining with stepped slicing (
::step), this can be used to take sliding views which skip elements:>>> x = np.arange(7) >>> sliding_window_view(x, 5)[:, ::2] array([[0, 2, 4], [1, 3, 5], [2, 4, 6]])
Or views which move by multiple elements
>>> x = np.arange(7) >>> sliding_window_view(x, 3)[::2, :] array([[0, 1, 2], [2, 3, 4], [4, 5, 6]])
A common application of
sliding_window_viewis the calculation of running statistics. The simplest example is the moving average:>>> x = np.arange(6) >>> x.shape (6,) >>> v = sliding_window_view(x, 3) >>> v.shape (4, 3) >>> v array([[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5]]) >>> moving_average = v.mean(axis=-1) >>> moving_average array([1., 2., 3., 4.])
Note that a sliding window approach is often not optimal (see Notes).