gpt4 book ai didi

python - 使用 nltk 绘制 50 个最不常见的单词

转载 作者:行者123 更新时间:2023-12-02 04:09:30 24 4
gpt4 key购买 nike

如何绘制 50 个最不常见的单词?

也许我想得太复杂了。这是我得到这些话的方式:

distr = nltk.FreqDist(word for word in items)
words = distr .keys()
seldomwords = words [:50]

现在我该如何绘制它?

通过 FreqDistplot 函数,我得到全部或仅 x 个最常见的单词。

我尝试过类似的方法:

distr .plot(:50)

但这在语法上是不正确的。

最佳答案

这有点奇怪,但最简单的方法是

  • 首先,您必须从 FreqDist 中提取最不常见的项目
  • 然后重新创建最不常见的项目并将其反馈到新的 FreqDist 对象中
  • 使用 FreqDist.plot() 使用新的 FreqDist。

[代码]:

>>> from nltk import FreqDist
>>> fd = FreqDist(list('aaabbbbbcccccdddddddd'))
>>> last_two = FreqDist(dict(fd.most_common()[-2:]))
>>> last_two.plot()

[输出]:

enter image description here

[代码]:

>>> from nltk import FreqDist
>>> fd = FreqDist(list('aaabbbbbcccccdddddddd'))
>>> last_two = FreqDist(dict(fd.most_common()[-2:]))
>>> last_two.plot()
>>> last_three = FreqDist(dict(fd.most_common()[-3:]))
>>> last_three.plot()

[输出]:

enter image description here

关于python - 使用 nltk 绘制 50 个最不常见的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37461239/

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