如何在循环中使用numpy.dstack? [英] How to use numpy.dstack in a loop?

查看:181
本文介绍了如何在循环中使用numpy.dstack?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过将2D数组与np.dstack一起使用来填充数组.

I'm trying to populate an array by using 2D arrays with np.dstack.

m1 = np.array([[1,1],[1,1]])
m2 = np.array([[2,2],[2,2]])
m3 = np.array([[3,3],[3,3]])

lst = m1
lst = np.dstack((lst,m2))
lst = np.dstack((lst,m3))

循环执行的正确方法是什么?我正在寻找类似的东西

What's the proper way to do it in a loop? I'm looking for something like

lst = np.empty(...)
for _
    lst = np.dstack((lst,variable2Darray))

推荐答案

如其他答案所示,您可以将数组放入列表中,并通过一次调用将它们加入新的数组中:

As other answers have shown, you can put the arrays in a list, and join them into a new array with one call:

In [173]: m1 = np.array([[1,1],[1,1]])
     ...: m2 = np.array([[2,2],[2,2]])
     ...: m3 = np.array([[3,3],[3,3]])
     ...: 
     ...: 
In [174]: alist = [m1,m2,m3]
In [175]: np.stack(alist)
Out[175]: 
array([[[1, 1],
        [1, 1]],

       [[2, 2],
        [2, 2]],

       [[3, 3],
        [3, 3]]])

在numpy中,第一个轴是最外侧.相反,在MATLAB中,最后一个轴位于最外面.因此stack(或np.array(alist))是自然的等价物.但是,如果必须将它们连接到新的内轴上,请使用:

In numpy the first axis is the outer most. In contrast in MATLAB the last axis is outermost. So stack (or np.array(alist)) is the natural equivalent. But if you must join them on a new inner axis use:

In [176]: np.stack(alist,axis=2)        # or np.dstack(alist)
Out[176]: 
array([[[1, 2, 3],
        [1, 2, 3]],

       [[1, 2, 3],
        [1, 2, 3]]])

或者如果您一次创建一个数组,请使用列表追加:

Or if you are creating the arrays one at a time, use a list append:

In [177]: alist = []
In [178]: for a in m1,m2,m3:
     ...:     alist.append(a)

也许最接近您的MATLAB的是:

Perhaps the closest thing to your MATLAB is:

In [181]: arr = np.zeros((2,2,3),int)
In [183]: for i,m in enumerate([m1,m2,m3]):
     ...:     arr[:,:,i] = m

for i=1:10 
    var = some2Dmatrix 
    A(:,:,i) = var;
end

与MATLAB相反,当索引超出当前大小时,numpy不会简单地添加图层.我还怀疑MATLAB代码实际上隐藏了一些昂贵的矩阵大小调整代码.这不是几年前我专业使用MATLAB时所做的.

In contrast to MATLAB numpy does not simply add a layer when you index beyond the current size. I also suspect that MATLAB code actually hides some costly matrix resizing code. It's not something I did years ago when I used MATLAB professionally.

但是要回答我们不想回答的问题:

But to answer the question that we don't want to answer:

In [185]: arr = np.zeros((2,2,0),int)   # your empty(?)
In [187]: for m in (m1,m2,m3):
     ...:     arr = np.concatenate((arr, m[:,:,None]), axis=2)    
In [188]: arr.shape
Out[188]: (2, 2, 3)

如果您不了解此迭代的详细信息,则可能不应该尝试使用重复的concatenate(或dstack)构建更大的数组.您必须正确设置arr的初始形状,并且必须正确获取迭代数组的正确形状.另外,这使每个循环都产生了全新的arr.这比列表追加要昂贵.

If you don't understand the details of this iteration, you probably shouldn't be trying to build a larger array with repeated concatenate (or dstack). You have to get the initial shape of arr correct, and you have to get the right shape of the iteration array right. Plus this is making brand new arr with each loop. That's costlier than list append.

这篇关于如何在循环中使用numpy.dstack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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