tfidfvectorizer 在保存的分类器中预测
tfidfvectorizer Predict in saved classifier
我使用 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.
使用管道时应该几乎没有开销,但是只要您还记得存储矢量化器,手动执行操作就可以了。
我使用 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.
使用管道时应该几乎没有开销,但是只要您还记得存储矢量化器,手动执行操作就可以了。