Transform dimensions using reshape(), flatten(), ravel(), and transpose (.T).
Reshaping changes the dimensional arrangement of an array without altering its underlying data. The total element count must remain unchanged.
Reshaping a 1D strip of 6 numbers into a 2x3 grid is like folding a strip of 6 stamps into 2 rows of 3 stamps without ripping any stamps.
Pass `-1` as one dimension in `.reshape(r, -1)` and NumPy will automatically compute the required dimension size.
mat = arr.reshape(2, 3); flat = mat.flatten()arr = np.arange(6); arr.reshape(2, 4) # 2x4 = 8 != 6arr = np.arange(6); arr.reshape(2, 3)The product of the new shape dimensions must exactly equal the total number of elements.
Solve the objective below using NumPy vectorized syntax
Create `arr = np.array([10, 20, 30, 40])`, reshape it into a 2x2 matrix using `.reshape(2, 2)`, and print it.
[[10 20]
[30 40]]