8000 Why does indexing an array after boolean comparison provide different results than doing a boolean comparison of an index? · Issue #10982 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Why does indexing an array after boolean comparison provide different results than doing a boolean comparison of an index? #10982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
grantmwilliams opened this issue Apr 26, 2018 · 5 comments
Labels
33 - Question Question about NumPy usage or development

Comments

@grantmwilliams
Copy link

So for context this question comes from this post on stackoverflow: https://stackoverflow.com/questions/50047174/array-comparison-not-matching-elementwise-comparison-in-numpy#50047327

I have a numpy array arr. It's a numpy.ndarray, size is (5553110,), dtype=float32.

When I do:

(arr > np.pi )[3154950]
False
(arr[3154950] > np.pi )
True

Why is the first comparison getting it wrong? And how can I fix it?

The values:

arr[3154950]= 3.1415927
np.pi= 3.141592653589793

Is the problem with precision?

Would someone be able to explain the difference in the way numpy approaches the problem? It looks like using np.float64 explicitly fixes the problem, but I'm curious to know if numpy does a different casting in the two indexing methods.

@eric-wieser
Copy link
Member
eric-wieser commented Apr 26, 2018

Numpy has different promotion rules for scalars vs arrays, which I'd consider to be a bug (#10322)

In this case, arr is of dtype np.float32, and np.pi is of dtype np.float64.

Your first example is computing arr > np.float32(np.pi), whereas your second is computing np.float64(arr[3154950]) > np.pi

@grantmwilliams
Copy link
Author
grantmwilliams 8000 commented Apr 26, 2018

@eric-wieser ah perfect. That's exactly what I was looking for. So in the first example numpy treats the values of the arr as np.float32 to match np.pi correct?

@eric-wieser
Copy link
Member
eric-wieser commented Apr 26, 2018

You have that backwards. In the first example numpy throws away precision from np.pi first, casting it from float64 to float32

@grantmwilliams
Copy link
Author

Oh right that's totally my bad. Guess i was reading too fast

@bsipocz bsipocz added the 33 - Question Question about NumPy usage or development label May 21, 2022
@bsipocz
Copy link
Member
bsipocz commented May 21, 2022

This question has been answered, and links to relevant bug reports are made, so closing the issue. Please reopen if you think it tracks something distinct not duplicated in another issue.

700D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
33 - Question Question about NumPy usage or development
Projects
None yet
Development

No branches or pull requests

3 participants
0