Skip to content

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

Closed

Description

@grantmwilliams

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.

Activity

  1. eric-wieser commented on Apr 26, 2018

    @eric-wieser
    Member

    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

  2. grantmwilliams commented on Apr 26, 2018

    @grantmwilliams
    Author

    @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?

  3. eric-wieser commented on Apr 26, 2018

    @eric-wieser
    Member

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

  4. grantmwilliams commented on Apr 26, 2018

    @grantmwilliams
    Author

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

  5. bsipocz commented on May 21, 2022

    @bsipocz
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    33 - QuestionQuestion about NumPy usage or development

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions