Shape of an array is the number of elements in each dimension.
To print the shape of 2D array:
arr = np.array([[1,2,3],[4,5,6]])
print(arr.shape)
Output: (2,4)
The output shows that the array has 2 dimensions, where 1st dimension has 2 elements and 2nd dimension has 4 elements.
Reshaping means changing the dimensions or changing the number of elements in each dimension in an array.
To reshape 1D to 2D:
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3)
Output: [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]]
We can also reshape from 1D to 3D.
As long as the elements required for shaping are equal in both shapes. We can reshape into any shape.
A 1D 8-element array can be reshaped into a 2D 4-element array, but we can't reshape it into a 3D 3-element array because it'll require 9 elements.
NumPy allows to have one unknown dimension while reshaping. It means we don't have to specify an exact number of dimensions. We can simply pass "-1".
arr.reshape(2,2,-1)
It means converting a multidimensional array into a 1D array.
arr.reshape(-1)