Gives a new shape to an array without changing its data. Parameters ---------- a : array_like Array to be reshaped. newshape : int or tuple of ints The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D a
(a, newshape, order='C')
| 199 | # not deprecated --- copy if necessary, view otherwise |
| 200 | @array_function_dispatch(_reshape_dispatcher) |
| 201 | def reshape(a, newshape, order='C'): |
| 202 | """ |
| 203 | Gives a new shape to an array without changing its data. |
| 204 | |
| 205 | Parameters |
| 206 | ---------- |
| 207 | a : array_like |
| 208 | Array to be reshaped. |
| 209 | newshape : int or tuple of ints |
| 210 | The new shape should be compatible with the original shape. If |
| 211 | an integer, then the result will be a 1-D array of that length. |
| 212 | One shape dimension can be -1. In this case, the value is |
| 213 | inferred from the length of the array and remaining dimensions. |
| 214 | order : {'C', 'F', 'A'}, optional |
| 215 | Read the elements of `a` using this index order, and place the |
| 216 | elements into the reshaped array using this index order. 'C' |
| 217 | means to read / write the elements using C-like index order, |
| 218 | with the last axis index changing fastest, back to the first |
| 219 | axis index changing slowest. 'F' means to read / write the |
| 220 | elements using Fortran-like index order, with the first index |
| 221 | changing fastest, and the last index changing slowest. Note that |
| 222 | the 'C' and 'F' options take no account of the memory layout of |
| 223 | the underlying array, and only refer to the order of indexing. |
| 224 | 'A' means to read / write the elements in Fortran-like index |
| 225 | order if `a` is Fortran *contiguous* in memory, C-like order |
| 226 | otherwise. |
| 227 | |
| 228 | Returns |
| 229 | ------- |
| 230 | reshaped_array : ndarray |
| 231 | This will be a new view object if possible; otherwise, it will |
| 232 | be a copy. Note there is no guarantee of the *memory layout* (C- or |
| 233 | Fortran- contiguous) of the returned array. |
| 234 | |
| 235 | See Also |
| 236 | -------- |
| 237 | ndarray.reshape : Equivalent method. |
| 238 | |
| 239 | Notes |
| 240 | ----- |
| 241 | It is not always possible to change the shape of an array without copying |
| 242 | the data. |
| 243 | |
| 244 | The `order` keyword gives the index ordering both for *fetching* the values |
| 245 | from `a`, and then *placing* the values into the output array. |
| 246 | For example, let's say you have an array: |
| 247 | |
| 248 | >>> a = np.arange(6).reshape((3, 2)) |
| 249 | >>> a |
| 250 | array([[0, 1], |
| 251 | [2, 3], |
| 252 | [4, 5]]) |
| 253 | |
| 254 | You can think of reshaping as first raveling the array (using the given |
| 255 | index order), then inserting the elements from the raveled array into the |
| 256 | new array using the same kind of index ordering as was used for the |
| 257 | raveling. |
| 258 |