The Numpy.where function does not find the value inside the array ... Does anyone know why?

I am trying to use the python numpy.where function to locate a specific value, but for some reason it incorrectly defines False where the value is really found. This returns an empty array. See below:

 >>>lbpoly=numpy.array([ 5.45 5.5 5.55 5.6 5.65 5.7 5.75 5.8 5.85 5.9 5.95 6. 6.05 6.1 6.15 6.2 6.25 6.3 6.35 6.4 6.45 6.5 6.55 6.6 6.65 6.7 6.75 6.8 6.85 6.9 6.95 7. ]) >>>cpah=numpy.where(lbpoly==6.2) >>>print cpah >>>(array([], dtype=int32),) 

Does anyone know why this is happening? I tried many different options, even using the < and > logic. But it gives indexes for 2 values.

+7
python arrays numpy where
source share
1 answer

There is very little point in comparing floating point numbers; in particular, you cannot learn by watching the printed representation of floating point numbers. (Read more here .)

Try something like this:

 import numpy as np lbpoly= np.array([4.0, 6.2]) >>> np.where(np.isclose(lbpoly, 6.2)) (array([1]),) 
+6
source share

All Articles