Can numpy add the second dim automatically when reshaping?

Viewed 48

I was wondering if numpy can supply the second dim automatically, because I don't want to calculate everytime, for example:

a1 = np.array([[ 0,  1,  2,  3,  4,  5],[ 6,  7,  8,  9, 10, 11]])
a2 = np.reshape(a1,(3, a1.shape[0] * a1.shape[1] / 3))

However, if you use a2 = np.reshape(a1, (3, )) it shows error:

cannot reshape array of size 12 into shape (3,)

1 Answers

Use -1 for the extra dimension:

a2 = np.reshape(a1,(3, -1))

Or a little simpler

a2 = a1.reshape(3,-1)
Related