【问题标题】:Find the Most common term in Scikit-learn classifier [duplicate]在 Scikit-learn 分类器中找到最常用的术语 [重复]
【发布时间】:2013-04-23 16:49:03
【问题描述】:

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

问题count_vect.vocabulary_.viewitems() 列出了所有术语及其频率。您如何按出现次数对它们进行排序?

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

【问题讨论】:

标签: python python-2.7 numpy scipy scikit-learn


【解决方案1】:

vocabulary_.viewitems() 实际上并没有列出术语及其频率,而是从术语到它们的索引的映射。频率(每个文档)由 fit_transform 方法返回,该方法返回一个稀疏(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])

【讨论】:

  • 您需要将matrix.sum(axis=0) 替换为matrix.sum(axis=0).tolist()[0],因为matrix.sum() 返回一个矩阵。
猜你喜欢
  • 2017-08-06
  • 2015-05-09
  • 1970-01-01
  • 2019-03-06
  • 2012-05-22
  • 2019-01-12
  • 2015-09-25
  • 2014-02-25
相关资源
最近更新 更多