简体   繁体   English

如何使用列表设置scipy.stats分发的参数

[英]How to set parameters for scipy.stats distribution with a list

I am using the package fitter to fit continuous distributions to numpy arrays of data in Python. 我正在使用包fitter来使连续分布适合Python中的numpy数据数组。 fitter.fitted_param returns a dictionary of tupples of varying length with the best-fit parameters for each distribution. fitter.fitted_param返回具有不同长度的tupple的字典,其中包含每个分布的最佳拟合参数。 These are the same parameters used to set these distributions in scipy.stats . 这些参数与用于在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. 我想使用这些元组直接通过scipy.stats设置参数,但是我不知道该怎么做。 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: 如果我现在要创建一个新的伽玛分布产生伽玛分布的随机值与同alocscale在指定param上面,我需要写:

>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 ,and scale directly without typing them out explicitly? 如何设置aloc ,以及scale的情况下直接键入出来明确? 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! 谢谢你的帮助!

You can do <distribution>(*param) , provided that the list of parameters param is in the right order. 您可以执行<distribution>(*param) ,只要参数param列表的顺序正确。

If it is not, you have to have your params in a dictionary and call it as keyword arguments with <distribution>(**param) , for instance: 如果不是,则必须在字典中包含参数,并使用<distribution>(**param)将其作为关键字参数调用,例如:

param = {"a": 20759, "scale": 0.001, "loc": -41}
stats.gamma(**param)

Edit: more information here: *args and **kwargs? 编辑:更多信息在这里: * args和** kwargs?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM