MCPcopy Create free account
hub / github.com/numpy/numpy / sliding_window_view

Function sliding_window_view

numpy/lib/stride_tricks.py:124–337  ·  view source on GitHub ↗

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. .. versionadded:: 1.20.0 Parameters ---

(x, window_shape, axis=None, *,
                        subok=False, writeable=False)

Source from the content-addressed store, hash-verified

122
123@array_function_dispatch(_sliding_window_view_dispatcher)
124def sliding_window_view(x, window_shape, axis=None, *,
125 subok=False, writeable=False):
126 """
127 Create a sliding window view into the array with the given window shape.
128
129 Also known as rolling or moving window, the window slides across all
130 dimensions of the array and extracts subsets of the array at all window
131 positions.
132
133 .. versionadded:: 1.20.0
134
135 Parameters
136 ----------
137 x : array_like
138 Array to create the sliding window view from.
139 window_shape : int or tuple of int
140 Size of window over each axis that takes part in the sliding window.
141 If `axis` is not present, must have same length as the number of input
142 array dimensions. Single integers `i` are treated as if they were the
143 tuple `(i,)`.
144 axis : int or tuple of int, optional
145 Axis or axes along which the sliding window is applied.
146 By default, the sliding window is applied to all axes and
147 `window_shape[i]` will refer to axis `i` of `x`.
148 If `axis` is given as a `tuple of int`, `window_shape[i]` will refer to
149 the axis `axis[i]` of `x`.
150 Single integers `i` are treated as if they were the tuple `(i,)`.
151 subok : bool, optional
152 If True, sub-classes will be passed-through, otherwise the returned
153 array will be forced to be a base-class array (default).
154 writeable : bool, optional
155 When true, allow writing to the returned view. The default is false,
156 as this should be used with caution: the returned view contains the
157 same memory location multiple times, so writing to one location will
158 cause others to change.
159
160 Returns
161 -------
162 view : ndarray
163 Sliding window view of the array. The sliding window dimensions are
164 inserted at the end, and the original dimensions are trimmed as
165 required by the size of the sliding window.
166 That is, ``view.shape = x_shape_trimmed + window_shape``, where
167 ``x_shape_trimmed`` is ``x.shape`` with every entry reduced by one less
168 than the corresponding window size.
169
170 See Also
171 --------
172 lib.stride_tricks.as_strided: A lower-level and less safe routine for
173 creating arbitrary views from custom shape and strides.
174 broadcast_to: broadcast an array to a given shape.
175
176 Notes
177 -----
178 For many applications using a sliding window view can be convenient, but
179 potentially very slow. Often specialized solutions exist, for example:
180
181 - `scipy.signal.fftconvolve`

Callers 8

test_1dMethod · 0.90
test_2dMethod · 0.90
test_2d_with_axisMethod · 0.90
test_2d_repeated_axisMethod · 0.90
test_2d_without_axisMethod · 0.90
test_errorsMethod · 0.90
test_writeableMethod · 0.90
test_subokMethod · 0.90

Calls 3

normalize_axis_tupleFunction · 0.90
as_stridedFunction · 0.85
anyMethod · 0.45

Tested by 8

test_1dMethod · 0.72
test_2dMethod · 0.72
test_2d_with_axisMethod · 0.72
test_2d_repeated_axisMethod · 0.72
test_2d_without_axisMethod · 0.72
test_errorsMethod · 0.72
test_writeableMethod · 0.72
test_subokMethod · 0.72