X_flatten = X.reshape(X.shape[0], -1).T
I did not expect the -1
in the code.
X_flatten = X.reshape(X.shape[0], -1).T
I did not expect the -1
in the code.
We can figure that out by looking at the documentation of numpy. You can find that page by googling for numpy reshape documentation
.
There is this part, which I found by CTRLF searching for -1
.
newshape: int or tuple of ints
The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.
Further down the page, there is an example:
>>> np.reshape(a, (3,-1)) # the unspecified value is inferred to be 2
array([[1, 2],
[3, 4],
[5, 6]])
So we can conclude that the -1
is inferred to be whatever number makes sense so that the number of elements in the reshaped array is the same as the number of elements in the original one. This is also why only one of the dimensions may be -1
- it would be impossible to know what you wanted if there would be multiple.
numpy.reshape has three parameters: (a, newshape, order='C'). Here -1 defines the second parameter: newshape
"newshape: int or tuple of ints
The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions."