what dose newshape (, -1) do?

Viewed 49

could you explain the role of newshape in reshape and what dose newshape=(n_channels, -1) mean? I wanna use the below code to reshape my signal matrix, but I don't understand the last part

np.reshape(np.transpose(signal, axes=[1, 2, 0]), newshape=(n_channel, -1))
1 Answers

Let's say, You want to reshape your NumPy array with 10*2 dimension to 5*4. You need:

a = np.zeros((10, 2)) # Generates 10*2 zero matrix
np.reshape(a, newshape=(5, 4))

This gives output like this:

array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])

The same output can be produced by:

np.reshape(a, newshape=(5, -1))

Because the value for second dimension is inferred from the length of the array and remaining dimensions. Here it will take 4, but if you want 2*10 array then you can use newshape=(2,-1) here second value will be 10.

Related