如何将numpy数组列表转换为单个numpy数组?

时间:2021-04-06 12:32:36

Suppose I have ;

假设我有;

LIST = [[array([1, 2, 3, 4, 5]), array([1, 2, 3, 4, 5],[1,2,3,4,5])] # inner lists are numpy arrays

I try to convert;

我试着转换;

array([[1, 2, 3, 4, 5],
       [1, 2, 3, 4, 5],
       [1, 2, 3, 4, 5])

I am solving it by iteration on vstack right now but it is really slow for especially large LIST

我现在通过vstack上的迭代来解决它,但对于特别大的LIST来说它真的很慢

What do you suggest for the best efficient way?

您对最有效的方式有何看法?

2 个解决方案

#1


39  

In general you can concatenate a whole sequence of arrays along any axis:

通常,您可以沿任意轴连接整个数组序列:

numpy.concatenate( LIST, axis=0 )

but you do have to worry about the shape and dimensionality of each array in the list (for a 2-dimensional 5x3 output, you need to ensure that they are all 2-dimensional n-by-3 arrays already). If you want to concatenate 1-dimensional arrays as the rows of a 2-dimensional output, you need to expand their dimensionality.

但你必须担心列表中每个数组的形状和维度(对于2维5x3输出,你需要确保它们都是2维n-by-3数组)。如果要将一维数组连接为二维输出的行,则需要扩展其维数。

vstack is an easier-to-use solution because it will take a sequence of 1- and/or 2-dimensional arrays and expand the dimensionality automatically where necessary, before concatenating the whole list together. Where a new dimension is required, it is added on the left, so each n-element 1D array becomes a 1-by-n 2D array. Again, you can concatenate a whole list at once without needing to iterate:

vstack是一种更容易使用的解决方案,因为这将需要1和/或2维阵列的序列,并且自动扩展的维度在必要时,串联整个列表在一起之前。在需要新维度的情况下,将其添加到左侧,因此每个n元素1D阵列变为1乘n的2D阵列。同样,您可以一次连接整个列表而无需迭代:

numpy.vstack( LIST )

There is also an analogous hstack function for concatenating 1- and/or 2-D arrays as columns of a 2-D output (where expansion is necessary, a new dimension is added on the right so that an n-element 1D array becomes an n-by-1 2D array).

还有一个类似的hstack函数,用于将1-和/或2-D阵列连接为2-D输出的列(需要扩展,右侧添加新维度,以便n元素1D阵列变为n-by-1 2D阵列)。

Finally, in the specific case of vertical stacking of 1-D arrays, the following also works:

最后,在垂直堆叠1-D阵列的特定情况下,以下也适用:

numpy.array( LIST )

...because arrays can be constructed out of a sequence of other arrays, adding a new dimension to the beginning.

...因为数组可以由一系列其他数组构成,在开头添加一个新维度。

#2


3  

Starting in NumPy version 1.10, we have the method stack. It can stack arrays of any dimension (all equal):

从NumPy版本1.10开始,我们有方法堆栈。它可以堆叠任何维度的数组(全部相等):

# List of arrays.
L = [np.random.randn(5,4,2,5,1,2) for i in range(10)]

# Stack them using axis=0.
M = np.stack(L)
M.shape # == (10,5,4,2,5,1,2)
np.all(M == L) # == True

M = np.stack(L, axis=1)
M.shape # == (5,10,4,2,5,1,2)
np.all(M == L) # == False (Don't Panic)

# This are all true    
np.all(M[:,0,:] == L[0]) # == True
all(np.all(M[:,i,:] == L[i]) for i in range(10)) # == True

Enjoy,

#1


39  

In general you can concatenate a whole sequence of arrays along any axis:

通常,您可以沿任意轴连接整个数组序列:

numpy.concatenate( LIST, axis=0 )

but you do have to worry about the shape and dimensionality of each array in the list (for a 2-dimensional 5x3 output, you need to ensure that they are all 2-dimensional n-by-3 arrays already). If you want to concatenate 1-dimensional arrays as the rows of a 2-dimensional output, you need to expand their dimensionality.

但你必须担心列表中每个数组的形状和维度(对于2维5x3输出,你需要确保它们都是2维n-by-3数组)。如果要将一维数组连接为二维输出的行,则需要扩展其维数。

vstack is an easier-to-use solution because it will take a sequence of 1- and/or 2-dimensional arrays and expand the dimensionality automatically where necessary, before concatenating the whole list together. Where a new dimension is required, it is added on the left, so each n-element 1D array becomes a 1-by-n 2D array. Again, you can concatenate a whole list at once without needing to iterate:

vstack是一种更容易使用的解决方案,因为这将需要1和/或2维阵列的序列,并且自动扩展的维度在必要时,串联整个列表在一起之前。在需要新维度的情况下,将其添加到左侧,因此每个n元素1D阵列变为1乘n的2D阵列。同样,您可以一次连接整个列表而无需迭代:

numpy.vstack( LIST )

There is also an analogous hstack function for concatenating 1- and/or 2-D arrays as columns of a 2-D output (where expansion is necessary, a new dimension is added on the right so that an n-element 1D array becomes an n-by-1 2D array).

还有一个类似的hstack函数,用于将1-和/或2-D阵列连接为2-D输出的列(需要扩展,右侧添加新维度,以便n元素1D阵列变为n-by-1 2D阵列)。

Finally, in the specific case of vertical stacking of 1-D arrays, the following also works:

最后,在垂直堆叠1-D阵列的特定情况下,以下也适用:

numpy.array( LIST )

...because arrays can be constructed out of a sequence of other arrays, adding a new dimension to the beginning.

...因为数组可以由一系列其他数组构成,在开头添加一个新维度。

#2


3  

Starting in NumPy version 1.10, we have the method stack. It can stack arrays of any dimension (all equal):

从NumPy版本1.10开始,我们有方法堆栈。它可以堆叠任何维度的数组(全部相等):

# List of arrays.
L = [np.random.randn(5,4,2,5,1,2) for i in range(10)]

# Stack them using axis=0.
M = np.stack(L)
M.shape # == (10,5,4,2,5,1,2)
np.all(M == L) # == True

M = np.stack(L, axis=1)
M.shape # == (5,10,4,2,5,1,2)
np.all(M == L) # == False (Don't Panic)

# This are all true    
np.all(M[:,0,:] == L[0]) # == True
all(np.all(M[:,i,:] == L[i]) for i in range(10)) # == True

Enjoy,