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