2017-04-06 103 views
0

我有一个程序,用于抓取维基百科页面并查找从任意随机页面到哲学页面的长度。该程序生成一个路径长度(从源页面到哲学)的列表,该列表被传递给绘制每个路径长度频率的另一个函数。我的方法是基于this SO帖子的回答。用一组分布拟合一个直方图

在这个函数中,我使用一组不同的分布曲线来拟合曲线,以查看哪一个最适合数据集。出于某种原因,它看起来像分布曲线偏离中心,距图中的实际直方图:

enter image description here

这似乎是应该的分布直方图之间的中心位置。这里是绘制频率的功能:

def plot_lengths(lens): 
    """Plot the distribution of path lengths.""" 
    freq = {} 
    max_len = 0 

    for length in lens: 
     max_len = max(length,max_len) 
     if length in freq: 
      freq[length] += 1 
     else: 
      freq[length] = 1 
    max_freq = max(freq.values()) 
    bins = range(0, max_len + 1, 2) 
    plt.hist(lens,bins,histtype = 'bar',rwidth = 0.8) 
    plt.xlabel('x') 
    plt.ylabel('Path Lengths') 
    plt.title('Distribution of path lengths') 
    dist_names = ['gamma', 'beta', 'rayleigh', 'norm', 'pareto'] 

    for dist_name in dist_names: 
     dist = getattr(scipy.stats, dist_name) 
     param = dist.fit(lens) 
     pdf_fitted = dist.pdf(bins, *param[:-2], loc=param[-2], scale=param[-1]) * len(lens) 
     plt.plot(pdf_fitted, label=dist_name) 
     plt.xlim(0,max_len) 
     plt.ylim(0,max_freq) 
    plt.legend(loc='upper right') 
    plt.show() 

什么可能导致图中的分布偏离中心?

回答

1

绘制拟合时,您忘了设置x。 第2行中的第4行应为

plt.plot(bins, pdf_fitted, label=dist_name)