Can't reshape numpy array Can't reshape numpy array numpy numpy

Can't reshape numpy array


As mentioned in the comments, you are really always just modifying one array with different shapes. It doesn't really make sense in numpy to say that you have a 2d array of 1 x 3 arrays. What that really is is actually a n x 3 array.

We start with a 1d array of length 3*n (I've added three numbers to your example to make the difference between a 3 x n and n x 3 array clear):

>>> import numpy as np>>> rgbValues = np.array([14, 25, 19, 24, 25, 28, 58, 87, 43, 1, 2, 3])>>> rgbValues.shape(12,)

And reshape it to be n x 3:

>>> lmsValues = rgbValues.reshape(-1, 3)>>> lmsValuesarray([[14, 25, 19],       [24, 25, 28],       [58, 87, 43],       [ 1,  2,  3]])>>> lmsValues.shape(4, 3)

If you want each element to be shaped 3 x 1, maybe you just want to transpose the array. This switches rows and columns, so the shape is 3 x n

>>> lmsValues.Tarray([[14, 24, 58,  1],       [25, 25, 87,  2],       [19, 28, 43,  3]])>>> lmsValues.T.shape(3, 4)>>> lmsValues.T[0]array([14, 24, 58,  1])>>> lmsValues.T[0].shape(4,)

If you truly want each element in lmsValues to be a 1 x 3 array, you can do that, but then it has to be a 3d array with shape n x 1 x 3:

>>> lmsValues = rgbValues.reshape(-1, 1, 3)>>> lmsValuesarray([[[14, 25, 19]],       [[24, 25, 28]],       [[58, 87, 43]],       [[ 1,  2,  3]]])>>> lmsValues.shape(4, 1, 3)>>> lmsValues[0]array([[14, 25, 19]])>>> lmsValues[0].shape(1, 3)