首页 > 解决方案 > 如何附加或连接或合并超过 2 个 numpy 数组?

问题描述

我有 4 个这种形状的 numpy 数组

(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)

我想将它们合并到一个数组中。形状将是:

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

试验 1

np.append(f1, f2, axis=0)它的形状为(2, 2, 1, 1, 1, 5, 2, 14)

我怎样才能做到这一点?

还是有其他方法来管理这些数据?

我唯一确定的是,4 个阵列的形状相同。

试验 2

np.concatenate(f1, f2, f3)

错误:

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

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

标签: pythonnumpy

解决方案


将您的数组放在一个列表中,然后使用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)

推荐阅读