简体   繁体   中英

np.concatenate a list of numpy.ndarray in new dimension?

I have a list with numpy.ndarrays - each of shape (33,1,8,45,3)

Problem that when i concatenate the list using a = np.concatenate(list) The output shape of a becomes

print a.shape 
(726,1,8,45,3)

instead of shape (22,33,1,8,45,3) .

How do I cleanly concatenate the list, without having to change the input.

np.concatenate :

Join a sequence of arrays along an existing axis .

np.stack :

Stack a sequence of arrays along a new axis .

a = np.ones((3, 4))
b = np.stack([a, a])
print(b.shape)  # (2, 3, 4)

You can use numpy.array() or numpy.stack() :

import numpy
a = [numpy.random.rand(33,1,8,45,3) for i in range(22)]

b = numpy.array(a)
b.shape    # (22, 33, 1, 8, 45, 3)

c = numpy.stack(a, axis=0)
c.shape    # (22, 33, 1, 8, 45, 3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM