使用 Scikit-Learn CountVectorizer 根据文本语料库中的出现次数列出词汇表中的单词 [英] List the words in a vocabulary according to occurrence in a text corpus, with Scikit-Learn CountVectorizer

查看:29
本文介绍了使用 Scikit-Learn CountVectorizer 根据文本语料库中的出现次数列出词汇表中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为 scikit-learn 中的一些文档安装了一个 CountVectorizer.我想在文本语料库中查看所有术语及其对应的频率,以便选择停用词.例如

I have fitted a CountVectorizer to some documents in scikit-learn. I would like to see all the terms and their corresponding frequency in the text corpus, in order to select stop-words. For example

'and' 123 times, 'to' 100 times, 'for' 90 times, ... and so on

是否有任何内置函数?

推荐答案

如果 cv 是你的 CountVectorizer 并且 X 是向量化的语料库,然后

If cv is your CountVectorizer and X is the vectorized corpus, then

zip(cv.get_feature_names(),
    np.asarray(X.sum(axis=0)).ravel())

CountVectorizer 提取的语料库中每个不同的词返回 (term, frequency) 对的列表.

returns a list of (term, frequency) pairs for each distinct term in the corpus that the CountVectorizer extracted.

(需要使用 asarray + ravel 来解决scipy.sparse 中的一些怪癖.)

(The little asarray + ravel dance is needed to work around some quirks in scipy.sparse.)

这篇关于使用 Scikit-Learn CountVectorizer 根据文本语料库中的出现次数列出词汇表中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆