我有一组离散的数据,看起来泊松分布在0到90之间。我正在尝试曲线拟合数据。我的代码如下:
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
import numpy as np
from scipy.stats import poisson
from scipy.optimize import curve_fit
from scipy.misc import factorial
plt.figure(figsize = (10, 10))
entries, bin_edges, patches = plt.hist(data, bins = 90, range = [1, 90], normed = True)
bin_middles = 0.5*(bin_edges[1:] + bin_edges[:-1])
def poisson(k, lamb):
return (lamb**k/factorial(k)) * np.exp(-lamb)
params, cov = curve_fit(poisson, bin_middles, entries)
x_plot = np.linspace(1, 90, 90)
plt.plot(x_plot, poisson(x_plot, *params), 'b--', lw = 2)
plt.axvline(np.mean(data), linestyle = '--', color = 'g')
plt.ylim(0, max(entries)+max(entries)/4)
plt.xlim([0, max(x_plot)])
我无法确定为什么曲线位于左侧且值非常小。谢谢你的帮助!
答案 0 :(得分:2)
这不是一个真正的答案,但我无法将评论纳入评论。我在你的代码中添加了一些合成数据(添加了适当的导入):
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.misc import factorial
%matplotlib inline
plt.figure(figsize = (10, 10))
#make up some synthetic data
data = np.random.poisson(3, 5000000)
entries, bin_edges, patches = plt.hist(data, bins = 90, range = [1, 90], normed = True)
bin_middles = 0.5*(bin_edges[1:] + bin_edges[:-1])
def poisson(k, lamb):
return (lamb**k/ factorial(k)) * np.exp(-lamb)
params, cov = curve_fit(poisson, bin_middles, entries)
x_plot = np.linspace(1, 30, 30)
plt.plot(x_plot, poisson(x_plot, *params), 'r', lw = 2)
你的数据肯定不是非常Poissonian(峰值和宽度之间的关系是错误的,加上向右的非常缓慢的下降),但是我找不到你的情节如此偏离的原因。