Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
3 views
in Python by (320 points)
How can I save a trained Naive Bayes classifier to a disk and use it for predicting data?

1 Answer

+2 votes
by (10.9k points)
edited by

You have to initialize your classifier first and then train it for a long time with

clf= some.classifier()

clf.fit(x,y)

After this you can opt for any of the two options-

1. Using Pickle

import pickle

with open('file.pkl', 'wb') as f:

   pickle.dump(clf, f)

with open('file.pkl', 'rb') as f:

   clf = pickle.load(f)

2. Using Joblib

from sklearn.externals import joblib

joblib.dump(clf, 'filename.pkl')

clf = joblib.load('filename.pkl')

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...