Python 如何将 numpy 数组列表转换为单个 numpy 数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27516849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert list of numpy arrays into single numpy array?
提问by erogol
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?
你对最有效的方法有什么建议?
采纳答案by jez
In general you can concatenate a whole sequence of arrays along any axis:
通常,您可以沿任何轴连接整个数组序列:
numpy.concatenate( LIST, axis=0 )
but you dohave 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.
但你也必须对列表中的形状和每个阵列的维度担心(用于2维3x5的输出,你需要确保它们都是2维正由-5阵列的话)。如果要将一维数组串联起来作为二维输出的行,则需要扩展它们的维数。
As Jorge's answer points out, there is also the function stack
, introduced in numpy 1.10:
正如 Jorge 的回答所指出的,还有stack
numpy 1.10 中引入的函数:
numpy.stack( LIST, axis=0 )
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.
这采用互补的方法:它为每个输入数组创建一个新视图,并在连接之前添加一个额外的维度(在这种情况下,在左侧,因此每个n
元素一维数组变为 1× n
2D 数组)。只有当所有输入数组都具有相同的形状时,它才会起作用——即使沿着串联轴也是如此。
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:
vstack
(或等价物row_stack
)通常是一种更易于使用的解决方案,因为它将采用一维和/或二维数组的序列,并在必要时且仅在必要时自动扩展维数,然后将整个列表连接在一起。如果需要新尺寸,则将其添加到左侧。同样,您可以一次连接整个列表而无需迭代:
numpy.vstack( 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
.
这种灵活的行为也体现在语法快捷方式上numpy.r_[ array1, ...., arrayN ]
(注意方括号)。这对于连接几个显式命名的数组很有用,但对您的情况不利,因为此语法不接受数组序列,例如您的LIST
.
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).
还有一个类似的函数column_stack
和快捷方式c_[...]
,用于水平(按列)堆叠,以及一个几乎类似的函数hstack
——尽管由于某种原因后者不太灵活(它对输入数组的维数更严格,并试图连接一维数组端到端而不是将它们视为列)。
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.
...因为数组可以由其他数组的序列构造而成,在开头添加一个新维度。
回答by Jorge E. Cardona
Starting in NumPy version 1.10, we have the method stack. It can stack arrays of any dimension (all equal):
从 NumPy 1.10 版开始,我们有了方法stack。它可以堆叠任何维度的数组(全部相等):
# 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,
享受,