2
$\begingroup$

I'm working at building a decision tree model that will be used in production.

In documentation here pickle is used to serialize the model however the concerns about this technique make me think there's maybe a better solution to export a model to production.

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

  • Never unpickle untrusted data as it could lead to malicious code being executed upon loading.
  • While models saved using one version of scikit-learn might load in other versions, this is entirely unsupported and inadvisable. It should also be kept in mind that operations performed on such data could give different and unexpected results.

So my question is : Using scikit-learn, is there a safe and convenient technique to export the model into production.

PS: Converting the dot data to a python function can be a solution but i'm surprised there's no built-in solution for this.

$\endgroup$
4
  • $\begingroup$ Why exactly is pickling the model off the table? Are there any concerns around speed of inference, frequency of model updates? $\endgroup$
    – Tom M.
    Commented Jun 22, 2018 at 17:37
  • $\begingroup$ Pickling makes the model dependant of the python version or of sklearn-version so i wonder if a version independant solution exists $\endgroup$
    – Bertrand
    Commented Jun 22, 2018 at 19:47
  • $\begingroup$ You could consider exporting the tree with tree.export as exemplified here. $\endgroup$
    – mapto
    Commented Jun 22, 2018 at 20:26
  • $\begingroup$ Indeed @mapto this is a possible solution we explore but i expected something more "built-in" $\endgroup$
    – Bertrand
    Commented Jun 22, 2018 at 20:32

1 Answer 1

1
$\begingroup$

Persisting the model parameters is the only out of the box solution in sklearn. You should ensure that:

  1. Your model is reproducible from data
  2. The versions of dependencies used (incl sklearn) is locked
  3. Your QA system checks correctness of your model

If you have these things down then the possible changes in model persistence in sklearn is a non-issue. If it happens, you just update sklearn in a controlled manner, retrain and deploy as usual. And you avoid a whole range of other problems, for instance bugs in sklearn/numpy/whatever catching you by suprise. Or new data needs new models trained and deployed.

You can of course build your own persistence mechanism but you then need to maintain the compatibility. Better spend the effort one the above.

$\endgroup$
2
  • $\begingroup$ Thx for your answer jonnor. This seems to be the consensus :) $\endgroup$
    – Bertrand
    Commented Jun 25, 2018 at 8:36
  • $\begingroup$ You're welcome. Will you mark the answer as accepted? $\endgroup$
    – Jon Nordby
    Commented Jun 25, 2018 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.