我不知道是否有人熟悉Dan Foreman-Mackley的"emcee" Python模块,但我正在使用它对一个分布进行采样。我使用平行回火采样器,因为我的分布是时髦的外观。以下是相关代码:
from emcee import PTSampler
rendim = 6
renwalkers = 100
ntemps = 20
gauss2 = PTSampler(ntemps, renwalkers, rendim, lnlike, lnprior)
p0 = [[[np.random.rand()*0.24,np.random.rand()*3,np.random.rand()*1,np.random.rand()*1,np.random.rand()*3,np.random.rand()*1] for i in range(renwalkers)] for i in range(ntemps)]
for q, lnprob, lnlike in gauss2.sample(p0,iterations=1000):
pass
gauss2.reset()
for q, lnprob, lnlike in gauss2.sample(p, lnprob0=lnprob,
lnlike0=lnlike,
iterations=10000, thin=10):
pass
我一直得到错误,'numpy。narray ' object不可调用。下面是完整的错误:
TypeError Traceback (most recent call last)
<ipython-input-25-d5f2a1f4b7b0> in <module>()
11 p0 = [[[np.random.rand()*0.24,np.random.rand()*3,np.random.rand()*1,np.random.rand()*1,np.random.rand()*3,np.random.rand()*1] for i in range(renwalkers)] for i in range(ntemps)]
12
---> 13 for q, lnprob, lnlike in gauss2.sample(p0,iterations=1000):
14 pass
15 gauss2.reset()
/Library/Python/2.7/site-packages/emcee-2.1.0-py2.7.egg/emcee/ptsampler.py in sample(self, p0, lnprob0, lnlike0, iterations, thin, storechain)
255 self.logpargs, self.loglkwargs, self.logpkwargs)
256 if self.pool is None:
--> 257 results = list(map(fn, p.reshape((-1, self.dim))))
258 else:
259 results = list(self.pool.map(fn, p.reshape((-1, self.dim))))
/Library/Python/2.7/site-packages/emcee-2.1.0-py2.7.egg/emcee/ptsampler.py in __call__(self, x)
94 return lp, lp
95
---> 96 return self.logl(x, *self.loglargs, **self.loglkwargs), lp
97
98
TypeError: 'numpy.ndarray' object is not callable
更新:这里是lnlike和lnprior函数:
def lnprior(theta):
eta, mu1, sig1, h, mu2, sig2 = theta
if 0 < eta < 0.24 and 0 < mu1 < 1.5 and 0 < sig1 < 1 and 0 < h < 1 and 0 < mu2 < 1.5 and 0 < sig2 < 1:
return 0
return -np.inf
def lnlike(theta):
eta, mu1, sig1, h, mu2, sig2 = theta
return -k*np.log(Nsamp)+np.sum(np.log(np.sum(MCetadist(eta,events,mu1,sig1,h,mu2,sig2),axis=1)))
提示"TypeError: 'numpy. ' "。narray的" object is not callable"意味着"numpy。你试图作为一个函数调用的narray是一个模块或变量,因此是不可调用的。当你用'()'调用非函数时,你会有这个类型错误。您可以在该引用之后删除'()',一切都会好起来的。