Utilities that manipulate strides to achieve desirable effects.
An explanation of strides can be found in the "ndarray.rst" file in the NumPy reference guide.
Function | as_strided |
Create a view into the array with the given shape and strides. |
Function | sliding_window_view |
Create a sliding window view into the array with the given window shape. |
Class | DummyArray |
Dummy object that just exists to hang __array_interface__ dictionaries and possibly keep alive a reference to a base array. |
Function | _broadcast_arrays_dispatcher |
Undocumented |
Function | _broadcast_shape |
Returns the shape of the arrays that would result from broadcasting the supplied arrays against each other. |
Function | _broadcast_to |
Undocumented |
Function | _broadcast_to_dispatcher |
Undocumented |
Function | _maybe_view_as_subclass |
Undocumented |
Function | _sliding_window_view_dispatcher |
Undocumented |
Function | broadcast_arrays |
Broadcast any number of arrays against each other. |
Function | broadcast_shapes |
Broadcast the input shapes into a single shape. |
Function | broadcast_to |
Broadcast an array to a new shape. |
Create a view into the array with the given shape and strides.
Warning
This function has to be used with extreme care, see notes.
If True, subclasses are preserved.
If set to False, the returned array will always be readonly. Otherwise it will be writable if the original array was. It is advisable to set this to False if possible (see Notes).
view : ndarray
broadcast_to : broadcast an array to a given shape. reshape : reshape an array. lib.stride_tricks.sliding_window_view :
userfriendly and safe function for the creation of sliding window views.
as_strided creates a view into the array given the exact strides and shape. This means it manipulates the internal data structure of ndarray and, if done incorrectly, the array elements can point to invalid memory and can corrupt results or crash your program. It is advisable to always use the original x.strides when calculating new strides to avoid reliance on a contiguous memory layout.
Furthermore, arrays created with this function often contain self overlapping memory, so that two elements are identical. Vectorized write operations on such arrays will typically be unpredictable. They may even give different results for small, large, or transposed arrays. Since writing to these arrays has to be tested and done with great care, you may want to use writeable=False to avoid accidental write operations.
For these reasons it is advisable to avoid as_strided when possible.
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.
axis
is not present, must have same length as the number of input
array dimensions. Single integers i
are treated as if they were the
tuple (i,)
.window_shape[i]
will refer to axis i
of x
.
If axis
is given as a tuple of int
, window_shape[i]
will refer to
the axis axis[i]
of x
.
Single integers i
are treated as if they were the tuple (i,)
.broadcast_to: broadcast an array to a given shape.
For many applications using a sliding window view can be convenient, but potentially very slow. Often specialized solutions exist, for example:
scipy.signal.fftconvolve
scipy.ndimage
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.
>>> 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_view
is 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).
Undocumented
Broadcast any number of arrays against each other.
*args
: array_likesThese arrays are views on the original arrays. They are typically not contiguous. Furthermore, more than one element of a broadcasted array may refer to a single memory location. If you need to write to the arrays, make copies first. While you can set the writable flag True, writing to a single output value may end up changing more than one location in the output array.
broadcast broadcast_to broadcast_shapes
>>> x = np.array([[1,2,3]]) >>> y = np.array([[4],[5]]) >>> np.broadcast_arrays(x, y) [array([[1, 2, 3], [1, 2, 3]]), array([[4, 4, 4], [5, 5, 5]])]
Here is a useful idiom for getting contiguous copies instead of non-contiguous views.
>>> [np.array(a) for a in np.broadcast_arrays(x, y)] [array([[1, 2, 3], [1, 2, 3]]), array([[4, 4, 4], [5, 5, 5]])]
Broadcast the input shapes into a single shape.
:ref:`Learn more about broadcasting here <basics.broadcasting>`.
*args
: tuples of ints, or intsbroadcast broadcast_arrays broadcast_to
>>> np.broadcast_shapes((1, 2), (3, 1), (3, 2)) (3, 2)
>>> np.broadcast_shapes((6, 7), (5, 6, 1), (7,), (5, 1, 7)) (5, 6, 7)
Broadcast an array to a new shape.
broadcast broadcast_arrays broadcast_shapes
>>> x = np.array([1, 2, 3]) >>> np.broadcast_to(x, (3, 3)) array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])