QBoard » Artificial Intelligence & ML » AI and ML - Python » How to save classifier to disk in scikit-learn

How to save classifier to disk in scikit-learn

  • 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()
      November 9, 2020 2:40 PM IST
    0
  • You can also use joblib.dump and joblib.load which is much more efficient at handling numerical arrays than the default python pickler.

    Joblib is included in scikit-learn:

    >>> import joblib
    >>> from sklearn.datasets import load_digits
    >>> from sklearn.linear_model import SGDClassifier
    
    >>> digits = load_digits()
    >>> clf = SGDClassifier().fit(digits.data, digits.target)
    >>> clf.score(digits.data, digits.target)  # evaluate training error
    0.9526989426822482
    
    >>> filename = '/tmp/digits_classifier.joblib.pkl'
    >>> _ = joblib.dump(clf, filename, compress=9)
    
    >>> clf2 = joblib.load(filename)
    >>> clf2
    SGDClassifier(alpha=0.0001, class_weight=None, epsilon=0.1, eta0=0.0,
           fit_intercept=True, learning_rate='optimal', loss='hinge', n_iter=5,
           n_jobs=1, penalty='l2', power_t=0.5, rho=0.85, seed=0,
           shuffle=False, verbose=0, warm_start=False)
    >>> clf2.score(digits.data, digits.target)
    0.9526989426822482

     

    Edit: in Python 3.8+ it's now possible to use pickle for efficient pickling of object with large numerical arrays as attributes if you use pickle protocol 5 (which is not the default).

     
      December 1, 2021 2:56 PM IST
    0
  • 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)
      November 9, 2020 2:42 PM IST
    0
  • What you are looking for is called Model persistence in sklearn words and it is documented in introduction and in model persistence sections.

    So you have initialized your classifier and trained it for a long time with

    clf = some.classifier()
    clf.fit(X, y)
     

    After this you have two options:

    1) Using Pickle

    import pickle
    # now you can save it to a file
    with open('filename.pkl', 'wb') as f:
        pickle.dump(clf, f)
    
    # and later you can load it
    with open('filename.pkl', 'rb') as f:
        clf = pickle.load(f)
     

    2) Using Joblib

    from sklearn.externals import joblib
    # now you can save it to a file
    joblib.dump(clf, 'filename.pkl')
    # and later you can load it
    clf = joblib.load('filename.pkl')
      November 9, 2020 2:43 PM IST
    0
  • Use pickle. dump() to save a classifier to disk

    dump(obj, file) with the classifier as obj and the desired filename as file to save the classifier to disk as the filename. To load the classifier from the file back into a Python object, call pickle. dump(file) with the filename that the classifier was saved to as file .
      November 24, 2021 12:06 PM IST
    0