Saving and Loading Models
After a model has been built and its performance matches our expectations, we may want to save it for future use. This eliminates the time needed for rebuilding it. Models can be saved on the hard disk using the joblib
and pickle
libraries.
The pickle
module makes use of binary protocols to save and load Python objects. joblib
makes use of the pickle
library protocols, but it improves on them to provide an efficient replacement to save large Python objects. Both libraries have two main functions that we will make use of to save and load our models:
dump
: This function is used to save a Python object to a file on the disk.loads
: This function is used to load a saved Python object from a file on the disk.
To deploy saved models, we need to load them from the hard disk to the memory. In the next section, we will perform an exercise based on this to get a better understanding of this process.