我正在尝试从Scikit-learn库中挑选一个经过训练的SVM分类器,这样我就不必一遍又一遍地训练它。但是当我将测试数据传递给从泡菜加载的分类器时,我得到了异常高的精确度、f度量等值。如果将测试数据直接传递给未酸洗的分类器,则得到的值要低得多。我不明白为什么酸洗和取消酸洗分类器对象会改变它的行为方式。有人能帮我解决这个问题吗?
我正在做这样的事情:
from sklearn.externals import joblib
joblib.dump(grid, 'grid_trained.pkl')这里,grid是经过训练的分类器对象。当我解开它时,它的行为与直接使用时有很大的不同。
发布于 2014-11-20 07:15:02
正如@AndreasMueller所说,应该没有任何区别,这里是一个使用pickle的http://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html#loading-the-20-newgroups-dataset修改后的示例
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
# Set labels and data
categories = ['alt.atheism', 'soc.religion.christian', 'comp.graphics', 'sci.med']
twenty_train = fetch_20newsgroups(subset='train', categories=categories, shuffle=True, random_state=42)
# Vectorize data
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(twenty_train.data)
# TF-IDF transformation
tf_transformer = TfidfTransformer(use_idf=False).fit(X_train_counts)
X_train_tf = tf_transformer.transform(X_train_counts)
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
# Train classifier
clf = MultinomialNB().fit(X_train_tfidf, twenty_train.target)
# Tag new data
docs_new = ['God is love', 'OpenGL on the GPU is fast']
X_new_counts = count_vect.transform(docs_new)
X_new_tfidf = tfidf_transformer.transform(X_new_counts)
predicted = clf.predict(X_new_tfidf)
answers = [(doc, twenty_train.target_names[category]) for doc, category in zip(docs_new, predicted)]
# Pickle the classifier
import pickle
with open('clf.pk', 'wb') as fout:
pickle.dump(clf, fout)
# Let's clear the classifier
clf = None
with open('clf.pk', 'rb') as fin:
clf = pickle.load(fin)
# Retag new data
docs_new = ['God is love', 'OpenGL on the GPU is fast']
X_new_counts = count_vect.transform(docs_new)
X_new_tfidf = tfidf_transformer.transform(X_new_counts)
predicted = clf.predict(X_new_tfidf)
answers_from_loaded_clf = [(doc, twenty_train.target_names[category]) for doc, category in zip(docs_new, predicted)]
assert answers_from_loaded_clf == answers
print "Answers from freshly trained classifier and loaded pre-trained classifer are the same !!!"使用sklearn.externals.joblib时也是如此:
# Pickle the classifier
from sklearn.externals import joblib
joblib.dump(clf, 'clf.pk')
# Let's clear the classifier
clf = None
# Loads the pretrained classifier
clf = joblib.load('clf.pk')
# Retag new data
docs_new = ['God is love', 'OpenGL on the GPU is fast']
X_new_counts = count_vect.transform(docs_new)
X_new_tfidf = tfidf_transformer.transform(X_new_counts)
predicted = clf.predict(X_new_tfidf)
answers_from_loaded_clf = [(doc, twenty_train.target_names[category]) for doc, category in zip(docs_new, predicted)]
assert answers_from_loaded_clf == answers
print "Answers from freshly trained classifier and loaded pre-trained classifer are the same !!!"https://stackoverflow.com/questions/27019285
复制相似问题