2017-04-06 46 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)