>我正在尝试保存经过训练的 SGD 分类器。我正在使用 Divtvectorizer.但是当我使用它进行预测时加载腌制分类器后,我收到以下错误
AttributeError: 'DictVectorizer' object has no attribute 'vocabulary_'
如何解决这个问题,我们可以保存字典向量器的词汇吗?
谢谢
下面是代码:
vecto= DictVectorizer(sparse=False)
transformer=vecto
X_train=transformer.fit_transform(features(sents))
X_test=transformer.transform(features(test))
y_test=[-1,1]
clf=SGDClassifier(alpha=0.2,loss='hinge',n_jobs=5)
clf=clf.partial_fit(X_train[:2],labels[:2],classes=[-1,1])
clf.partial_fit(X_train[2:3],labels[2:3],classes=[-1,1])
print clf.predict(X_test)
print clf.score(X_test,y_test)
您可以使用
pickle
库来保存和加载您的SGDClassifer
以及您的DictVectorizer
喜欢这样:
import pickle
# save SGDClassifier
with open('model.pkl','wb') as f:
pickle.dump(clf,f)
# load SGDClassifier
with open('model.pkl', 'rb') as f:
clf2 = pickle.load(f)
并类似于您的DictVectorizer
:
# save DictVectorizer
with open('transformer.pkl','wb') as f:
pickle.dump(transformer,f)
# load DictVectorizer
with open('transformer.pkl', 'rb') as f:
transformer2 = pickle.load(f)