Numpy Reshape Method

Below is a code snippet from the “Python, Numpy, Vectorization optional lab”:
a = np.arange(6).reshape(-1, 2) #reshape is a convenient way to create matrices

With this code, are we reshaping the array [1,2,3,4,5,6] into a matrix with -1 rows and 2 columns? What does “-1” mean in this case and how do the parameters -1 and 2 lead to the array being reshaped into a matrix with 3 rows and 2 columns?

Thank you!

This is the link you need to read, normally whenever I dont understand something I search in google.

https://numpy.org/doc/stable/reference/generated/numpy.reshape.html#

The -1 means that if the second shape is 2 the the first dimension of the matrix will be 3, so the -1 infers the other dimension automatically from the shape of the input matrix.

1 Like