I 've tried the above syntax for flattening in a separate example to understand it, and I had a problem with the ‘-1’, if I changed it to any other value an error occurs, and I couldn’t figure out what it is doing here.
Thanks.
Thanks.
When using the reshape
function you have to use two dimensions which make sense for the array you are working with.
Example:
a = np.array([0, 1, 2, 3, 4, 5])
# we can reshape the array to be a (3,2)
a.reshape(3, 2)
array([[0, 1],
[2, 3],
[4, 5]])
# when we use -1 the dimension is inferred so it makes sense
# for example if we put (2, -1) the dimension inferred will be 3
# so the array becomes (2, 3)
a.reshape(2, -1)
array([[0, 1, 2],
[3, 4, 5]])
So, the -1 is put above because the second dimension is unknown and we want to invert the array anyway ?
and for the example you gave, what should happen if the array can’t be transformed to (2, -1) or (2, 3) ?
Thanks.
The -1 is not to invert the array but rather to infer the missing value
If the array cannot be reshaped then you get an error, for example, the array a
cannot be reshaped as (5, -1) because there is no number to be put in place of -1 that would allow the array to be reshaped:
>>> a = np.array([0, 1, 2, 3, 4, 5])
>>> a.reshape(5, -1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: cannot reshape array of size 6 into shape (5,newaxis)
Got it, Thanks so much.