【发布时间】:2019-01-25 00:28:08
【问题描述】:
我正在尝试使用sklearn 的TfidfVectorizer 为输入列表输出tf-idf 分数,包括一元和二元。
这是我正在做的事情的本质:
comprehensive_ngrams = comprehensive_unigrams + comprehensive_bigrams # List of unigrams and bigrams(comprehensive_unigrams and comprehensive_bigrams are lists in their own right)
print("Length of input list: ", len(comprehensive_ngrams))
vectorizer = TfidfVectorizer(ngram_range = (1,2), lowercase = True)
vectorizer.fit(comprehensive_ngrams)
vocab = vectorizer.vocabulary_
print("Length of learned vocabulary: ", len(vocab))
term_document_matrix = vec.toarray()
print("Term document matrix shape is: ", term_document_matrix.shape)
这个 sn-p 输出如下:
Length of input list: 12333
Length of learned vocabulary: 6196
Term document matrix shape is: (12333, 6196)
将输入元素映射到由TfidfVectorizer 发出的位置索引的字典长度比它所馈送的唯一输入的数量要短。对于较小的数据集(大约 50 个元素)来说,这似乎不是问题——TfidfVectorizer 在拟合后生成的字典的大小等于输入的大小。
我错过了什么?
【问题讨论】:
-
您确定
comprehensive_ngrams是唯一术语列表吗?可以有非唯一条目吗?什么是len(set(comprehensive_ngrams))? -
@petezurich *comprehensive_ngrams 中有一些非唯一条目。我想这就是罪魁祸首。但是,我希望矢量化器输出语料库中某个(冗余)术语的所有出现及其相应的单个 tf-idf 分数。我该如何实现这一目标?
-
这似乎是一个新问题。请将此作为单独的问题与数据样本和 MCVE 一起发布:stackoverflow.com/help/mcve
-
如果这回答了您之前的问题,请接受我的回答,以便其他人可以看到问题已解决。
-
是的,我会改写我的查询
标签: python machine-learning scikit-learn nlp tf-idf