关于如何保存经过训练的分类器,我有些困惑。就像在其中一样,每次我想使用分类器时都要对其进行重新训练显然很糟糕而且很慢,如何保存它并在需要时再次加载它?代码如下,在此先感谢您的帮助。我正在将Python与NLTK朴素贝叶斯分类器一起使用。
classifier = nltk.NaiveBayesClassifier.train(training_set) # look inside the classifier train method in the source code of the NLTK library def train(labeled_featuresets, estimator=nltk.probability.ELEProbDist): # Create the P(label) distribution label_probdist = estimator(label_freqdist) # Create the P(fval|label, fname) distribution feature_probdist = {} return NaiveBayesClassifier(label_probdist, feature_probdist)
保存:
import pickle f = open('my_classifier.pickle', 'wb') pickle.dump(classifier, f) f.close()
稍后加载:
import pickle f = open('my_classifier.pickle', 'rb') classifier = pickle.load(f) f.close()