【问题标题】:ValueError: zero-dimensional arrays cannot be concatenated when arrays are not zero-dimensional and inside bracket/parenthesisValueError:当数组不是零维且在括号/括号内时,不能连接零维数组
【发布时间】:2020-04-14 18:35:37
【问题描述】:
        print(foo.shape)  # prints(1,6,288,512)

        for i in range(2):
            print(foo[:,i].shape,foo[:,i+2].shape, foo[:,i+4].shape)
            # prints (1, 288, 512) for all three

            fi = np.concatenate([foo[:,i],foo[:,i+2], foo[:,i+4]],axis=0)
            #expecting fi to be a (3,288,512) array

最后一行返回

ValueError: zero-dimensional arrays cannot be concatenated

数组不是零维的,我已经在要连接的数组周围加上括号/括号,所以我不确定它是如何实现的。 Numpy 版本是 1.18.2,带有 python 3.6。

----------------添加------------ 当我堆叠它时,它不会返回错误,但结果数组的形状很奇怪。

fi = np.stack([foo[:,i],foo[:,i+2], foo[:,i+4]],axis=0)
print(fi.shape)
# returns (3,)

【问题讨论】:

  • 如果您发现错误 - 说明,然后删除问题。我们不需要浪费时间了。

标签: python python-3.x numpy


【解决方案1】:

数组应该作为元组提供给np.concatenate

np.concatenate((foo[:,i],foo[:,i+2], foo[:,i+4]),axis=0)

应该可以。


更具体地说,以下 MWE 对我有用:

foo = np.ones((1, 6, 288, 512))

bar = np.concatenate((foo[:, 0], foo[:, 2], foo[:, 4]), axis=0)  # no exception raised
print(bar.shape) # prints (3, 288, 512)

【讨论】:

  • 这很奇怪。查看我的更新 --- 对我来说它有效!
  • 这很奇怪..我添加了其他信息以防万一
  • 您是否尝试过将foo 的实际内容替换为所有ones 之类的内容?
  • 用有效的替换它。是不是说明数组里面的数据有问题?
  • 我发现了问题! foo 是一个张量 NdArray 变量,所以我必须使用 foo.data 访问它的数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
相关资源
最近更新 更多