Reputation: 43
I am using the package fitter
to fit continuous distributions to numpy arrays of data in Python. fitter.fitted_param
returns a dictionary of tupples of varying length with the best-fit parameters for each distribution. These are the same parameters used to set these distributions in scipy.stats
. I would like to use these tuples to set the parameters directly with scipy.stats
, but I do not know how to do so. Any ideas?
Example:
>from fitter import Fitter
>import numpy as np
>data = np.random.random((1000,1))
>f = Fitter(data,distributions = ['norm','gamma'])
>f.fit()
>param = f.fitted_param['gamma']
>param
out:(20759.430545279687, -41.012521759919224, 0.0019996776498165851)
If I now want to create a new gamma distribution to generate gamma-distributed random values with the same a
,loc
and scale
specified in param
above, I need to write:
>from scipy import stats
>rv = stats.gamma(a=param[0],loc=param[1],scale=param[2])
>rv.rvs(100)
How can I set a
,loc
,andscale
directly without typing them out explicitly? I want to be able to quickly fit any distribution with an arbitrary number of parameters and generate random values that follow that distribution.
Thanks for your help!
Upvotes: 0
Views: 1377
Reputation: 7293
You can do <distribution>(*param)
, provided that the list of parameters param
is in the right order.
If it is not, you have to have your params in a dictionary and call it as keyword arguments with <distribution>(**param)
, for instance:
param = {"a": 20759, "scale": 0.001, "loc": -41}
stats.gamma(**param)
Edit: more information here: *args and **kwargs?
Upvotes: 2