【问题标题】:How to stack 3d array of shape(100,600,600) into 2d array of (600,600)?如何将形状(100,600,600)的 3d 数组堆叠成(600,600)的 2d 数组?
【发布时间】:2021-07-12 14:11:54
【问题描述】:

所以我正在生成一个像素数 = 600 的地图。所以我有一个形状数组(600,600)。现在我需要生成 100 个随机地图,我创建并制作了一个形状数组(100,600,600)。在这个数组中,我有 100 张地图,但我需要堆叠或重叠到一张地图中。我怎样才能做到这一点?我正在使用 numpy 库

【问题讨论】:

  • 对不起,问题不清楚。请您稍微编辑一下,让它更全面一点。

标签: python arrays numpy numpy-ndarray


【解决方案1】:

不知道你的问题是否正确,这是我的尝试

import numpy as np


a = np.random.randint(10,size=(600,600)) # random 600X600 array

print('a shape & size : ',a.shape, a.size)

b = np.random.randint(10,size=(600,600)) # random 600X600 array

c = np.random.randint(10,size=(600,600)) # random 600X600 array

print('b shape & size : ',b.shape, b.size)
print('c shape & size : ',c.shape, c.size)

x = np.stack([a,b,c], axis = 2)  # numpy.stack() https://numpy.org/doc/stable/reference/generated/numpy.vstack.html

print('a+b+c axis 2 stack  shape & size : ',x.shape, x.size) # stacked on new axes = 2 (axes start at 0), 600X600X3 array

输出:

a shape & size :  (600, 600) 360000
b shape & size :  (600, 600) 360000
c shape & size :  (600, 600) 360000
a+b+c axis 2 stack  shape & size :  (600, 600, 3) 1080000

【讨论】:

    猜你喜欢
    • 2019-09-27
    • 2022-01-20
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多