【问题标题】:In NLP using tf-idf how to find the frequency of specific word from the corpus(contaning large numbers of documentation) in python在使用 tf-idf 的 NLP 中,如何在 python 中从语料库(包含大量文档)中找到特定单词的频率
【发布时间】:2019-09-01 17:17:42
【问题描述】:

如何使用 Tf-idf 从语料库中查找单个单词的频率。下面是我的示例代码,现在我想打印一个单词的频率。我怎样才能做到这一点?

from sklearn.feature_extraction.text import CountVectorizer

vectorizer = CountVectorizer()
corpus = ['This is the first document.',
      'This is the second second document.',
      'And the third one.',
      'Is this the first document?',]
X = vectorizer.fit_transform(corpus)
X
print(vectorizer.get_feature_names())
X.toarray()
vectorizer.vocabulary_.get('document')

print(vectorizer.get_feature_names())

X.toarray()

vectorizer.vocabulary_.get('document')

【问题讨论】:

    标签: python nlp tf-idf n-gram countvectorizer


    【解决方案1】:

    您的vectorizer.vocabulary_ 有每个单词的计数:

    print(vectorizer.volcabulary_)
    
    {'this': 8,
     'is': 3,
     'the': 6,
     'first': 2,
     'document': 1,
     'second': 5,
     'and': 0,
     'third': 7,
     'one': 4}
    

    然后计算词频很简单:

    vocab = vectorizer.vocabulary_
    tot = sum(vocab.values())
    frequency = {vocab[w]/tot for w in vocab.keys()}
    

    【讨论】:

      猜你喜欢
      • 2020-03-16
      • 2015-12-31
      • 2020-10-28
      • 2015-09-07
      • 2015-11-27
      • 2019-10-18
      • 2020-01-06
      • 2014-09-01
      • 1970-01-01
      相关资源
      最近更新 更多