Numpy:最后一维的堆栈数组 [英] Numpy: stack array by the last dimension

查看:51
本文介绍了Numpy:最后一维的堆栈数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有 3 个相同形状的 numpy 数组 abc,比如

Suppose I have 3 numpy arrays a, b, c, of the same shape, say

a.shape == b.shape == c.shape == (7,9)

现在我想创建一个大小为 (7,9,3) 的 3 维数组,比如 x,这样

Now I'd like to create a 3-dimensional array of size (7,9,3), say x, such that

x[:,:,0] == a
x[:,:,1] == b
x[:,:,2] == c

做这件事的pythonic"方式是什么(也许在一行中)?

What is the "pythonic" way of doing it (perhaps in one line)?

提前致谢!

推荐答案

有一个函数可以做到这一点:numpy.dstack(d"代表深度").例如:

There's a function that does exactly that: numpy.dstack ("d" for "depth"). For example:

In [10]: import numpy as np

In [11]: a = np.ones((7, 9))

In [12]: b = a * 2

In [13]: c = a * 3

In [15]: x = np.dstack((a, b, c))

In [16]: x.shape
Out[16]: (7, 9, 3)

In [17]: (x[:, :, 0] == a).all()
Out[17]: True

In [18]: (x[:, :, 1] == b).all()
Out[18]: True

In [19]: (x[:, :, 2] == c).all()
Out[19]: True

这篇关于Numpy:最后一维的堆栈数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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