本文介绍了在 NLTK 中保存朴素贝叶斯训练的分类器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何保存训练有素的分类器有点困惑.就像在每次我想使用分类器时重新训练它显然非常糟糕和缓慢,我如何保存它并在需要时再次加载它?代码如下,在此先感谢您的帮助.我将 Python 与 NLTK 朴素贝叶斯分类器一起使用.

I'm slightly confused in regard to how I save a trained classifier. As in, re-training a classifier each time I want to use it is obviously really bad and slow, how do I save it and the load it again when I need it? Code is below, thanks in advance for your help. I'm using Python with NLTK Naive Bayes Classifier.

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()

这篇关于在 NLTK 中保存朴素贝叶斯训练的分类器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 02:47