繁体   English   中英

np.linalg.norm:“在sqrt中遇到无效的值”

[英]np.linalg.norm: “invalid value encountered in sqrt”

我正在处理一些位置向量。 我正在彼此操作每个位置,并且正在使用矩阵尽可能高效地执行操作。 我在最新版本中RuntimeWarning: invalid value encountered in sqrt return sqrt(add.reduce(s, axis=axis, keepdims=keepdims))了警告,它给了我一个警告: RuntimeWarning: invalid value encountered in sqrt return sqrt(add.reduce(s, axis=axis, keepdims=keepdims))

下面是给出此警告的一些代码示例。

此警告是由np.linalg.norm引起的,仅当我为数组指定数据类型时才会发生,并且仅在以下示例代码中有90个以上的向量时才会发生。

这是NumPy错误,还是NumPy中的已知限制,还是我做错了什么?

x = np.full((100, 3), 1)  # Create an array of vectors, in this case all [1, 1, 1]
ps, qs = np.broadcast_arrays(x, np.expand_dims(x, 1))  # Created so that I can operate each vector on each other vector.
z = np.subtract(ps, qs, dtype=np.float32)  # Get the difference between them.
np.linalg.norm(z, axis=2)  # Get the magnitude of the difference.

您应确保Z不包含任何负值! 测试您是否具有负值:

print len([_ for _ in z if _ < 0])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM