问题
I know how to fit the data entering an histogram with a normal distribution using the SCipy library (Fitting a histogram with python) but how could I do the same if on top of having data I have an array of weights having the same dimension. Is there a proper function for that or should I create a second array fed by the data and weighting it myself?
Cheers.
Edit:
This is pretty much already answered here:
Weighted standard deviation in NumPy?
回答1:
just use the weights paramater on scipy.histogram and pass in your array of weights:
scipy.stats.histogram(a, numbins=10, defaultlimits=None, weights=None, printextras=False)
from the docs:
weights : array_like, optional The weights for each value in a. Default is None, which gives each value a weight of 1.0
NOTE: as of v1.0, scipy does not have the function histogram, but as of v1.11 histogram appears in numpy, with a similar (but not identical) call signature that includes the weights= argument:
numpy.histogram(a, bins=10, range=None, normed=False, weights=None, density=None)
来源:https://stackoverflow.com/questions/21063935/fitting-a-weighted-histogram-with-a-normal-distribution