2013-11-25 68 views
3

我需要適合泊松分佈到一組數據:泊松分佈適合

fitfunc = lambda p, x: p[0]*pow(p[1],x)*pow(e,-p[1])/math.gamma(x+1) # Target function 
errfunc = lambda p, x, y: fitfunc(p, x) - y # Distance to the target function 
p0 = [1., 2.] # Initial guess for the parameters 
p1, success = optimize.leastsq(errfunc, p0[:], args=(bins_mean, n)) 

我提出,使用在SciPy documentation給出的例子。

如果我對伽瑪函數部分發表評論,它就像一個魅力,所以問題在那裏,但我不知道如何解決它。

我得到以下錯誤:

TypeError: can only concatenate list (not "int") to list 

擬合的輸入參數是plt.hist的輸出,我檢查和類型是numpy的ndarray

+0

你會得到什麼錯誤? –

+2

如果你有數據(不只是直方圖),這裏沒有必要進行優化。對於泊松分佈,只需通過「np.mean(data)」就可以分析找到最合適的參數(lambda,你的'p [1]')。 'p [0]'只是標準化。 – askewchan

+1

是'bins_mean'和'n' numpy數組嗎?錯誤使我相信你正在通過列表。 –

回答

4
fitfunc = lambda p, x: p[0]*pow(p[1],x)*pow(e,-p[1])/math.gamma(x+1) # Target function 
errfunc = lambda p, x, y: fitfunc(p, x) - y # Distance to the target function 
p0 = [1., 2.] # Initial guess for the parameters 
p1, success = optimize.leastsq(errfunc, p0[:], args=(bins_mean, n)) 

既然你說它工作沒有math.gamma(x + 1)的一部分,我猜測它會工作,如果你改變

fitfunc = lambda p, x: p[0]*pow(p[1],x)*pow(e,-p[1])/math.gamma(x+1) 

from scipy.misc import factorial 
fitfunc = lambda p, x: p[0]*pow(p[1],x)*pow(e,-p[1])/factorial(x) 

因爲math.gamma不喜歡列表(或任何不是我猜的浮動),而階乘可以正常使用列表嗎?

旁邊的問題:你爲什麼要使用pow,而不是僅僅使用**?