Class np.lib.stride_tricks
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
NumPy's numpy.lib.stride_tricks: views that re-read an array's memory with other strides.
sliding_window_view(NDArray, Shape, NDAxis?, bool) is the safe one (every window lies inside
the array); as_strided(NDArray, Shape?, long[], bool, bool) takes any shape and byte strides, and where NumPy reads whatever
memory they reach, NumSharp refuses a view that leaves the array's buffer (the bounds-checked form the owner
approved in API-parity batch 12).
[ModuleName("np.lib.stride_tricks")]
public static class np.lib.stride_tricks
- Inheritance
-
np.lib.stride_tricks
- Inherited Members
Methods
as_strided(NDArray, Shape?, long[], bool, bool)
A VIEW of x's memory with any shape and strides - NumPy's
as_strided(x, shape=None, strides=None, subok=False, writeable=True): element (i, j, ...)
is the item istrides[0] + jstrides[1] + ... bytes past x's first element
(as_strided(np.arange(6L), new Shape(4, 3), new long[] { 8, 8 }) is the overlapping windows
[[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5]]).
public static NDArray as_strided(NDArray x, Shape? shape = null, long[] strides = null, bool subok = false, bool writeable = true)
Parameters
xNDArrayThe array whose memory is viewed (any layout; the view starts at its first element).
shapeShape?The view's shape; null keeps
x's.strideslong[]The view's strides in BYTES (NumPy's), one per dimension; null keeps
x's - for a C-contiguousxthe C-contiguous strides of the view's shape, as NumPy's array interface reports no strides for it.subokboolKeep
x's class (anumpy.matrixstays a matrix, its finalize rule applied); false (the default) gives a plain array.writeableboolFalse makes the view read-only; true keeps
x's writeability (a read-only array's view stays read-only).
Returns
- NDArray
The view: it shares
x's memory (OWNDATA false), and a write through it reaches every element that reads the same item.
Remarks
NumPy's statements: np.array(x, copy=None, subok=subok), its __array_interface__
with the given shape / strides, np.asarray(DummyArray(interface, base=x)) (strides of another
length than the shape are refused there, before a negative dimension), the subclass view, then
if view.flags.writeable and not writeable: view.flags.writeable = False.
[Misaligned] by design: NumPy's own documentation calls such calls unsafe (it reads past the array, or misaligned items); NumSharp refuses them instead of reading foreign memory. A zero stride (a broadcast-like view) and negative strides are fine. API-parity batch 12.
Footgun, as in NumPy: a zero or overlapping stride makes several elements read ONE item, and the
view is still writeable (unlike np.broadcast_to's), so a single write changes all of them; pass
writeable: false unless the writes are meant.
Exceptions
- ArgumentNullException
xis null.- ValueError
NumPy's: strides of another length than the shape (
mismatch in length of strides and shape), a negative dimension (negative dimensions are not allowed); NumSharp's refusals where NumPy reads arbitrary memory: a stride that is not a multiple of the item size (NumSharp's strides count whole items) or a view reaching outsidex's buffer.- ValueError
A
numpy.matrixview (subok) of more than two long axes (shape too large to be a matrix.).
sliding_window_view(NDArray, Shape, int, bool)
sliding_window_view(NDArray, Shape, NDAxis?, bool) with the axis as one int: the
overload a C# integer binds (the owner's kept integer form beside every NumPy-axis member),
forwarding to the NDAxis form, so every answer and error is that overload's.
public static NDArray sliding_window_view(NDArray x, Shape window_shape, int axis, bool writeable = false)
Parameters
xNDArrayThe array to take windows of.
window_shapeShapeThe window size (one entry: one axis).
axisintThe axis to slide over (negative counts from the end).
writeableboolFalse (the default): a read-only view.
Returns
- NDArray
The windowed view.
Exceptions
- ArgumentNullException
xis null.- ValueError
The NDAxis form's window checks.
- AxisError
axisis out of range.
sliding_window_view(NDArray, Shape, int[], bool)
sliding_window_view(NDArray, Shape, NDAxis?, bool) with the axes as an int[]: the
overload a C# array (and a bare null, NumPy's None) binds — the owner's kept integer form,
forwarding to the NDAxis form.
public static NDArray sliding_window_view(NDArray x, Shape window_shape, int[] axis, bool writeable = false)
Parameters
xNDArrayThe array to take windows of.
window_shapeShapeThe window size per axis.
axisint[]The axes to slide over (a repeat windows an axis again); null is every axis.
writeableboolFalse (the default): a read-only view.
Returns
- NDArray
The windowed view.
Exceptions
- ArgumentNullException
xis null.- ValueError
The NDAxis form's window checks.
- AxisError
An axis is out of range.
sliding_window_view(NDArray, Shape, NDAxis?, bool)
A sliding-window VIEW of x — NumPy's
sliding_window_view(x, window_shape, axis=None, *, subok=False, writeable=False): every window
of window_shape over the chosen axes, as trailing dimensions, without copying.
public static NDArray sliding_window_view(NDArray x, Shape window_shape, NDAxis? axis = null, bool writeable = false)
Parameters
xNDArrayThe array to take windows of (any layout: transposed, strided, broadcast, 0-d).
window_shapeShapeThe window size per windowed axis: an int (one axis) or a tuple; one entry per axis in
axis(every axis ofxwhenaxisis null). A 0 is legal (an empty window); a negative entry is NumPy's ValueError.axisNDAxis?The axes to slide over: null (NumPy's None) is every axis in order; an int or a tuple (negative axes count from the end). An axis may repeat — NumPy windows it again.
writeableboolFalse (the default) returns a READ-ONLY view (a write raises NumPy's
assignment destination is read-only); true leaves it writeable whenxis, so writes reachx— and, as windows overlap, one element may appear in several windows: NumPy's warning that writing through such a view is rarely what one wants applies.
Returns
- NDArray
A view of
x's buffer, of shapex.shapewith each windowed axis trimmed ton - w + 1, followed bywindow_shape; its strides arex's followed by the windowed axes' strides. A plain array (NumPy'ssubok=False).
Remarks
https://numpy.org/doc/stable/reference/generated/numpy.lib.stride_tricks.sliding_window_view.html
NumPy's statements: the window checks, normalize_axis_tuple(axis, x.ndim,
allow_duplicate=True), out_strides = x.strides + tuple(x.strides[ax] for ax in axis), the
trimmed shape, then as_strided(x, strides=out_strides, shape=out_shape, writeable=writeable) —
here a Shape over x's own buffer window (its offset and buffer size),
so every window stays inside the memory x may read. The view holds its own
reference to that buffer, so it outlives a disposed x.
A moving reduction is a reduction over the window axes:
np.mean(sliding_window_view(x, 3), axis: -1) is the 3-point moving average. Added in API-parity
batch 7 with the module facade.
Exceptions
- ArgumentNullException
xis null.- ValueError
In NumPy's order: a negative
window_shapeentry (); with a nullwindow_shapecannot contain negative valuesaxis, awindow_shapelength other thanx.ndim(Since axis is); a length other than the axes' (None, must provide window_shape for all dimensions ofx; got 1 window_shape elements andx.ndimis 2.Must provide matching length window_shape and axis; ...); a window longer than its axis (window shape cannot be larger than input array shape).- AxisError
An axis out of range (
axis 2 is out of bounds for array of dimension 2).