简体   繁体   中英

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))

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.

Is this a NumPy bug, a known limitation in NumPy, or am I doing something wrong?

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! test if you have negative values:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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