I am looking to reshape an array as follows:
Current array
a.shape = (1,4)
a[0][0].shape = (144,256)
I want the array to instead be shape
(1,4,144,256)
Any help would be appreciated
Related
I created an array with size (256, 144, 3).
empty_windows = np.empty((256, 144, 3))
Then I want to append new elements into the array with:
for i in range(256):
for j in range(144):
empty_windows[i, j] = np.append(empty_windows[i, j], np.asarray(some_new_array)).reshape(3, )
But it doesnt work as I get the error msg:
ValueError: cannot reshape array of size 6 into shape (3,)
Is there a way of doing it? Thank you.
I hope, it will help you understanding concatenate 3dim array
import numpy as np
empty_windows = np.empty((256, 144, 3))
random_arr = np.random.randint(0, 100, size=(256, 144, 3)) # it's dimension should be same
np.concatenate([empty_windows, random_arr], axis=2) # it can concatenate into an array axis=2 defines 3rd dimension
np.empty and np.append are dangerous functions to use. They are not clones of the the empty list [] and list.append.
empty_windows = np.empty((256, 144, 3))
has made a (256,144,3) shape array with float values - they are unpredictable, but more than likely not what you want. Look at that array, or a smaller example to see for yourself. Also read, and if necessary reread, the np.empty docs. np.zeros is safer.
With scalar i,j,
empty_windows[i, j]
is a (3,) shape array, or slot.
When you np.append it with another (3,) shape, the result is a (6,) shape, with the first 3 value being those "random" values originally in empty_window. The error tells you quite clearly that it can't put a (6,) shape array into a slot that only holds (3,).
Your goal isn't clear, but you can't grow a (n,m,3) shape array to (n,m,6) by doing this kind of "row" by "row" append.
You can set the "row" with new values, as in:
empty_windows[i, j] = np.asarray(some_new_array)).reshape(3, )
I have a multidimensional array and a set of scale factors that I want to apply along the first axis:
>>> data.shape, scale_factors.shape
((22, 20, 2048, 2048), (22,))
>>> data * scale_factors
ValueError: operands could not be broadcast together with shapes (22,20,2048,2048) (22,)
I can do this with apply_along_axis, but is there a vectorized way to do this? I found a similar question, but the solution is specific to a 1-D * 2-D operation. The "data" ndarray will not always be the same shape, and won't even always have the same number of dimensions. But the length of the 1-D scale_factors will always be the same as axis 0 of data.
You can try reshape the data into 2D, then broadcast scale_factor to 2D, and reshape back:
(data.reshape(data.shape[0], -1) * scale_factors[:,None]).reshape(data.shape)
Or, you can swap the 0-th axis to the last so you can broadcast:
(data.swapaxes(0,-1) * scale_factors).swapaxes(0,-1)
data * scale_factors.reshape([-1]+[1]*(len(data.shape)-1))
data * scale_factors[:,None,None,None]
I basically have a numpy array of shape (9400,20,30).
I want to take slices of 200 2-Dimensional arrays (Shape = (200,20,30)) and then flatten those to do some calculations.
Then I want to turn the array back to my original (9400,20,30) shape.
Any help is welcome! Thank you!
you should slice it and reshape it like this:
smallArray = bigArray[:200]
bigArray = bigArray[200:]
print(f'bigArray sliced shape: {np.shape(bigArray)}')
print(f'smallArray shape: {np.shape(smallArray)}')
smallArray = smallArray.flatten()
print(f'smallArray flattened: {np.shape(smallArray)}')
smallArray = np.reshape(smallArray, (200,30,20))
print(f'smallArray reshaped shape: {np.shape(smallArray)}')
PD:
I'm not sure slicing the big array is a good Idea, because then you are going to keep an array of smaller arrays to reshape it afterwards, consider leaving it and try:
smallArray = bigArray[i*200:(i+1)*200]
I want to reshape this array: np.array(np.arange(15)) to a 3d array that is built from a 3x3 array and a 3x2 array.
I've tried to do it with the reshape method but it didn't work.
I thought that maybe reshape can get a number of tuples maybe.
a=np.array(np.arange(15)).reshape(1,((3,2),(3,3)))
but I then I saw it cant.
How can I reshape it then? is there a nice way?
a multidimensional array can't have dimensions with different size.
but if you want a tuple you will need to split the array in 2 parts, the first that match in size with the 3x3 array and the second that match the 3x2, at this point you'll have 2 one dimensional array, then reshape them
arr1 = arr1.reshape((3,3))
arr2 = arr2.reshape((3,2))
tuple = arr1, arr2
I want to reshape array of size (3,1) to (3,) with following code:
import numpy as np
a=np.random.random(size=(4,3,1))
a[1]=a[1].reshape(3,)
But getting following error:
ValueError: could not broadcast input array from shape (3) into shape (3,1)
how to solve it.
As per I understand, your array is consist of array of array (a.shape = (4,3,1)).
I do understand that a[1].shape = (3,1) seems to be not so different to a[1].shape = (3,), the program language however doesn't understand that way ((3,1) != (3,)) which means (3,1) and (3,) are totally different, since a[2],a[3] remain having shape = (3,1), every array within an array of array must have the same shape (3,1). Therefore, you need to reshape all the array at once or alternatively make a copy of a[1] to another variable and reshape this variable instead.
a = a.reshape(4,3)
and use a[1]
alternatively:
b = a[1]
b = b.reshape(3,)