【问题标题】:numpy ValueError: operands could not be broadcast together with shapesnumpy ValueError:操作数无法与形状一起广播
【发布时间】:2020-06-20 07:51:38
【问题描述】:
thetaU = np.linalg.inv(np.linalg.inv(theta) + theta2_input**(-1)*np.transpose(X_test[i]) * X_test[i])

当我在我的一个函数中执行以下操作时.. 我收到以下错误:

ValueError:  operands could not be broadcast together with shapes (2,100) (100,2)

我对 Python 有点陌生,如果有任何帮助,我将不胜感激。谢谢。

【问题讨论】:

  • 确定操作以及导致问题的阵列。哪些数组有这两种形状?这是你所期望的吗?也许您不需要transpose?或者,也许您使用了错误的乘法。我们不能仅仅通过阅读您的代码来判断。

标签: python-3.x numpy


【解决方案1】:

在 NumPy 中,运算符 * 表示矩阵乘法。它按元素将两个数组相乘。将其替换为np.matmul()np.dot()

np.matmul(np.transpose(X_test[i]), X_test[i]))

【讨论】:

    猜你喜欢
    • 2012-08-05
    • 2014-08-24
    • 1970-01-01
    • 2017-09-09
    • 2012-10-31
    • 2013-04-07
    • 2018-08-18
    • 1970-01-01
    • 2018-12-26
    相关资源
    最近更新 更多