3.4. Model persistence

After training a scikit-learn model, it is desirable to have a way to persistthe model for future use without having to retrain. The following section givesyou an example of how to persist a model with pickle. We’ll also review a fewsecurity and maintainability issues when working with pickle serialization.

An alternative to pickling is to export the model to another format using oneof the model export tools listed under Related Projects. Unlikepickling, once exported you cannot recover the full Scikit-learn estimatorobject, but you can deploy the model for prediction, usually by using toolssupporting open model interchange formats such as ONNX orPMML.

3.4.1. Persistence example

It is possible to save a model in scikit-learn by using Python’s built-inpersistence model, namely pickle:

>>>

  1. >>> from sklearn import svm
  2. >>> from sklearn import datasets
  3. >>> clf = svm.SVC()
  4. >>> X, y= datasets.load_iris(return_X_y=True)
  5. >>> clf.fit(X, y)
  6. SVC()
  7.  
  8. >>> import pickle
  9. >>> s = pickle.dumps(clf)
  10. >>> clf2 = pickle.loads(s)
  11. >>> clf2.predict(X[0:1])
  12. array([0])
  13. >>> y[0]
  14. 0

In the specific case of scikit-learn, it may be better to use joblib’sreplacement of pickle (dump & load), which is more efficient onobjects that carry large numpy arrays internally as is often the case forfitted scikit-learn estimators, but can only pickle to the disk and not to astring:

>>>

  1. >>> from joblib import dump, load
  2. >>> dump(clf, 'filename.joblib')

Later you can load back the pickled model (possibly in another Python process)with:

>>>

  1. >>> clf = load('filename.joblib')

Note

dump and load functions also accept file-like objectinstead of filenames. More information on data persistence with Joblib isavailable here.

3.4.2. Security & maintainability limitations

pickle (and joblib by extension), has some issues regarding maintainabilityand security. Because of this,

  • Never unpickle untrusted data as it could lead to malicious code beingexecuted upon loading.

  • While models saved using one version of scikit-learn might load inother versions, this is entirely unsupported and inadvisable. It shouldalso be kept in mind that operations performed on such data could givedifferent and unexpected results.

In order to rebuild a similar model with future versions of scikit-learn,additional metadata should be saved along the pickled model:

  • The training data, e.g. a reference to an immutable snapshot

  • The python source code used to generate the model

  • The versions of scikit-learn and its dependencies

  • The cross validation score obtained on the training data

This should make it possible to check that the cross-validation score is in thesame range as before.

Since a model internal representation may be different on two differentarchitectures, dumping a model on one architecture and loading it onanother architecture is not supported.

If you want to know more about these issues and explore other possibleserialization methods, please refer to thistalk by Alex Gaynor.