gpt4 book ai didi

python - tfidfvectorizer 在保存的分类器中预测

转载 作者:太空宇宙 更新时间:2023-11-04 03:04:19 26 4
gpt4 key购买 nike

我使用 TfIdfVectorizer 和 MultinomialNB 训练了我的模型,并将其保存到 pickle 文件中。

现在我正尝试使用另一个文件中的分类器来预测看不见的数据,我不能这样做,因为它告诉我分类器的特征数量与我当前的特征数量不同语料库。

这是我试图预测的代码。函数 do_vectorize 与训练中使用的完全相同。

def do_vectorize(data, stop_words=[], tokenizer_fn=tokenize):
vectorizer = TfidfVectorizer(stop_words=stop_words, tokenizer=tokenizer_fn)
X = vectorizer.fit_transform(data)
return X, vectorizer

# Vectorizing the unseen documents
matrix, vectorizer = do_vectorize(corpus, stop_words=stop_words)

# Predicting on the trained model
clf = pickle.load(open('../data/classifier_0.5_function.pkl', 'rb'))
predictions = clf.predict(matrix)

但是我收到的错误是特征数量不同

ValueError: Expected input with 65264 features, got 472546 instead

这意味着我还必须从训练中保存词汇以进行测试?如果训练中不存在术语,会发生什么情况?

我尝试使用来自 scikit-learn 的管道,具有相同的矢量化器和分类器,以及两者的相同参数。但是,从1小时变成6个多小时太慢了,所以我更喜欢手动完成。

最佳答案

This means I also have to save my vocabulary from training in order to test?

是的,您必须保存整个 tfidf vectorizer,这尤其意味着保存词汇。

What will happen if there are terms that did not exist on training?

它们将被忽略,这是完全合理的,因为您没有关于此的没有训练数据,因此没有什么需要考虑的(还有更复杂的方法仍然可以使用它,但是它们不要使用像 tfidf 这样简单的方法。

I tried to used pipelines from scikit-learn with the same vectorizer and classifier, and the same parameters for both. However, it turned too slow from 1 hour to more than 6 hours, so I prefer to do it manually.

使用管道时应该几乎没有开销,但是只要您还记得存储矢量化器,手动执行操作就可以了。

关于python - tfidfvectorizer 在保存的分类器中预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39961294/

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