gpt4 book ai didi

python - 如何在测试集上应用 TFIDF

转载 作者:行者123 更新时间:2023-12-04 16:28:33 24 4
gpt4 key购买 nike

假设我有两个文本文件。文件 1 包含训练集,主要用于定义词汇表。文件 2 是用户输入的单词。

d1 = (
"Project 1 details on Machine learning",
"Project 2 detail on machine learning and statics",
"Project 3 is on mach learn as well"
)

d2 = (
"Projects related to machine learning"
)

现在使用 sklearn 我们找到 d1 的 tfidf

from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform(documents)
print( tfidf_matrix.shape)

现在对于查询 d2,我想根据从 d1 中学到的词汇来计算 tfidf 向量。我应该怎么做?

最佳答案

SKLearn 中的任何转换器一样,在您将其 .fit 放在火车组上之后(使用 .fit_transform(d1),在此case) 你可以使用 tfidf_vectorizer.transform(d2)

transform 测试集

关于python - 如何在测试集上应用 TFIDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57572184/

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