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:
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)
If you are looking for a Normal distribution N(mu, sigma) you can calculate exactly mu and sigma from the input data.
For example: X = x1,...,xN are the values and W = w1,..., wN their weights
mu = sum (X * W) / sum(W)
sigma = np.sqrt (sum (W * (X- mu)**2) / sum(W))
If you are to fit another kind of distribution, I suggested an answer here using OpenTURNS library.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.