我有一份有5000条评论的文档。我在那份文件上申请了tf idf。此处sample_data包含5000条评论。我正在将tf idf矢量器应用于一克范围的sample_data。现在我想获得前1000个单词从具有最高tf idf值的sample_ data。有人能告诉我如何获得最热门的单词吗?
from sklearn.feature_extraction.text import TfidfVectorizer
tf_idf_vect = TfidfVectorizer(ngram_range=(1,1))
tf_idf_vect.fit(sample_data)
final_tf_idf = tf_idf_vect.transform(sample_data)
TF-IDF值取决于各个文档。通过使用TfidfVectorizer:的max_features
参数,您可以根据计数(Tf(获得前1000项
max_features:int或None,default=None
If not None, build a vocabulary that only consider the top max_features ordered by term frequency across the corpus.
只需执行:
tf_idf_vect = TfidfVectorizer(ngram_range=(1,1), max_features=1000)
您甚至可以在使用idf_
属性对文档进行拟合(学习(后,从tf_idf_vect
中获得'idf'
(全局术语权重(:
idf_array,shape=[n_features]或None
The learned idf vector (global term weights) when use_idf is set to True,
调用tf_idf_vect.fit(sample_data)
:后执行此操作
idf = tf_idf_vect.idf_
然后从中选择前1000个,并根据这些选定的特征重新拟合数据。
但你不可能通过">tf-idf";,因为tf-idf是单个文档中的术语的CCD_ 6与词汇表的idf
(全局(的乘积。因此,对于在单个文档中出现2次的同一个单词,其tf idf将是在另一个文档中仅出现一次的相同单词的两倍。你怎么能比较同一术语的不同值呢。希望这能说明问题。