如何将numpy数组列表转换为单个numpy数组? [英] How to convert list of numpy arrays into single numpy array?

查看:596
本文介绍了如何将numpy数组列表转换为单个numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有;

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

我尝试转换;

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

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

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

您对最佳有效方法有何建议?

What do you suggest for the best efficient way?

推荐答案

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

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

numpy.concatenate( LIST, axis=0 )

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

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

正如Jorge的答案所指出的那样,还有numpy 1.10中引入的函数stack:

As Jorge's answer points out, there is also the function stack, introduced in numpy 1.10:

numpy.stack( LIST, axis=0 )

这采用了补充方法:它为每个输入数组创建一个新视图并添加一个额外的维数(在这种情况下,在左侧,因此每个n元素的一维数组都变成了一个1-by- n串联之前的2D数组).只有在所有输入数组都具有相同形状的情况下(即使沿着串联轴),该方法也将起作用.

This takes the complementary approach: it creates a new view of each input array and adds an extra dimension (in this case, on the left, so each n-element 1D array becomes a 1-by-n 2D array) before concatenating. It will only work if all the input arrays have the same shape—even along the axis of concatenation.

vstack(或等效的row_stack)通常是一种更易于使用的解决方案,因为它将采用一维和/或二维数组序列,并在必要时且仅在必要时自动扩展维数,在将整个列表连接在一起之前.在需要新尺寸的地方,将其添加到左侧.同样,您可以一次串联整个列表,而无需进行迭代:

vstack (or equivalently row_stack) is often 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 and only where necessary, before concatenating the whole list together. Where a new dimension is required, it is added on the left. Again, you can concatenate a whole list at once without needing to iterate:

numpy.vstack( LIST )

此灵活行为​​也通过语法快捷方式numpy.r_[ array1, ...., arrayN ](请注意方括号)表现出来.这对于连接几个显式命名的数组很有用,但对您的情况不利,因为此语法将不接受数组序列,例如您的LIST.

This flexible behavior is also exhibited by the syntactic shortcut numpy.r_[ array1, ...., arrayN ] (note the square brackets). This is good for concatenating a few explicitly-named arrays but is no good for your situation because this syntax will not accept a sequence of arrays, like your LIST.

还有一个类似的功能column_stack和一个快捷方式c_[...],用于水平(列方式)堆叠,以及一个几乎类似的功能hstack-尽管出于某些原因后者的灵活性较差(对输入数组的维数要求严格,并尝试将一维数组端对端连接起来,而不是将它们视为列).

There is also an analogous function column_stack and shortcut c_[...], for horizontal (column-wise) stacking, as well as an almost-analogous function hstack—although for some reason the latter is less flexible (it is stricter about input arrays' dimensionality, and tries to concatenate 1-D arrays end-to-end instead of treating them as columns).

最后,在垂直堆叠一维数组的特定情况下,以下内容也适用:

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

numpy.array( LIST )

...因为数组可以从其他数组序列中构造出来,因此在开头增加了新的维度.

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

这篇关于如何将numpy数组列表转换为单个numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆