8000 numpy.histogram(a, bins) behaves differently from numpy.histogram(a, bins, range=(a.min(), a.max())) · Issue #11780 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

numpy.histogram(a, bins) behaves differently from numpy.histogram(a, bins, range=(a.min(), a.max())) #11780

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
puntofisso opened this issue Aug 18, 2018 · 8 comments

Comments

@puntofisso
Copy link

As reported in gboeing/osmnx#196, while running OSMnx's code, which directly references numpy.histogram().

It's tricky to reproduce without fully using the jupyter notebook in the example, but I'll do my best to summarise and I'm happy to test things and report back if required. I'm sorry if this issue is filed in a substandard way.

Reproducing code example:

While running the jupyter notebook above, which aims to calculate a histogram over a set of bearings extracted from Open Street Map, I have the following behaviour.

This works:
np.histogram(bearings, bins=bins, range=(bearings.min(), bearings.max()))
but this doesn't
np.histogram(bearings, bins=bins).

My understanding from the documentation is that the two calls should work exactly the same, so I'm puzzled.

Error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-c59ecd43a7f0> in <module>()
      9 # plot each city's polar histogram
     10 for ax, place in zip(axes, sorted(places.keys())):
---> 11     polar_plot(ax, bearings[place], title=place)
     12 
     13 # add super title and save full image

<ipython-input-5-3f02bf8f385b> in polar_plot(ax, bearings, n, title)
      3 
      4     bins = [ang * 360 / n for ang in range(0, n + 1)]
----> 5     count = count_and_merge(n, bearings)
      6     _, division = np.histogram(bearings, bins=bins)
      7     frequency = count / count.sum()

<ipython-input-6-c7da03125f04> in count_and_merge(n, bearings)
      4     n = n * 2
      5     bins = [ang * 360 / n for ang in range(0, n + 1)]
----> 6     count, _ = np.histogram(bearings, bins=bins)
      7 
      8     # move the last bin to the front, so eg 0.01° and 359.99° will be binned together

~/anaconda3/envs/OSMNX/lib/python3.6/site-packages/numpy/lib/function_base.py in histogram(a, bins, range, normed, weights, density)
    668     if not np.all(np.isfinite([first_edge, last_edge])):
    669         raise ValueError(
--> 670             'range parameter must be finite.')
    671     if first_edge == last_edge:
    672         first_edge -= 0.5

ValueError: range parameter must be finite.

Numpy/Python version information:

!pip list returns

numpy 1.14.5

conda list returns

numpy 1.14.5
numpy 1.13.1 py36_0

'import sys, numpy; print(numpy.version, sys.version)' returns

1.14.5 3.6.5 | packaged by conda-forge | (default, Apr 6 2018, 13:44:09)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]

@eric-wieser
Copy link
Member
eric-wieser commented Aug 19, 2018

Can you show the values of bearings so we can reproduce this?

I suspect this is fixed in 1.15.0 - so alternatively, can you try reproducing on the latest version?

@puntofisso
Copy link
Author

It's 32K lines, so I hope the attached zipped pickle helps. In order to replicate, once you unzip the file, here's the code:

import pickle

bins=[0.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 110.0, 120.0, 130.0, 140.0, 150.0, 160.0, 170.0, 180.0, 190.0, 200.0, 210.0, 220.0, 230.0, 240.0, 250.0, 260.0, 270.0, 280.0, 290.0, 300.0, 310.0, 320.0, 330.0, 340.0, 350.0, 360.0]

outFile = open('output.pickle','rb')
a = pickle.load(outFile)

np.histogram(a, bins=bins, range=(b.min(), b.max())) # THIS WORKS
#np.histogram(a, bins=bins) # THIS WILL RETURN ERROR 'ValueError: range parameter must be finite.'

output.pickle.zip

@mattip
Copy link
Member
mattip commented Aug 19, 2018

What is b? Anyhow, even without it, can confirm your data (a pandas Series) raises an exception in 1.14.5, passes in 1.15.0. Please reopen if needed.

@mattip mattip closed this as completed Aug 19, 2018
@puntofisso
Copy link
Author

Sorry, it was a.min(), a.max(), i.e.:

import pickle

bins=[0.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 
8000
90.0, 100.0, 110.0, 120.0, 130.0, 140.0, 150.0, 160.0, 170.0, 180.0, 190.0, 200.0, 210.0, 220.0, 230.0, 240.0, 250.0, 260.0, 270.0, 280.0, 290.0, 300.0, 310.0, 320.0, 330.0, 340.0, 350.0, 360.0]

outFile = open('output.pickle','rb')
a = pickle.load(outFile)

np.histogram(a, bins=bins, range=(a.min(), a.max())) # THIS WORKS
#np.histogram(a, bins=bins) # THIS WILL RETURN ERROR 'ValueError: range parameter must be finite.'

Great, then, will update to 1.15.0!

Out of curiosity, what is it that triggers the exception? I'm curious as the documented behaviour is that not providing an explicit range value for a should default to a.min(), a.max() - was there a bug in 1.14.5 that changed this behaviour?

@mattip
Copy link
Member
mattip commented Aug 19, 2018

Note that you may encounter #11628 "RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88" which will be fixed in 1.15.1.

I think the problem was the NAN in a

@puntofisso
Copy link
Author

Ah, thanks for flagging that up!

On the NAN in a: if that is the problem, how come specifying the range explicitly does work? Shouldn't it fail too? (At least if the docs are correct?)

@mattip
Copy link
Member
mattip commented Aug 19, 2018

There was a bug, perhaps #7503 is relevant.

@puntofisso
Copy link
Author
puntofisso commented Aug 19, 2018

Thanks @mattip - I'll report back to gboeing/osmnx#196.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
0