在Numpy中为一个3D阵列添加新行[重复]。

问题描述 投票:0回答:1

如何为一个3D数组添加新行?

例:假设我们有以下尺寸的数组。

arr1.shape = (1,1,20)
arr2.shape = (1,1,20)

combined.shape = (1,2,20)

如何在numpy中实现这个功能?

python arrays numpy
1个回答
1
投票

你可以使用 concatenate 并指定所需的 axis 像下面这样。

import numpy as np

arr1 = np.ones((1, 1, 20))
arr2 = np.ones((1, 1, 20))

print(np.concatenate((arr1, arr2), axis=1).shape)

或者你可以用 hstack 像以下这些:

import numpy as np

arr1 = np.ones((1, 1, 20))
arr2 = np.ones((1, 1, 20))

print(np.hstack((arr1,arr2)).shape)
© www.soinside.com 2019 - 2024. All rights reserved.