use*_*145 13 python text-extraction machine-learning scikit-learn
我已经装了CountVectorizer一些文件scikit-learn.我想在文本语料库中看到所有术语及其相应的频率,以便选择停用词.例如
'and' 123 times, 'to' 100 times, 'for' 90 times, ... and so on
Run Code Online (Sandbox Code Playgroud)
这有什么内置功能吗?
Fre*_*Foo 22
如果cv是你的CountVectorizer并且X是矢量化语料库,那么
zip(cv.get_feature_names(),
np.asarray(X.sum(axis=0)).ravel())
Run Code Online (Sandbox Code Playgroud)
返回提取(term, frequency)的语料库中每个不同术语的对列表CountVectorizer.
(需要小asarray+ ravel舞蹈来解决一些怪癖scipy.sparse.)
没有内置的。根据Ando Saabas 的回答,我找到了一种更快的方法:
from sklearn.feature_extraction.text import CountVectorizer
texts = ["Hello world", "Python makes a better world"]
vec = CountVectorizer().fit(texts)
bag_of_words = vec.transform(texts)
sum_words = bag_of_words.sum(axis=0)
words_freq = [(word, sum_words[0, idx]) for word, idx in vec.vocabulary_.items()]
sorted(words_freq, key = lambda x: x[1], reverse=True)
Run Code Online (Sandbox Code Playgroud)
输出
[('world', 2), ('python', 1), ('hello', 1), ('better', 1), ('makes', 1)]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8398 次 |
| 最近记录: |