How to save classifier to disk in scikit-learn?

Machine Learning

Hi Guys,

I have trained the Naive Bayes classifier. I want to save this model for future use. How do I save a trained Naive Bayes classifier to disk and use it to predict data?



Classifiers are referred to as the objects that can be dumped and pickled like any other. You can use the below code which will help you to save the classifier to the disk in scikit-learn.

import cPickle

# saving the classifier

with open('my_dumped_classifier.pkl', 'wb') as fid:

    cPickle.dump(gnb, fid)    

# loading the classifier again

with open('my_dumped_classifier.pkl', 'rb') as fid:

    gnb_loaded = cPickle.load(fid)


If you want to unleash your potential in this competitive field, please visit the Machine Learning course page for more information, where you can find the Machine Learning tutorials and Machine Learning frequently asked interview questions and answers as well.


This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.
Protected by Astra Security