I have 2 lists l1, l2 that contain 5 numpy arrays each of the following shapes:
[(32, 84, 84, 1), (32,), (32,), (32,), (32, 84, 84, 1)]
The result I want is achieved using:
[np.concatenate(item) for item in zip(l1, l2)]
The resulting shapes are (which are the expected / desired result)
[(64, 84, 84, 1), (64,), (64,), (64,), (64, 84, 84, 1)]
However when I try to concatenate them directly using np.concatenate(l1, l2), I get an error:
ValueError: could not broadcast input array from shape (32,84,84,1) into shape (32)
So I expand dims for the middle 3 arrays in each l1 and l2, the resulting shapes are:
[(32, 84, 84, 1), (1, 32), (1, 32), (1, 32), (32, 84, 84, 1)]
I then try to concatenate again:
np.concatenate((l1, l2))
And the resulting shapes are:
[(32, 84, 84, 1),
(1, 32),
(1, 32),
(1, 32),
(32, 84, 84, 1),
(32, 84, 84, 1),
(1, 32),
(1, 32),
(1, 32),
(32, 84, 84, 1)]
And trying with np.concatenate((l1, l2), axis=1) results in the following error:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3418, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-111-0928bf6cff84>", line 1, in <module>
ccc = np.concatenate((aaa, bbb), axis=1)
File "<__array_function__ internals>", line 5, in concatenate
numpy.AxisError: axis 1 is out of bounds for array of dimension 1
So, is there a way to do it without a for loop?
np.random.random()and specify the shapes I mentioned and try yourself, it won't make a difference what the values are.map(np.concatenate, zip(l1, l2))but under the hood its still going to have to iterate all the items returned from zip and apply the np function to them. you seem to be under the impression that for loops must just be slow. if you run it and find a bottle neck or performance problem them re-evaluate the issue. but given your current scenario your always going to have to iterate over the items and for loops are pretty efficient at that