9

I have been trying to use python's numpy.where function to determine the location of a specific value, but for some reason it incorrectly determines False where the value is actually found. Thereby returning 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've tried many different variants, even using < and > logic. But this produces indices for 2 values.

3
  • is your lbpoly a numpy-array? Looks like a list. Commented Mar 6, 2016 at 17:47
  • A list displays with commas, numpy arrays without (usually). Commented Mar 6, 2016 at 18:11
  • @dede Yes, it's an array. In my haste I incorrectly placed it here to look like a list. Commented Mar 6, 2016 at 19:39

1 Answer 1

10

There is very little meaning in comparing floating point numbers; in particular, there is a lot you cannot learn by observing the printed representation of floating point numbers. (See 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]),)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.