从现有的2d数组以numpy构造3d数组 [英] Contruct 3d array in numpy from existing 2d array

查看:55
本文介绍了从现有的2d数组以numpy构造3d数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在准备用于NumPy计算的数据时.我对构造方法感到好奇:

During preparing data for NumPy calculate. I am curious about way to construct:

myarray.shape => (2,18,18)

来自:

d1.shape => (18,18)
d2.shape => (18,18)

我尝试使用NumPy命令:

I try to use NumPy command:

hstack([[d1],[d2]])

但它似乎不起作用!

推荐答案

hstack和vstack不会更改数组的维数:它们只是将它们并排"放置.因此,组合二维数组会创建一个新的二维数组(不是3D数组!).

hstack and vstack do no change the number of dimensions of the arrays: they merely put them "side by side". Thus, combining 2-dimensional arrays creates a new 2-dimensional array (not a 3D one!).

您可以执行丹尼尔(Daniel)建议的操作(直接使用numpy.array([d1, d2])).

You can do what Daniel suggested (directly use numpy.array([d1, d2])).

通过在每个数组上添加新尺寸,您还可以在将数组堆叠之前将其转换为3D数组:

You can alternatively convert your arrays to 3D arrays before stacking them, by adding a new dimension to each array:

d3 = numpy.vstack([ d1[newaxis,...], d2[newaxis,...] ])  # shape = (2, 18, 18)

实际上是d1[newaxis,...].shape == (1, 18, 18),您可以直接堆叠两个3D阵列并获得所需的新3D阵列(d3).

In fact, d1[newaxis,...].shape == (1, 18, 18), and you can stack both 3D arrays directly and get the new 3D array (d3) that you wanted.

这篇关于从现有的2d数组以numpy构造3d数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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