【问题标题】:How to combine two arrays of different dimension in Python?如何在 Python 中组合两个不同维度的数组?
【发布时间】:2022-12-13 00:54:58
【问题描述】:

我有两个数组,它们是 (150,1400,1200) 和 (150,1400)。我想知道如何在 Python 中按行和列组合这两个数组?

【问题讨论】:

标签: python arrays numpy multidimensional-array numpy-ndarray


【解决方案1】:

IIUC,您需要向b 添加一维,然后使用numpy.concatenate

a = np.random.rand(150, 1400, 1200)
print('a.shape:',a.shape)
b = np.random.rand(150, 1400)
print('b.shape:',b.shape)
print('b[..., None].shape:',b[..., None].shape)

c = np.concatenate([a, b[..., None]], axis=2)
print('c.shape:',c.shape)

输出:

a.shape: (150, 1400, 1200)
b.shape: (150, 1400)
b[..., None].shape: (150, 1400, 1)
c.shape: (150, 1400, 1201)

【讨论】:

    【解决方案2】:

    嘿,你可以看看下面的链接来解决你的问题

    Concat two arrays of different dimensions numpy

    【讨论】:

      猜你喜欢
      • 2021-12-18
      • 2019-09-19
      • 1970-01-01
      • 2019-02-26
      • 2012-09-02
      • 1970-01-01
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多