【问题标题】:how to get the most representative features in the following tfidf model?如何获得以下 tfidf 模型中最具代表性的特征?
【发布时间】:2017-05-08 23:03:00
【问题描述】:

您好,我有以下清单:

listComments = ["comment1","comment2","comment3",...,"commentN"]

我创建了一个 tfidf 矢量化器来从我的 cmets 获取模型,如下所示:

tfidf_vectorizer = TfidfVectorizer(min_df=10,ngram_range=(1,3),analyzer='word')
tfidf = tfidf_vectorizer.fit_transform(listComments)

现在为了进一步了解我的模型,我想获得最具代表性的特征,我尝试了:

print("these are the features :",tfidf_vectorizer.get_feature_names())
print("the vocabulary :",tfidf_vectorizer.vocabulary_)

这给了我一个我认为我的模型用于矢量化的单词列表:

these are the features : ['10', '10 days', 'red', 'car',...]

the vocabulary : {'edge': 86, 'local': 96, 'machine': 2,...}

但是我想找到一种方法来获得 30 个最具代表性的特征,我的意思是在我的 tfidf 模型中达到最高值的词,具有最高逆频率的词,我正在阅读文档但我不是能够找到这个方法我真的很感激这个问题的帮助,在此先感谢,

【问题讨论】:

    标签: scikit-learn tf-idf


    【解决方案1】:

    如果您想获得与 idf 分数相关的词汇列表,您可以使用idf_ 属性和argsort 它。

    # create an array of feature names
    feature_names = np.array(tfidf_vectorizer.get_feature_names())
    
    # get order
    idf_order = tfidf_vectorizer.idf_.argsort()[::-1]
    
    # produce sorted idf word
    feature_names[idf_order]
    

    如果您想获得每个文档的 tfidf 分数排序列表,您可以做类似的事情。

    # get order for all documents based on tfidf scores
    tfidf_order = tfidf.toarray().argsort()[::-1]
    
    # produce words
    feature_names[tfidf_order]
    

    【讨论】:

      猜你喜欢
      • 2017-05-11
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      相关资源
      最近更新 更多