ValueError:在将数组添加到另一个数组时,无法将形状(28)中的输入数组广播为形状(28,0)

时间:2017-05-17 18:55:35

标签: python numpy

我试图将numpy数组添加到另一个numpy数组中,但我收到此错误:

ValueError: could not broadcast input array from shape (28) into shape (28,0)

这是我的代码:

sample = np.fabs(sample - avg)
counter = np.arange(1,len(sample)+1)
np.append(sample, counter, axis=1)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这表明形状为(28,0)的数组实际上是空的,这意味着您可能需要解决生成sample和avg的上游处理,并验证这些对象的内容。我可以用以下内容复制这个:

import numpy as np
from numpy import random
a = random.rand(28)
b = random.random((28,0))
print(a.shape, b.shape)

(28,)(28,0)

print(a + b)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-f1c1de818ef8> in <module>()
      5 print(a.shape, b.shape)
      6 
----> 7 print(a + b)
      8 
      9 print(b)

ValueError: operands could not be broadcast together with shapes (28,) (28,0)

print(b)

[]