【问题标题】:How to Select Top 1000 words using TF-IDF Vector?如何使用 TF-IDF 向量选择前 1000 个单词?
【发布时间】:2019-01-10 08:12:38
【问题描述】:

我有一个包含 5000 条评论的文档。我在该文件上应用了 tf-idf。这里 sample_data 包含 5000 条评论。我在 一克范围 的 sample_data 上应用 tf-idf 矢量化器。现在我想获得前 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)

【问题讨论】:

    标签: python-3.x scikit-learn tf-idf sklearn-pandas tfidfvectorizer


    【解决方案1】:

    TF-IDF 值取决于单个文档。您可以使用max_features parameter of TfidfVectorizer 根据计数 (Tf) 获得前 1000 个术语:

    max_features : int 或 None,默认=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_ : 数组、形状 = [n_features] 或无

      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”获得前 1000 名,因为 tf-idf 是单个文档中的一个术语 tfidf(全局)的乘积词汇。因此,对于在单个文档中出现 2 次的同一个词,其 tf-idf 将是在另一个文档中只出现一次的同一个词的两倍。您如何比较同一术语的不同值。希望这能说明问题。

    【讨论】:

    • 大概 OP 想要将 5000 条评论视为单独的文档,因此他们提到的“文档”真的是语料库吗?在这种情况下,TFIDF 是明确定义的。
    • @smci 对不起,我不明白。如果 OP 想要将每条评论视为单独的文档,那么他是否希望将每条评论中的前 1000 个术语分开?
    猜你喜欢
    • 2014-12-14
    • 2013-05-31
    • 2023-03-19
    • 2015-01-14
    • 2023-02-02
    • 2019-11-16
    • 2019-04-17
    • 2019-06-12
    • 2012-02-29
    相关资源
    最近更新 更多