TfidfVectorizer - 词汇表不适合

TfidfVectorizer - Vocabulary wasn't fitted

导入模型的代码和运行对单个输入的预测

Tfidf_vect = TfidfVectorizer(max_features=5000) # Same classifier as I used in the model
Train_X_IP = Tfidf_vect.transform(["change in the meaning"]).toarray() #Passing the input 
loaded_model = pickle.load(open("finalized_model.sav", 'rb')) #loading the model

predictions_SVM = loaded_model.predict_proba(Train_X_IP)
print(predictions_SVM)


Error I get : TfidfVectorizer - Vocabulary wasn't fitted.

我看到很多文章提出了很多方法,到目前为止我尝试了

添加 fit_transform 而不是 Tfidf_vect.transform 但这并没有解决问题

我尝试的第二个选项,从外部加载 Tfidfvectorizer

 Tfidf_vect = TfidfVectorizer(max_features=5000)
 import pickle
 pickle.dump(Tfidf_vect, open("vectorizer.pickle", "wb"))  
 multilabel_binarizer = joblib.load('vectorizer.pickle')
 Still get the same error : TfidfVectorizer - Vocabulary wasn't fitted.

这是使用模型和向量化器的正确方法吗? ?

您首先需要用于训练模型的矢量化器。我假设您会在训练期间使用 "fit" 或 "fit_transform" 函数。完成后,将矢量化器保存为 joblib 格式的 pickle。然后重新载入,对新数据使用变换函数进行预测。