【问题标题】:how to merge two array into one in python and numpy?如何在python和numpy中将两个数组合并为一个?
【发布时间】:2018-08-27 06:37:36
【问题描述】:

我想将两个数组(a,b)(每个shape = (30,192,192,1))合并为一个(输入)像这样shape = (30,192,192,2)
有没有人可以帮助我?

我的代码如下:

input = np.ndarray((a.shape[0], a.shape[1], a.shape[2],2))
input[:,:,:,0] = a
input[:,:,:,1] = b

但我收到此错误:

input[:,:,:,0] = a
ValueError: could not broadcast input array from shape (30,192,192,1) into shape (30,192,192)

【问题讨论】:

  • 试试input[:,:,:,[0]] = a,或input[:,:,:,0]=a[:,:,:,0]。即匹配形状。 numpy广播可以在开头添加维度,而不是结尾。
  • 非常感谢!效果很好!!!

标签: python numpy keras


【解决方案1】:

您可以在之前重塑 a 和 b 数组:

a=a.reshape((30,192,192))
input[:,:,:,0] = a

【讨论】:

  • 感谢您的 cmets!
猜你喜欢
  • 1970-01-01
  • 2020-09-07
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多