Reputation: 2563
The array that I'm trying to enter has length 240.
I've tried:
r = np.reshape(array, (3, 80))
because I read somewhere else on this site that the rows and columns entered into reshape have to multiply to the array length.
However, I'm still getting the error:
ValueError: total size of new array must be unchanged
Upvotes: 1
Views: 5814
Reputation: 152587
You said you have additional dimensions in your array so you need to keep them:
>>> arr = np.random.random((240, 215, 3))
>>> reshaped = np.reshape(arr, (3, 80, arr.shape[1], arr.shape[2]))
>>> reshaped.shape
(3, 80, 215, 3)
or using unpacking to avoid hardcoding the dimensions:
>>> reshaped = np.reshape(arr, (3, 80, *arr.shape[1:]))
(3, 80, 215, 3)
If you want the last dimension to be ravelled then you could also use -1
as last axis in your reshape:
>>> reshaped_ravel = np.reshape(arr, (3, 80, -1))
>>> reshaped_ravel.shape
(3, 80, 645)
Upvotes: 1