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. Def
(
x, shape=None, strides=None, subok=False, writeable=True, *, check_bounds=None
)
| 37 | |
| 38 | @set_module("numpy.lib.stride_tricks") |
| 39 | def as_strided( |
| 40 | x, shape=None, strides=None, subok=False, writeable=True, *, check_bounds=None |
| 41 | ): |
| 42 | """ |
| 43 | Create a view into the array with the given shape and strides. |
| 44 | |
| 45 | .. warning:: This function has to be used with extreme care, see notes. |
| 46 | |
| 47 | Parameters |
| 48 | ---------- |
| 49 | x : ndarray |
| 50 | Array to create a new. |
| 51 | shape : sequence of int, optional |
| 52 | The shape of the new array. Defaults to ``x.shape``. |
| 53 | strides : sequence of int, optional |
| 54 | The strides of the new array. Defaults to ``x.strides``. |
| 55 | subok : bool, optional |
| 56 | If True, subclasses are preserved. |
| 57 | writeable : bool, optional |
| 58 | If set to False, the returned array will always be readonly. |
| 59 | Otherwise it will be writable if the original array was. It |
| 60 | is advisable to set this to False if possible (see Notes). |
| 61 | check_bounds : bool or None |
| 62 | Check new stride and shape for potential out of bound memory |
| 63 | access. |
| 64 | |
| 65 | Returns |
| 66 | ------- |
| 67 | view : ndarray |
| 68 | |
| 69 | Raises |
| 70 | ------ |
| 71 | ValueError |
| 72 | If `check_bounds` is True the given shape and strides could result in |
| 73 | out-of-bounds memory access. |
| 74 | |
| 75 | See also |
| 76 | -------- |
| 77 | broadcast_to : broadcast an array to a given shape. |
| 78 | reshape : reshape an array. |
| 79 | lib.stride_tricks.sliding_window_view : |
| 80 | userfriendly and safe function for a creation of sliding window views. |
| 81 | |
| 82 | Notes |
| 83 | ----- |
| 84 | `as_strided` creates a view into the array given the exact strides |
| 85 | and shape. This means it manipulates the internal data structure of |
| 86 | ndarray and, if done incorrectly, the array elements can point to |
| 87 | invalid memory and can corrupt results or crash your program. |
| 88 | It is advisable to always use the original ``x.strides`` when |
| 89 | calculating new strides to avoid reliance on a contiguous memory |
| 90 | layout. |
| 91 | |
| 92 | Furthermore, arrays created with this function often contain self |
| 93 | overlapping memory, so that two elements are identical. |
| 94 | Vectorized write operations on such arrays will typically be |
| 95 | unpredictable. They may even give different results for small, large, |
| 96 | or transposed arrays. |
searching dependent graphs…