Determine whether the returned array is a copy or a view.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) print(arr.reshape(2, 4).base) |
The example above returns the original array, indicating that it is a view.
You can include one “unknown” dimension in the reshape method, allowing you to leave one dimension unspecified. By using -1 as the value, NumPy will automatically determine the correct size for that dimension.
Transform a 1-D array with 8 elements into a 3-D array with dimensions of 2×2 elements.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) newarr = arr.reshape(2, 2, –1) print(newarr) |