找到最常见的术语

2024-06-22 10:35:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在跟踪example in Scikit learn docs,其中CountVectorizer用于某些数据集。

问题:count_vect.vocabulary_.viewitems()列出所有术语及其频率。你如何根据发生的次数来分类?

sorted( count_vect.vocabulary_.viewitems() )似乎不起作用。


Tags: 数据indocsexamplecount分类scikit次数
1条回答
网友
1楼 · 发布于 2024-06-22 10:35:12

vocabulary_.viewitems()实际上并不列出术语及其频率,而是从术语到索引的映射。频率(每个文档)由fit_转换方法返回,该方法返回一个稀疏(coo)矩阵,其中行是文档,列是单词(列索引通过词汇表映射到单词)。例如,您可以通过

matrix = count_vect.fit_transform(doc_list)
freqs = zip(count_vect.get_feature_names(), matrix.sum(axis=0))    
# sort from largest to smallest
print sorted(freqs, key=lambda x: -x[1])

相关问题 更多 >