0

Can someone give me a hint if I have an precission issue with floating points as stated here: Hist Precission with floats or do I miss something here??

import numpy
import matplotlib.pyplot as plt    

frame = numpy.array([0.9, 0.99, 1, 1.9, 1.99, 2])

MyBins = numpy.linspace(0, 100, 1000, endpoint=False) 
(counts, bins, bars) = plt.hist(frame, bins = MyBins)

Output counts:
       0
...    ...
8      0.0
9      2.0
10     1.0
...    ...
18     1.0
19     1.0
20     1.0

The counts of 0.9 and 0.99 should be 2 as well as for 1.9 and 1.99. However, 1.9 is counted as an 1.8 value... If this an floating precission issue, what can I do to fix it?

Thank you!

1
  • MyBins mark the threshold between bins, maybe you want to shift them by 0.05 and do MyBins = numpy.linspace(-0.05, 100.05, 1001, endpoint=False) Commented Apr 13, 2022 at 10:58

1 Answer 1

1

If you print the numbers with high precision they turn out to be something like: '0.1000000000000000055511151' and '1.8999999999999999111821580'. So yes, floating point precision likely is the issue. Moving your bin boundaries might be a solution.

Normally floating point numbers with the same precision should compare equal and behave as expected though. If your values and the histogram use different precisions, you might get unexpected results like this. You might try to find out what precision the histogram uses and use the same for your values.

Example:

frame = numpy.array([0.9, 0.99, 1, 1.9, 1.99, 2], dtype=numpy.float32)
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! This solved my issue. I just hat do cast the frame AND the bins into float32 and now I get the correct results. Thank you a lot.

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.