简体   繁体   English

Python,如何将二维数组堆叠到 3D 数组中

[英]Python, How can I stack a 2D array into a 3D array

Now,i have a 3D(c) array and a 2D(b) array,i want to make a new 3D(d) array, so what shall I do?现在,我有一个 3D(c) 数组和一个 2D(b) 数组,我想制作一个新的 3D(d) 数组,我该怎么办? :

c=np.array([[[1, 2, 3],[2, 3, 4]],[[1, 2, 3],[2, 3, 4]]])
c.shape
(2, 2, 3)
a=np.array([[1, 2, 3],[2, 3, 4]])
a.shape
(2, 3)

d=np.array([[[1, 2, 3],[2, 3, 4]],[[1, 2, 3],[2, 3, 4]],[[1,2,3],[1,2,3]]])
d.shape
(3, 2, 3)

You first need to reshape one of them, then you can use vstack or dstack depends on which one you want to use.您首先需要reshape其中一个,然后您可以使用vstackdstack取决于您要使用哪个。 For example I use dstack :例如我使用dstack

c = c.reshape((2, 3, 2))
np.dstack((c, a)).shape

i solved it.我解决了。 b.reshape(1,2,3), then d=np.vstack((c,b)) b.reshape(1,2,3),然后 d=np.vstack((c,b))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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