I didnot understand why the(-1,2) functions same as (3,2)? where did this -1 come from?
a = np.arange(6).reshape(-1, 2)
This line of code first created a 1-D Vector of six elements. It then reshaped that vector into a 2-D array using the reshape command. This could have been written: a = np.arange(6).reshape(3, 2)
To arrive at the same 3 row, 2 column array. The -1 argument tells the routine to compute the number of rows given the size of the array and the number of columns.
Yup, you are correct about the -1 argument. In this case, the reshape(-1, 2) is the same as reshape(3, 2).
According to the official documentation, one of the dimensions in reshape() can be -1, in which case the routine/method will infer/compute the number automatically.
Right! The “-1” on reshape just means “use whatever is left here”. That’s a nice easy way to make the code more general. E.g. if you know you want 2 columns, but the number of rows could be different based on the size of the input, you can use that technique to avoid having to either “hard-code” the number of rows or to do the work of computing what it should be.