Skip to content Skip to sidebar Skip to footer

What Does Np.reshape(-1) Do

I am aware of what -1 in one of the reshape dimensions does, when the other dimension is provided - say a.reshape(2,-1) ( numpy calculates the dimension for -1, based on the origin

Solution 1:

It seems that is just returns the flattened array, regardless of the initial shape.

x = np.arange(27).reshape((3,3,3))
y = x.reshape(-1)
y.shape
(27,)
y
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26])
x = np.arange(10)
y = x.reshape(-1)
y
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Also, make sure your new shape argument to reshape is a tuple, i.e. a.reshape((2, -1)).

Post a Comment for "What Does Np.reshape(-1) Do"