将 numpy 数组除以 numpy 标量

Dividing numpy array by numpy scalar

我正在尝试将 numpy 数组除以 numpy float64 类型标量。以下是我的代码。

pose_q = np.array(pose_q)
expectecd_q = np.array(expectecd_q)

pose_q = np.squeeze(pose_q)
expectecd_q = np.squeeze(expectecd_q)

q1 = expectecd_q / np.linalg.norm(expectecd_q)
q2 = pose_q / np.linalg.norm(pose_q)

d = abs(np.sum(np.multiply(q1, q2)))

但是我收到以下指向 expectecd_q / np.linalg.norm(expectecd_q)

的错误
TypeError: ufunc 'true_divide' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

由于您没有提供数据,我将两个数组创建为:

a = np.array([12.0, 15.2, 19.3])  # Dividend
b = np.array(3.0)                 # Divider (a Numpy scalar)

如果你想将 a 除以 b 运行(毫不奇怪)a / b。结果是:

array([4.        , 5.06666667, 6.43333333])

在你的情况下,也许你应该确定你有哪些特定的价值观 作为操作数。

我在网上查找了您的错误消息。我发现了一个建议 原因可能是所讨论的数组具有 text 值(而不是 float)。 当您从数据库中读取数组时可能会发生这种情况。 检查此数组的 dtypeclass 'numpy.ndarray' 只是说 这是一个 Numpy 数组。但是它的元素的类型是什么?