question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

weights argument seems to be ignored in np.histogram

See original GitHub issue
>>> import numpy as np
>>> a=np.array([1,2,3,4,5,6,7,8,9,10])
>>> weights=np.array([0.8,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1])
>>> hist, bin_edges = np.histogram(a,bins=10, weights=weights)
>>> print(bin_edges)
[  1.    1.9   2.8   3.7   4.6   5.5   6.4   7.3   8.2   9.1  10. ]
>>> hist, bin_edges = np.histogram(a,bins=10)
>>> print(bin_edges)
[  1.    1.9   2.8   3.7   4.6   5.5   6.4   7.3   8.2   9.1  10. ]

The bin_edges calculated with and without weights are the same. It seems the weights are ignored in the bin_edges calculation.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nayyarvcommented, Mar 18, 2018

Hmm, when you pass in 10 bins, the bin edges are literally calculated by np.linspace(min(x), max(x), n_bins+1). This will never change depending on weights, since this does not change the range nor the number of bins.

What does change is the value (i.e. height) of each bin. That is affected by weights.

In [4]: >>> import numpy as np
   ...: >>> a=np.array([1,2,3,4,5,6,7,8,9,10])
   ...: >>> weights=np.array([0.8,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1])
   ...: >>> hist, bin_edges = np.histogram(a,bins=10, weights=weights)
   ...: >>> print(hist)
[0.8 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
   ...: >>> hist, bin_edges = np.histogram(a,bins=10)
   ...: >>> print(hist)
[1 1 1 1 1 1 1 1 1 1]

And if you were to try the same with an automated method, you get a ValueError since the weights would change the estimate.

In [5]:  >>> hist, bin_edges = np.histogram(a,bins='auto', weights=weights)
TypeError                                 Traceback (most recent call last)
----> 1 hist, bin_edges = np.histogram(a,bins='auto', weights=weights)

/usr/local/lib/python2.7/site-packages/numpy/lib/function_base.pyc in histogram(a, bins, range, normed, weights, density)
    689                 "{!r} is not a valid estimator for `bins`".format(bin_name))
    690         if weights is not None:
--> 691             raise TypeError("Automated estimation of the number of "
    692                             "bins is not supported for weighted data")
    693         # Make a reference to `a`

TypeError: Automated estimation of the number of bins is not supported for weighted data

This is not a bug, this is a misunderstanding of how weights and bin edges interact.

0reactions
domagalskicommented, Mar 18, 2018

I agree. It can be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plotting histrogram with weighted bell curve - Stack Overflow
I'm plotting a histogram with a bell curve and I'm running into ... the same argument down in stats.norm.fit seems to be ignored...
Read more >
numpy.histogram — NumPy v1.12 Manual
An array of weights, of the same shape as a. Each value in a only contributes its associated weight towards the bin count...
Read more >
matplotlib.pyplot.hist — Matplotlib 3.1.2 documentation
See the documentation of the weights parameter to draw a histogram of already-binned data. Multiple data can be provided via x as a...
Read more >
numpy.histogram — NumPy v1.24 Manual
An array of weights, of the same shape as a. Each value in a only contributes its associated weight towards the bin count...
Read more >
Is there a hisgram algo func in rust? - help
i know numpy got a funcion which named "histgram" Signature: np.histogram( a, bins=10, range=None, normed=None, weights=None, density=None, ) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found