我有一个程序,可以刮去维基百科页面,并找到从任意随机页面到哲学页面的长度。程序生成路径长度列表(从源页面到哲学),这些路径将传递给另一个函数,该函数绘制每个路径长度的频率。我的方法是基于thisSO post的答案。
在这个函数中,我用一组不同的分布曲线拟合曲线,以确定哪一条曲线最适合数据集。出于某种原因,分布曲线看起来偏离中心,远离图中的实际直方图:
似乎分布应该在直方图之间居中。以下是绘制频率的功能:
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()
什么会导致图中的分布偏离中心?
绘制配合时忘记设置x。 第2个for循环中的第4行应该是
相关问题 更多 >
编程相关推荐