2013-04-29 24 views
3

我遵循example in Scikit learn docs,其中CountVectorizer用於某些數據集。查找Scikit-learn分類器中最常用的術語

問題count_vect.vocabulary_.viewitems()列出了所有的術語及其頻率。你如何根據發生次數對它們進行分類?

sorted(count_vect.vocabulary_.viewitems())似乎沒有工作。

+0

嗨!也許你想看到我的回答https://stackoverflow.com/a/48490046/1093674 – 2018-01-28 18:49:01

回答

13

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]) 
相關問題