gpt4 book ai didi

python - 如何计算文本中词频的最佳 zipf 分布

转载 作者:行者123 更新时间:2023-12-01 08:06:17 27 4
gpt4 key购买 nike

对于家庭作业,我必须绘制文本的词频并将其与最佳 zipf 分布进行比较。

根据对数对数图中的排名绘制文本的词频计数似乎效果很好。

但是我在计算最佳 zipf 分布时遇到了麻烦。结果应该如下所示:

a

我不明白计算 zipf 直线的方程是什么样的。

zipf定律的德语维基百科页面上,我发现了一个似乎有效的方程式

b

但没有引用来源,所以我不明白 1.78 常数来自哪里。

#tokenizes the file 
tokens = word_tokenize(raw)
tokensNLTK = Text(tokens)

#calculates the FreqDist of all words - all words in lower case
freq_list = FreqDist([w.lower() for w in tokensNLTK]).most_common()

#Data for X- and Y-Axis plot
values=[]
for item in (freq_list):
value = (list(item)[1]) / len([w.lower() for w in tokensNLTK])
values.append(value)

#graph of counted frequencies gets plotted
plt.yscale('log')
plt.xscale('log')
plt.plot(np.array(list(range(1, (len(values)+1)))), np.array(values))

#graph of optimal zipf distribution is plotted
optimal_zipf = 1/(np.array(list(range(1, (len(values)+1))))* np.log(1.78*len(values)))###1.78
plt.plot(np.array(list(range(1, (len(values)+1)))), optimal_zipf)
plt.show()

我使用此脚本的结果如下所示:

my zipf distribution

但我只是不确定最佳 zipf 分布是否计算正确。如果是这样,最佳 zipf 分布不应该在某一点穿过 X 轴吗?

编辑:如果有帮助的话,我的文本有 2440400 个标记和 27491 个类型

最佳答案

看看这个 research paper by Andrew William Chisholm.特别是第 #22 页。

H(N) ≈ ln(N) + γ

Where γ is the Euler-Mascheroni constant with approximate value 0.57721. Noting that exp(γ) ≈ 1.78, equation <...> can be re-written to become for large N (N must be greater than 1,000 for this to be accurate to one part in a thousand).

pr ≈ 1 / [r*ln(1.78*N)]

关于python - 如何计算文本中词频的最佳 zipf 分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55518957/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com