gpt4 book ai didi

python - TfidfVectorizer 是否隐式地对大型数据集的拟合输出进行阈值处理?

转载 作者:行者123 更新时间:2023-11-30 09:05:59 27 4
gpt4 key购买 nike

我正在尝试使用 sklearnTfidfVectorizer 输出由两个一元组组成的输入列表的 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)

此代码片段输出以下内容:

Length of input list: 12333

Length of learned vocabulary: 6196

Term document matrix shape is: (12333, 6196)

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

我错过了什么?

最佳答案

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

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

关于python - TfidfVectorizer 是否隐式地对大型数据集的拟合输出进行阈值处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51904251/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com