Numpy reshape() 以编程方式以 3D 形式显示 2D 数组

问题描述 投票:0回答:1

Example Data

我有一系列经纬度的天气数据,形状如下:(1038240,4)(有关示例数据,请参阅照片)

我想将其重塑为形状 (4,721,1440),这将是 721 x 1440 地球图像上的四个天气变量(和纬度/经度)。

我已经尝试过:

newarr = t_new.reshape(4,721,1440) 

这会将其置于正确的形状,但与前两个纬度/经度坐标与以下首选格式不匹配:

对于上图中的 (6,4) 示例数据,此操作看起来像下面的 (2,3,2) 数组:

Example Desired Output

newarr = t_new.reshape(4,721,1440) 
numpy machine-learning reshape
1个回答
0
投票

进一步调查表明,

numpy.reshape()
默认按行优先(C 风格)顺序运行,这意味着它首先沿最后一个轴填充新数组(即从左到右,从上到下)

所以如果我先重塑它然后转置:

correct_order = reshaped.transpose((2, 1, 0))  # Swap axes to get (4, 721, 1440)```

It seems to produce the desired output. I test this by looking at the slices to see that longitude/latitude are constant along each slice:

correct_order[:,:,1]
correct_order[:,1,:]

Thanks to comments from hpaulj on transposing this first
© www.soinside.com 2019 - 2024. All rights reserved.