Save classifier to disk in scikit-learn

garak :

How do I save a trained Naive Bayes classifier to disk and use it to predict data?

I have the following sample program from the scikit-learn website:

from sklearn import datasets
iris = datasets.load_iris()
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
y_pred = gnb.fit(iris.data, iris.target).predict(iris.data)
print "Number of mislabeled points : %d" % (iris.target != y_pred).sum()
mwv :

Classifiers are just objects that can be pickled and dumped like any other. To continue your example:

import cPickle
# save the classifier
with open('my_dumped_classifier.pkl', 'wb') as fid:
    cPickle.dump(gnb, fid)    

# load it again
with open('my_dumped_classifier.pkl', 'rb') as fid:
    gnb_loaded = cPickle.load(fid)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Save classifier to postrgesql database, in scikit-learn

scikit learn averaged perceptron classifier

how to save a scikit-learn pipline with keras regressor inside to disk?

Evaluating convergence of SGD classifier in scikit learn

ROC curve for discrete classifier using scikit learn

Python scikit-learn: exporting trained classifier

Save and reuse TfidfVectorizer in scikit learn

What is the theorical foundation for scikit-learn dummy classifier?

What is the classifier used in scikit-learn's VotingClassifier?

Can scikit-learn 'dummy classifier' be applied to multiclass scenario

Is Scikit Learn's Support Vector Classifier hard margin or soft margin

Plot Confusion Matrix with scikit-learn without a Classifier

How to upgrade the classifier to the latest version of scikit-learn

How to get all alpha values of scikit-learn SVM classifier?

'Multiclass-multioutput is not supported' Error in Scikit learn for Knn classifier

Converting JPG images for input to scikit learn SVM classifier

Post-process classifier output in scikit learn Pipeline

scikit-learn get certainty of classification / score of the classifier for the chosen category

Why is scikit-learn SVM classifier cross validation so slow?

Scikit-learn classifier with custom scorer dependent on a training feature

How to test unseen sentences for a new classifier in scikit learn

Scikit-learn Ridge classifier: extracting class probabilities

Save scikit-learn model without datasets

How to save a randomforest in scikit-learn?

How to use a fixed validation set (not K-fold cross validation) in Scikit-learn for a decision tree classifier/random forest classifier?

Cannot pickle Scikit learn NearestNeighbor classifier - can't pickle instancemethod objects

Fitting a Support Vector Classifier in scikit-learn with image data produces error

How can I perform ensemble (multi-classifier) classification using scikit-learn?

How to build my training data in my case to train a SVM in classifier in scikit-learn?