Python – What is the idea of “-1” in numpy reshape?

Today I was “fighting” with the numpy reshape function and I found something strange like matrix.reshape(1, -1). Thus, I have researched quite some time and I found, that the -1 is actually a placeholder for python to change the dimensions of the array, given the other dimensions.

E.g., if you declare the following matrix:

Then, you may kindly ask python to change its dimensions to “6” in rows and whatever is left as a column. “Whatever is left” in our case is 2, and this “whatever” is presented as -1. Thus the following code:

kindly presents:

If you want to try the rest of the reshape() function, then I will not take the fun away from you, by telling you the results:

Use it wisely! 😉

Tagged with: , , ,