简体   繁体   English

使用numpy从许多2D数组构建3D数组

[英]Building a 3D array from a number of 2D arrays with numpy

Let's start with 2 2D arrays: 让我们从2个2D数组开始:

import numpy as np
a = np.zeros( (3,4) )
b = np.zeros( (3,4) )

Now let's combine them into a 3D array: 现在让我们将它们组合成一个3D数组:

c = np.stack( (a,b) )

Everything fine so far, but how to add an additional 2D array to the 3D array, the following is not working: 到目前为止一切都很好,但是如何向3D阵列添加额外的2D阵列,以下是行不通的:

np.stack( (c,a) )

So, my question is how to add an additional layer to the 3D array? 那么,我的问题是如何在3D阵列中添加额外的图层? (numpy version 1.12.1 ) (numpy版本1.12.1

If you know all of your 2D arrays at the start, you can just stack more than two of them: 如果您在开始时就知道所有的2D数组,那么您可以叠加两个以上的数组:

import numpy as np
a = np.zeros((3, 4))
b = np.zeros((3, 4))
c = np.stack((a, b, a))

If you already have one "stacked" array and want to add another array to it, you can use eg numpy.concatenate : 如果你已经有一个“堆叠”数组并想要添加另一个数组,你可以使用例如numpy.concatenate

If the array you want to add is "flat", you would have to wrap it in a list to make the dimensions match. 如果要添加的数组是“平面”,则必须将其包装在列表中以使尺寸匹配。 By default, the arrays are joined along the first dimension (same as if you were to specify axis=0 in the keyword arguments): 默认情况下,数组沿第一个维度连接(就像在关键字参数中指定axis=0 ):

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

c = np.concatenate((c, [a]))

If both arrays are already "stacked", this will also work: 如果两个阵列已经“堆叠”,这也将起作用:

c = np.concatenate((c, c))

You can add a new axis with None/np.newaxis at the start of the array to be appended : a[None,:,:] or simply a[None,...] or just a[None] and for stacking use np.vstack . 您可以在要追加的数组的开头添加一个带有None/np.newaxis的新轴: a[None,:,:]或简单地a[None,...]或只是a[None]并用于堆叠使用np.vstack

Here's a sample run to make things clear - 这是一个让事情变得清晰的示例 -

In [14]: c.shape
Out[14]: (2, 3, 4)

In [15]: d = np.vstack((c,a[None]))

In [16]: d.shape
Out[16]: (3, 3, 4)

In [17]: e = np.vstack((d,a[None]))

In [18]: e.shape
Out[18]: (4, 3, 4)

Workflow 工作流程

So, the workflow would be : 因此,工作流程将是:

1) To start off with 2D arrays, use new axes for the arrays : 1)从2D阵列开始,为阵列使用新轴:

c = np.vstack( (a[None],b[None]) )

2) For later appending steps, use new axis for the incoming 2D array and use np.vstack to stack with the existing 3D array - 2)对于后续的附加步骤,使用新轴作为传入的2D阵列,并使用np.vstack与现有的3D阵列堆叠 -

d = np.vstack((c,a[None]))

Using np.concatenate for performance : 使用np.concatenate获得性能:

np.vstack under the hoods uses np.concatenate as a special case when we need to stack along the first axis. 当我们需要沿着第一个轴堆叠时, np.vstack下的np.vstack使用np.concatenate作为特例。 So, if we want to make use of np.concatenate maybe for performance reasons to avoid the additional function call overhead, we need to specify the axis of concatenation, which would be the first axis. 因此,如果我们想利用np.concatenate可能出于性能原因避免额外的函数调用开销,我们需要指定连接轴,这将是第一个轴。

Thus, with np.concatenate - 因此,使用np.concatenate -

In [23]: d = np.concatenate((c, a[None]), axis=0)

In [24]: d.shape
Out[24]: (3, 3, 4)

In [25]: e = np.concatenate((d, a[None]), axis=0)

In [26]: e.shape
Out[26]: (4, 3, 4)

First make a the same dimension as c 首先做一个相同的尺寸为c
result= np.append(c, [a], axis=0)
But it is not effective to change size of numpy arrays unlike of lists 但是,与列表不同,改变numpy数组的大小是无效的

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

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