【发布时间】:2020-05-10 09:29:34
【问题描述】:
好的,我关注 https://medium.com/@phylypo/text-classification-with-scikit-learn-on-khmer-documents-1a395317d195 ,正在使用这样布局并命名为 result 的数据框:
target type post
1 intj "hello world shdjd"
2 entp "hello world fddf"
16 estj "hello world dsd"
4 esfp "hello world sfs"
1 intj "hello world ddfd"
每个帖子都是独一无二的,目标只是为 16 种类型或类别中的每一种分配编号 1-16。我想使用 sklearn 来查找 16 种类型中的每一种的最热门单词。
我知道您可以使用 TfidfTransformer 获取语料库的热门词并查看 Sklearn how to get the 10 words from each topic ,但我不知道这在数据帧中究竟是如何发挥作用的。
def get_top_n_words(corpus, n=None):
vec = CountVectorizer().fit(corpus)
bag_of_words = vec.transform(corpus)
sum_words = bag_of_words.sum(axis=0)
words_freq = [(word, sum_words[0, idx]) for word, idx in vec.vocabulary_.items()]
words_freq = sorted(words_freq, key=lambda x: x[1], reverse=True)
return words_freq[:n]
print(get_top_n_words(result.post, 10))
这让我在所有帖子中排名前 10,但没有删除“this”或“and”等停用词,并且没有按类型分类。我该怎么做? p>
【问题讨论】:
标签: python machine-learning scikit-learn