我想使用"双高斯"来对某些数据进行拟合。 (即两个正态分布的归一化和)。使用SciPy 0.18.1,以下代码可以工作:
import numpy as np
from scipy.stats import rv_continuous
class DoubleGauss(rv_continuous):
"""Double Gaussian distribution"""
def _pdf(self, x, mu1, sigma1, mu2, sigma2, w):
g1 = np.exp(-0.5*((x-mu1)/sigma1)**2) / np.sqrt(2.0*np.pi*sigma1**2)
g2 = np.exp(-0.5*((x-mu2)/sigma2)**2) / np.sqrt(2.0*np.pi*sigma2**2)
return w * g1 + (1.0-w) * g2
dg = DoubleGauss(name='DG')
print dg.rvs(mu1=0.001, sigma1=2.0, mu2=0.05, sigma2=4.0, w=0.8)
(这当然是一个最小的例子。最后,我还将重新定义_cdf的速度和_argcheck以允许mu参数的非零值。)
因为我需要使用其他一些库来读取要安装的数据,所以我必须在只有SciPy 0.8.0可用的环境中运行代码。但是,运行相同的代码会导致此错误:
File "test.py", line 13, in <module>
print dg.rvs(mu1=0.01, sigma1=2.0, mu2=0.05, sigma2=4.0, w=0.8)
File "/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/py2-scipy/0.8.0-cms/lib/python2.7/site-packages/scipy/stats/distributions.py", line 523, in rvs
vals = self._rvs(*args)
File "/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/py2-scipy/0.8.0-cms/lib/python2.7/site-packages/scipy/stats/distributions.py", line 848, in _rvs
Y = self._ppf(U,*args)
File "/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/py2-scipy/0.8.0-cms/lib/python2.7/site-packages/scipy/stats/distributions.py", line 861, in _ppf
return self.vecfunc(q,*args)
File "/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/py2-numpy/1.6.1-cms/lib/python2.7/site-packages/numpy/lib/function_base.py", line 1862, in __call__
theout = self.thefunc(*newargs)
File "/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/py2-scipy/0.8.0-cms/lib/python2.7/site-packages/scipy/stats/distributions.py", line 817, in _ppf_single_call
return optimize.brentq(self._ppf_to_solve, self.xa, self.xb, args=(q,)+args, xtol=self.xtol)
File "/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/py2-scipy/0.8.0-cms/lib/python2.7/site-packages/scipy/optimize/zeros.py", line 362, in brentq
r = _zeros._brentq(f,a,b,xtol,maxiter,args,full_output,disp)
File "/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/py2-scipy/0.8.0-cms/lib/python2.7/site-packages/scipy/stats/distributions.py", line 814, in _ppf_to_solve
return apply(self.cdf, (x, )+args)-q
File "/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/py2-scipy/0.8.0-cms/lib/python2.7/site-packages/scipy/stats/distributions.py", line 952, in cdf
place(output,cond,self._cdf(*goodargs))
File "/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/py2-scipy/0.8.0-cms/lib/python2.7/site-packages/scipy/stats/distributions.py", line 855, in _cdf
return self.veccdf(x,*args)
File "/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/py2-numpy/1.6.1-cms/lib/python2.7/site-packages/numpy/lib/function_base.py", line 1862, in __call__
theout = self.thefunc(*newargs)
File "/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/py2-scipy/0.8.0-cms/lib/python2.7/site-packages/scipy/stats/distributions.py", line 852, in _cdf_single_call
return scipy.integrate.quad(self._pdf, self.a, x, args=args)[0]
File "/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/py2-scipy/0.8.0-cms/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 247, in quad
retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
File "/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/py2-scipy/0.8.0-cms/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 313, in _quad
return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)
TypeError: _pdf() takes exactly 7 arguments (2 given)
在文档中,我发现如何使用rv_continuous的描述没有区别。如何修复代码以使其与SciPy 0.8.0一起使用?
答案 0 :(得分:0)
在0.13之前,pdf等人不接受关键字参数。尝试将它们作为位置参数,而不是关键字(命名)参数。
自v0.8以来还有其他一些改进。我认为你需要在构造函数中指定形状,还有一些callef xa和xb for ppf ---你可能想查看https://github.com/scipy/scipy/blob/v0.8.0/scipy/stats/distributions.py
的来源