简体   繁体   中英

How can I append or concatenate or merge more than 2 numpy arrays?

I have 4 numpy arrays of this shape

(1, 2, 1, 1, 1, 5, 2, 14)
(1, 2, 1, 1, 1, 5, 2, 14)
(1, 2, 1, 1, 1, 5, 2, 14)
(1, 2, 1, 1, 1, 5, 2, 14)

And I want to merge them into a single array. The shape will be:

(4, 2, 1, 1, 1, 5, 2, 14)

Trial 1

np.append(f1, f2, axis=0) which has a shape of (2, 2, 1, 1, 1, 5, 2, 14)

How can I do this?

Or is there another way to manage this data?

The only thing I am certain about, the 4 arrays are of the same shape.

Trial 2

np.concatenate(f1, f2, f3)

Error:

----> 1 np.concatenate(f1, f2, f3)

TypeError: only integer scalar arrays can be converted to a scalar index

Put your arrays in a list then use np.concatenate :

import numpy as np
l = [np.ones((1, 2, 1, 1, 1, 5, 2, 14))] * 4
a = np.concatenate(l, axis=0)
a.shape
Out[9]: (4, 2, 1, 1, 1, 5, 2, 14)

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