简体   繁体   English

将2D numpy数组列表转换为一个3D numpy数组?

[英]Convert a list of 2D numpy arrays to one 3D numpy array?

I have a list of several hundred 10x10 arrays that I want to stack together into a single Nx10x10 array. 我有一个数百个10x10阵列的列表,我想将它们堆叠成一个Nx10x10阵列。 At first I tried a simple 起初我试过一个简单的

newarray = np.array(mylist)

But that returned with "ValueError: setting an array element with a sequence." 但是返回的是“ValueError:使用序列设置数组元素”。

Then I found the online documentation for dstack(), which looked perfect: "...This is a simple way to stack 2D arrays (images) into a single 3D array for processing." 然后我找到了dstack()的在线文档,它看起来很完美:“......这是将2D数组(图像)堆叠成单个3D数组进行处理的简单方法。” Which is exactly what I'm trying to do. 这正是我想要做的。 However, 然而,

newarray = np.dstack(mylist)

tells me "ValueError: array dimensions must agree except for d_0", which is odd because all my arrays are 10x10. 告诉我“ValueError:数组维度必须同意,除了d_0”,这是奇怪的,因为我的所有数组都是10x10。 I thought maybe the problem was that dstack() expects a tuple instead of a list, but 我想也许问题是dstack()期望一个元组而不是一个列表,但是

newarray = np.dstack(tuple(mylist))

produced the same result. 产生了同样的结果。

At this point I've spent about two hours searching here and elsewhere to find out what I'm doing wrong and/or how to go about this correctly. 在这一点上,我花了大约两个小时在这里和其他地方搜索,以找出我做错了什么和/或如何正确地解决这个问题。 I've even tried converting my list of arrays into a list of lists of lists and then back into a 3D array, but that didn't work either (I ended up with lists of lists of arrays, followed by the "setting array element as sequence" error again). 我甚至尝试将我的数组列表转换为列表列表然后再转换为3D数组,但这也不起作用(我最终得到了数组列表的列表,接着是“设置数组元素”作为序列“再次出错”。

Any help would be appreciated. 任何帮助,将不胜感激。

newarray = np.dstack(mylist)

should work. 应该管用。 For example: 例如:

import numpy as np

# Here is a list of five 10x10 arrays:
x = [np.random.random((10,10)) for _ in range(5)]

y = np.dstack(x)
print(y.shape)
# (10, 10, 5)

# To get the shape to be Nx10x10, you could  use rollaxis:
y = np.rollaxis(y,-1)
print(y.shape)
# (5, 10, 10)

np.dstack returns a new array. np.dstack返回一个新数组。 Thus, using np.dstack requires as much additional memory as the input arrays. 因此,使用np.dstack需要与输​​入数组一样多的额外内存。 If you are tight on memory, an alternative to np.dstack which requires less memory is to allocate space for the final array first , and then pour the input arrays into it one at a time. 如果内存紧张,则需要较少内存的np.dstack的替代方法是首先为最终数组分配空间,然后一次一个地将输入数组倒入其中。 For example, if you had 58 arrays of shape (159459, 2380), then you could use 例如,如果您有58个形状阵列(159459,2380),那么您可以使用

y = np.empty((159459, 2380, 58))
for i in range(58):
    # instantiate the input arrays one at a time
    x = np.random.random((159459, 2380))
    # copy x into y
    y[..., i] = x

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

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