加载泡菜 NotFittedError:CountVectorizer - 未安装词汇
Posted
技术标签:
【中文标题】加载泡菜 NotFittedError:CountVectorizer - 未安装词汇【英文标题】:Load pickle NotFittedError: CountVectorizer - Vocabulary wasn't fitted 【发布时间】:2020-08-01 16:41:50 【问题描述】:我正在尝试使用 scikit 机器学习对垃圾邮件进行分类。一旦我将矢量化器和分类器转储到各自的 .pkl 文件中并在 temp.py 中导入 tem 以进行预测,我就会收到此错误:
raise NotFittedError(msg % 'name': type(estimator).__name__)
NotFittedError: CountVectorizer - Vocabulary wasn't fitted
一旦我建立了一个模型,用名称(my_model.pkl),(vectorizer.pkl)保存模型并重新启动我的内核,但是当我在预测示例文本期间加载保存的模型(sample.pkl)时,它给出未找到相同的 Volcubary 错误。
app.py:
import pandas as pd
df = pd.read_csv('spam.csv', encoding="latin-1")
#Drop the columns not needed
df.drop(['Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4'], axis=1, inplace=True)
#Create a new column label which has the same values as v1 then set the ham and spam values to 0 and 1 which is the standard format for our prediction
df['label'] = df['v1'].map('ham': 0, 'spam': 1)
#Create a new column having the same values as v2 column
df['message'] = df['v2']
#Now drop the v1 and v2
df.drop(['v1', 'v2'], axis=1, inplace=True)
#print(df.head(10))
from sklearn.feature_extraction.text import CountVectorizer
bow_transformer = CountVectorizer().fit_transform(df['message'])
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import classification_report
#Split the data
X_train, X_test, y_train, y_test = train_test_split(bow_transformer, df['label'], test_size=0.33, random_state=42)
#Naive Bayes Classifier
clf = MultinomialNB()
clf.fit(X_train,y_train)
clf.score(X_test,y_test)
y_pred = clf.predict(X_test)
print(classification_report(y_test, y_pred))
pickle.dump(bow_transformer, open("vector.pkl", "wb"))
pickle.dump(clf, open("my_model.pkl", "wb"))
temp.py:::我在这个文件中做预测
from sklearn.feature_extraction.text import CountVectorizer
cv=CountVectorizer()
vectorizer = pickle.load(open("my_model.pkl", "rb"))
selector = pickle.load(open("vector.pkl", "rb"))
test_set=["heloo how are u"]
new_test=cv.transform(test_set)
【问题讨论】:
以下答案对您有帮助吗? 【参考方案1】:在您的app.py
中,您正在酸洗document-term matrix 而不是矢量化器,
pickle.dump(bow_transformer, open("vector.pkl", "wb"))
bow_transformer 在哪里
bow_transformer = CountVectorizer().fit_transform(df['message'])
在你的temp.py
中,当你解开它时,你只有文档术语矩阵。正确的腌制方法是:
bow_transformer = CountVectorizer().fit(df['message'])
bow_transformer_dtm = bow_transformer.transform(df['message'])
现在您可以使用
腌制您的bow_transformer
pickle.dump(bow_transformer, open("vector.pkl", "wb"))
这将是一个转换器而不是文档术语矩阵。
在您的temp.py
中,您可以解开它并使用它,如下图所示:
selector = pickle.load(open("vector.pkl", "rb"))
test_set=["heloo how are u"]
new_test=selector.transform(test_set)
希望这会有所帮助!
【讨论】:
以上是关于加载泡菜 NotFittedError:CountVectorizer - 未安装词汇的主要内容,如果未能解决你的问题,请参考以下文章