histogram with Freedman Diaconis estimator crashes on small numbers
See original GitHub issuenp.histogram
crashes with MemoryError
when the input contains very small numbers.
In [13]: print(sys.version)
2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2]
In [14]: print(np.__version__)
1.11.2
In [15]: x = np.array([ 2, 2, 2 - 1e-15, 2 - 1e-15, 1], dtype=np.float64)
In [16]: np.histogram(x, bins='fd')
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
<ipython-input-16-4ee7362b8a29> in <module>()
----> 1 np.histogram(x, bins='fd')
/home/artem/.local/lib/python2.7/site-packages/numpy/lib/function_base.pyc in histogram(a, bins, range, normed, weights, density)
562
563 # Initialize empty histogram
--> 564 n = np.zeros(bins, ntype)
565 # Pre-compute histogram scaling factor
566 norm = bins / (mx - mn)
MemoryError:
I did some debugging and figured out that in numpy.lib.function_base._hist_bin_fd
returns bin width 1.29852472695e-15
def _hist_bin_fd(x): # x: array([ 2, 2, 2 - 1e-15, 2 - 1e-15, 1])
iqr = np.subtract(*np.percentile(x, [75, 25])) # iqr: 1.11022302463e-15
return 2.0 * iqr * x.size ** (-1.0 / 3.0) # returns 1.29852472695e-15
I think that in numpy/lib/function_base.py:533 it would be good to check that we get a reasonable number of bins (may be max size of the array) or at least show a warning.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:17 (11 by maintainers)
Top Results From Across the Web
Determining Histogram Bin Width using the Freedman ...
Using the Freedman-Diaconis Rule to determine optimal histogram bin width. ... n in the empirical dataset, and returns a bin width estimate.
Read more >Optimal number of bins in histogram by the Freedman ...
The function gives a formula for the number of bins. The relationship between number of bins and the width of bins will be...
Read more >Relationship Between Traffic Volume and Accident Frequency ...
The ideal number of bins for accidents in low, middle- and high-volume intersections calculated using the Freedman–Diaconis rule.
Read more >Re: root crashes when creating large histogram/histograms ...
That depends on what fraction of the bins are filled. But as you said you had a problem with integer-overflowing bin numbers anyway,...
Read more >10. Kernel density estimators of the cubic power of a Gaussian...
Two histograms are constructed using Freedman-Diaconis'and Hall- Marron's rules ... a small or high number of samples provides a poor density estimation.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Not in the general case, no. Imagine a dataset with two clumps of data separated by a huge gap for an example of why not. It may, however, be worth considering merging together empty bins. To remove what I imagine to be the most common cause for the issue you are describing.
And raise a warning about the data having too fine a structure or something.