TfidfVectorizer 是否隐式地阈值其拟合输出用于大型数据集?



我正在尝试使用sklearnTfidfVectorizer来输出输入列表的tf-idf分数,该输入列表由Unigram和Bigram组成。

这是我正在做的事情的本质:

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)

此代码段输出以下内容:

Length of input list: 12333
Length of learned vocabulary: 6196
Term document matrix shape is: (12333, 6196)

将输入元素映射到TfidfVectorizer发出的位置索引的字典长度短于它馈送的唯一输入数。对于较小的数据集(大约~50个元素(,这似乎不是问题 -TfidfVectorizer在拟合后生成的字典大小等于输入的大小。

我错过了什么?

确保comprehensive_ngrams是唯一单词的列表。 即:

assert len(set(comprehensive_ngrams)) == len(comprehensive_ngrams)

最新更新