Skip to main content
`Ctrl`+`K`

# numpy.lib.stride_tricks.as_strided

lib.stride_tricks.as_strided(x, shape=None, strides=None, subok=False, writeable=True, *, check_bounds=None)

  Create a view into the array with the given shape and strides.

  Warning

  This function has to be used with extreme care, see notes.

  Parameters:
    x ndarray

      Array to create a new.

    shape sequence of int, optional

      The shape of the new array. Defaults to `x.shape`.

    strides sequence of int, optional

      The strides of the new array. Defaults to `x.strides`.

    subok bool, optional

      If True, subclasses are preserved.

    writeable bool, optional

      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).

    check_bounds bool or None

      Check new stride and shape for potential out of bound memory access.

  Returns:
    view ndarray
  Raises:
    ValueError

      If check_bounds is True the given shape and strides could result in out-of-bounds memory access.

  See also

  `broadcast_to`

    broadcast an array to a given shape.

  `reshape`

    reshape an array.

  `lib.stride_tricks.sliding_window_view`

    userfriendly and safe function for a creation of sliding window views.

  Notes

  `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.

  Examples

      >>> import numpy as np
      ... from numpy.lib.stride_tricks import as_strided
      ... x = np.arange(10)
      ... y = as_strided(x, shape=(5,), strides=(8,), check_bounds=True)
      ... y
      array([0, 1, 2, 3, 4])

  Attempting to create an out-of-bounds view and use `check_bounds=True` as_strided will raises an error:

      >>> as_strided(x, shape=(20,), strides=(8,), check_bounds=True)
      Traceback (most recent call last):
      ...
      ValueError: Given shape and strides would access memory out of bounds...

  When working with views, bounds are checked against the base array:

      >>> a = np.arange(1000)
      ... b = a[:2]
      ... c = as_strided(b, shape=(2,), strides=(400,), check_bounds=True)
      ... c[0], c[1]
      (0, 50)