简体   繁体   English

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

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

I'm working with some position vectors. 我正在处理一些位置向量。 I am operating each position with each other position and am using matrices to do it as efficiently as I can. 我正在彼此操作每个位置,并且正在使用矩阵尽可能高效地执行操作。 I encountered a problem with my most recent version where it gives me a warning: 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))了警告,它给了我一个警告: RuntimeWarning: invalid value encountered in sqrt return sqrt(add.reduce(s, axis=axis, keepdims=keepdims))

An example of some code that gives me this warning is below. 下面是给出此警告的一些代码示例。

This warning is caused by np.linalg.norm and only happens when I specify a data type for the array, it also only happens in the example code below when I have more than 90 vectors. 此警告是由np.linalg.norm引起的,仅当我为数组指定数据类型时才会发生,并且仅在以下示例代码中有90个以上的向量时才会发生。

Is this a NumPy bug, a known limitation in NumPy, or am I doing something wrong? 这是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.

You should make sure that Z doesn't contain any negative value! 您应确保Z不包含任何负值! test if you have negative values: 测试您是否具有负值:

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

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

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