如何在Python中将2D数组组合成3D数组?

6

我有一个项目,其中有一个for循环大约运行了14次。在每次迭代中,都会创建一个形状为(4,3)的二维数组。我想将这些2D数组连接成一个3D数组(形状为4,3,14),以便每个2D数组都位于不同的“层”中。 在Python中应该如何实现?

4个回答

4

我尝试了这个,但是它没有成功,因为在第一次迭代中,我只有一个2D数组。在第二次迭代中,我有两个2D数组,我想将它们连接成一个3D数组。在第三次迭代中,我想将3D数组与新的2D数组连接起来,但发生了一些问题。 - pyigal
1
@pyigal:做你正在做的事情会非常慢。相反,创建一个列表,在循环中将每个数组附加到其中,然后在最后使用dstack()函数将整个列表堆叠起来。这样做会快得多,因为它避免了数据被复制N次的问题。 - John Zwinck
你的意思是使用这个函数ndarray.tolist(),然后再用dstack()吗? - pyigal
不。请再次阅读我的上一条评论。先制作一个数组列表,然后将它们dstack在一起。 - John Zwinck
那么最终的数组是一个numpy数组吗? - pyigal
为什么不试一下呢? - John Zwinck

1
如果您提到的数组大小是静态的,您可以执行以下操作。
output_array = numpy.zeros((14,4,3), dtype=np.float32)
for i in range(14):
    mat = numpy.random.rand(4,3)
    output_array[i] = mat

你需要初始化最终数组的大小,然后循环并将矩阵(4,3)分配给相应的循环计数器的索引。
形状和最终矩阵如下:
numpy.shape(output_array)

返回你

(14, 4, 3)

输出是。
array([[[ 0.62507486,  0.3246161 ,  0.43934602],
    [ 0.14476213,  0.76139957,  0.92813474],
    [ 0.26556504,  0.02475475,  0.90740073],
    [ 0.08017973,  0.97526789,  0.2213122 ]],

   [[ 0.70042586,  0.8122381 ,  0.79289031],
    [ 0.0369414 ,  0.10780825,  0.77501732],
    [ 0.10386232,  0.86237574,  0.5829311 ],
    [ 0.1888348 ,  0.85105735,  0.31599012]],

   [[ 0.26350111,  0.8787083 ,  0.12869285],
    [ 0.25927794,  0.25701383,  0.81212741],
    [ 0.06661031,  0.53449911,  0.50212061],
    [ 0.40009728,  0.78002244,  0.81524432]],

   [[ 0.49921468,  0.82028496,  0.51261139],
    [ 0.62790054,  0.64566481,  0.02624587],
    [ 0.39364958,  0.99537313,  0.33225098],
    [ 0.88214922,  0.20252077,  0.78350848]],

   [[ 0.29032609,  0.95975012,  0.06733917],
    [ 0.24497923,  0.51818371,  0.93520784],
    [ 0.80267638,  0.88271469,  0.30779642],
    [ 0.57030594,  0.34175804,  0.52563131]],

   [[ 0.61039209,  0.57186425,  0.76554799],
    [ 0.55681604,  0.33107477,  0.05680386],
    [ 0.15465826,  0.13452645,  0.09498007],
    [ 0.29682869,  0.93196124,  0.94435322]],

   [[ 0.23904459,  0.94893754,  0.97033942],
    [ 0.89159942,  0.85306913,  0.02144577],
    [ 0.57696968,  0.82578647,  0.33358794],
    [ 0.81979036,  0.73351973,  0.027876  ]],

   [[ 0.6568135 ,  0.25458351,  0.10369358],
    [ 0.06151289,  0.00939822,  0.00798484],
    [ 0.92518032,  0.19057493,  0.84838325],
    [ 0.78189474,  0.15273546,  0.34607282]],

   [[ 0.46961641,  0.19778872,  0.1498462 ],
    [ 0.55704814,  0.96889585,  0.08894933],
    [ 0.48003736,  0.59383452,  0.42212519],
    [ 0.78752649,  0.07204869,  0.4215464 ]],

   [[ 0.6454156 ,  0.84189773,  0.10041234],
    [ 0.89345407,  0.60821944,  0.56667495],
    [ 0.62806529,  0.67642623,  0.4951494 ],
    [ 0.85371262,  0.13159418,  0.3402876 ]],

   [[ 0.39828625,  0.50659049,  0.34835485],
    [ 0.06839356,  0.74652916,  0.5722388 ],
    [ 0.20762053,  0.0692997 ,  0.02790474],
    [ 0.84786427,  0.98461425,  0.19105092]],

   [[ 0.36976317,  0.44268745,  0.23061621],
    [ 0.47827819,  0.43044546,  0.90150601],
    [ 0.2307732 ,  0.61590552,  0.82066673],
    [ 0.49611789,  0.4480612 ,  0.46685895]],

   [[ 0.40907925,  0.15996945,  0.05480348],
    [ 0.70230347,  0.00926704,  0.97775948],
    [ 0.19834276,  0.20127937,  0.44351548],
    [ 0.48512974,  0.07319999,  0.5580616 ]],

   [[ 0.35749629,  0.88443983,  0.55465496],
    [ 0.61600298,  0.08260803,  0.4010818 ],
    [ 0.40910226,  0.31984288,  0.50188118],
    [ 0.34836289,  0.14394118,  0.06841569]]], dtype=float32)

希望有所帮助。

谢谢。我想我会使用这个选项。 - pyigal
不客气。也许你可以接受这个作为正确答案,并更明确地表达感谢 ;) - Raja Sattiraju

0

在评论中,@JohnZwinck建议向2D数组添加层。

#data_in是原始数据,形状为(14,n,m) #循环涉及逐层处理(并应用适用的函数),然后将结果2D数组存储到列表中:

以下是有效的解决方案:

new_list = []
for i in range(0,14): #looping 14 times (or equivalent to size of dimension considered)
    print (i)
    print (data_in[i,:,:].shape) #Choosing a level makes it a 2D array 

#(so, the shape at this stage will be (n,m))
    new_list.append(data_in)
 
final_list = np.dstack(new_list)


#In case the final dimension gets added as the last dimension, do this:
a = np.empty((14,n,m))
final_list = np.transpose(a, (0,1,2))

print (final_list.shape)

-2
如果你的数组只是一个列表,那么可以在开始时定义一个空列表,并将项目添加到其中: foo=[] for i in range(14): ... foo.append(tab)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接