47 python classification machine-learning nltk naivebayes
关于如何保存训练有素的分类器,我有点困惑.就像在每次我想要使用它时重新训练分类器显然是非常糟糕和缓慢的,我如何保存它并在需要时再次加载它?代码如下,提前感谢您的帮助.我正在使用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)
Run Code Online (Sandbox Code Playgroud)
Jac*_*cob 89
要保存:
import pickle
f = open('my_classifier.pickle', 'wb')
pickle.dump(classifier, f)
f.close()
Run Code Online (Sandbox Code Playgroud)
要稍后加载:
import pickle
f = open('my_classifier.pickle', 'rb')
classifier = pickle.load(f)
f.close()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18380 次 |
| 最近记录: |