基于频繁/常见单词的直方图

3

我正在尝试基于频繁/常用单词创建直方图,但运行代码时仅收到错误提示。我已经成功找到了10个最常见的单词,但无法将其可视化为直方图。

description_list = df['description'].values.tolist()

from collections import Counter
Counter(" ".join(description_list).split()).most_common(10)

#histogram 
plt.bar(x, y)
plt.title("10 most frequent tokens in description")
plt.ylabel("Frequency")
plt.xlabel("Words")
plt.show

请勿通过破坏您的帖子给其他人带来更多工作。在Stack Exchange网络上发布帖子,您已授予Stack Exchange在CC BY-SA 4.0许可证下分发该内容的不可撤销权利(即使您将来做出其他选择)。 根据Stack Exchange的政策,分发的是未被破坏的版本。因此,任何破坏行为都将被还原。如果您想了解更多关于删除帖子的信息,请参见:如何删除帖子? - Machavity
1个回答

4

看起来漏掉了几个:

  1. Counter(...).most_common(10)的结果未被分配给xy
  2. xy似乎没有绑定任何值
  3. plt.show未被调用,因此它可能不会执行任何操作,或者输出类似于<function show at 0x...>的内容。

这里有一个可重现的例子,可以解决上述问题:

from collections import Counter
import matplotlib.pyplot as plt
import pandas as pd

data = {
    "description": [
        "This is the first example",
        "This is the second example",
        "This is similar to the first two",
        "This exists add more words"
    ]
}
df = pd.DataFrame(data)


description_list = df['description'].values.tolist()

# Assign the Counter instance `most_common` call to a variable:
word_frequency = Counter(" ".join(description_list).split()).most_common(10)

# `most_common` returns a list of (word, count) tuples
words = [word for word, _ in word_frequency]
counts = [counts for _, counts in word_frequency]

plt.bar(words, counts)
plt.title("10 most frequent tokens in description")
plt.ylabel("Frequency")
plt.xlabel("Words")
plt.show()

需要输出的结果如下:

代码的预期输出是一个柱状图,显示出现最频繁的10个单词。单词'This'出现了4次,'exists'只出现了一次。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接