【问题标题】:How to find most frequently used words to describe a category如何找到最常用的词来描述一个类别
【发布时间】:2019-07-27 07:49:00
【问题描述】:

我有两列。一个是动物,另一个是它们的描述。我想在 Python 中找到与每种动物相关的最常见单词。另外,我想添加一组单词,包括英语停用词和二元组和三元组。或许能找到前 20 个单词/短语。

dataset = pd.read_sql( q , dlconn )
x=dataset['Animal']
y= dataset[Description]
count_vect = CountVectorizer(stop_words = esw, ngram_range=(1, 3))

【问题讨论】:

  • 你能提供样本数据吗?一种特定的动物会出现在多行还是只有一个?
  • x: [狗, 狗, 猫, 猫, 猫, 兔子] , y: [woof hairy, hairy big, meow, meow meowwhisers,胡萝卜]

标签: python countvectorizer


【解决方案1】:

要查找最常用的单词,您可以使用collections.Counter

from collections import Counter

df = pd.DataFrame({
    'Animal': ['dog', 'dog', 'cat', 'cat', 'cat', 'rabbit'] ,
    'Description':['woof hairy', 'hairy big', 'meow', 'meow', 'meow whiskers', 'carrot']
})

most_common = {}
for animal, grp_df in df.groupby('Animal')['Description']:
    counts = Counter([word for phrase in grp_df.tolist() for word in phrase.split(' ')])
    most_common[animal] = max(counts.keys(), key=lambda x: counts[x])

输出:

{'cat': 'meow', 'dog': 'hairy', 'rabbit': 'carrot'}

【讨论】:

  • 您好,感谢您回复我!我很感激你的帮助。你能用我到目前为止的代码来展示它吗?你如何结合 esw 和 trigrams?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多