【问题标题】:Broadcasting of array [duplicate]数组广播[重复]
【发布时间】:2021-07-23 14:45:44
【问题描述】:

我试图将两个 Numpy 数组相乘,但我收到了与广播相关的错误-

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-124-6e9219ae5e45> in <module>
----> 1 d=B*C.reshape(-1,1)

ValueError: operands could not be broadcast together with shapes (3,5,7) (3,1) 

我的数组是-

B=np.random.rand(5,7,3).T.swapaxes(2,1)
C=np.linspace(1,5,3)
d=B*C.reshape(-1,1)

您能否解释一下这是什么错误以及我应该做什么? 谢谢

【问题讨论】:

    标签: python numpy array-broadcasting


    【解决方案1】:

    您可以尝试将C 数组的形状从(3,1) 更改为(3, 1, 1)

    B=np.random.rand(5,7,3).T.swapaxes(2,1)
    C=np.linspace(1,5,3)
    d=B*C.reshape(-1,1, 1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 2012-06-26
      相关资源
      最近更新 更多