The reason why we use the -1 is that it provides the new shape to the existing dimension. Also, the new shape should be compatible with the original shape.
Let’s see how the numpy allow us to give one of the new shape parameters as -1.
For example : (2,-1). After seeing the shape the numpy will look at the length of the array and remaining dimensions and it will make sure it satisfies the above-mentioned criteria.
Below is an example that shows how it is done:-
import numpy as np
z = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
z.shape
Now the real thing comes as you will trying to reshape with (-1). This will result in new shape which will be (12) and this new shape is compatible with original shape (3,4).
import numpy as np
z = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(z.reshape(-1))
To know more about this you can have a look at the following video tutorial:-