Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to save sklearn model?

I am working on a python desktop app. This app does some predictions. Right now I train my sklearn model using python script, save the parameters of the model as a dictionary in a yaml file. Then, I build in this yaml into my python app. Then, when I am using the app, the model is recreated using parameters from the dictionary. I realized, that people who have a different version of sklearn get an error. I tried to save my model in a pickle file, but in this case, it produced some warning when app was running on a machine with a different version of sklearn.

like image 943
Ekaterina Tcareva Avatar asked Oct 05 '17 16:10

Ekaterina Tcareva


People also ask

Can I save Sklearn model?

We can save the model and later load the model to make predictions on unseen data. Pickle is used for serializing and de-serializing Python object structures also called marshalling or flattening.

How do you store machine learning models?

#1 Pickle. Pickle is one of the most popular ways to serialize objects in Python; You can use Pickle to serialize your trained machine learning model and save it to a file. At a later time or in another script, you can deserialize the file to access the trained model and use it to make predictions.


1 Answers

There is no guarantee that a given sklearn model would be compatible between versions of sklearn. Indeed, the implementation or the internal API may change between versions. See more informations here.

If you consider one version, the best way is indeed to pickle, and not to save the parameters in a yaml file. It's even better to use joblib to do so. See more informations here.

like image 133
TomDLT Avatar answered Oct 10 '22 02:10

TomDLT