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:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top 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 >
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 Free
Top 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
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.
And if you were to try the same with an automated method, you get a ValueError since the weights would change the estimate.
This is not a bug, this is a misunderstanding of how weights and bin edges interact.
I agree. It can be closed.