2017-02-27 117 views
-1

我知道以前有關於該主題的問題,但我找不到我的問題的具體情況。我試圖在直方圖上繪製泊松分佈。我試着在SciPy的使用泊松功能從統計模塊的第一件事:疊加泊松分佈到直方圖

import numpy 
from scipy.stats import poisson 

mu = mean(data) 
n, bins, patches = pyplot.hist(data, 20, normed = 1) 
pyplot.plot(bins, poisson.pmf(bins, mu), 'r-') 
pyplot.show() 

然而,由於如圖所示(圖中藍色我的數據的柱狀圖),我得到的紅色情節有三個山峯怪異。

The histogram in blue represents my data. The red line is the Poisson function using scipy.stats

因此我試着寫我自己的泊松分佈函數:

def poisson(mu, x): 
    from scipy.misc import factorial 
    return numpy.exp(-mu) * mu**x * factorial(x)**-1 

y = poisson(mu, bins) 

但是,當我嘗試打印它,我得到楠的數組。難道我做錯了什麼?或者是垃圾箱裏的數字太大?

print y  
[ nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan] 

但是打印從stats.poisson結果時,我得到:

[3.25452236e-06 0.00000000e+00 0.00000000e+00 0.00000000e+00 
0.00000000e+00 3.63110218e-04 0.00000000e+00 0.00000000e+00 
0.00000000e+00 0.00000000e+00 5.24385396e-03 0.00000000e+00 
0.00000000e+00 0.00000000e+00 0.00000000e+00 1.06061293e-02 
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 
3.23183010e-03] 

回答

0
  • 爲泊松功能,你應該輸入,而不是你「垃圾箱」給「詮釋」,numpy的。例如(1400,1475)。

  • 對於你自己的泊松函數,當你使用'factorial'時,你必須小心,特別是對於大x(x> 20),因爲它會迅速增加!我懷疑是你的楠的起源。也不存在階乘的浮點數!

嘗試:

X = np.arange(1200, 1450) 
plt.plot(X, poisson.pmf(X,1375), 'r-') 
+0

嗨,好吧,我想通了,對我自己的階乘函數我不得不做出的垃圾箱整數,我不知道爲什麼它沒有對泊松發生功能在scipy模塊中。有點愚蠢的我。謝謝! – user3412058