![]() |
VOOZH | about |
In this article, let's learn how to save and load your machine learning model in Python with scikit-learn in this tutorial.
Once we create a machine learning model, our job doesn't end there. We can save the model to use in the future. We can either use the pickle or the joblib library for this purpose. The dump method is used to create the model and the load method is used to load and use the dumped model. Now let's demonstrate how to do it. The save and load methods of both pickle and joblib have the same parameters.
syntax of dump() method:
pickle.dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None)
parameters:
- obj: The pickled Python object.
- file: The pickled object will be written to a file or buffer.
- fix_imports: When supplied, the method dump() will determine if the pickling procedure should be compatible with Python version 2 or not based on the value for the pickle protocol option. True is the default value. Only a name-value pair should be used with this default parameter.
syntax of load() method:
pickle.load(file, *, fix_imports=True, encoding='ASCII', errors='strict', buffers=None)
The load() method Returns the rebuilt object hierarchy indicated therein after reading the pickled representation of an object from the open file object file.
Python's default method for serializing objects is a pickle. Your machine learning algorithms can be serialized/encoded using the pickling process, and the serialized format can then be saved to a file. When you want to deserialize/decode your model and utilize it to produce new predictions, you can load this file later. The training of a linear regression model is shown in the example that follows. In the below example we fit the data with train data and the dump() method is used to create a model. The dump method takes in the machine learning model and a file is given. The test data is used to find predictions after loading the model using the load() method. root mean square error metric is used to evaluate the predictions of the model.
Output:
root mean squared error : 72.11529287182815
The SciPy ecosystem includes Joblib, which offers tools for pipelining Python jobs. It offers tools for effectively saving and loading Python objects that employ NumPy data structures. This can be helpful for machine learning algorithms that need to store the complete dataset or have a lot of parameters. let's look at a simple example where we save and load a linear regression model. The same steps are repeated while using the joblib library.
Output:
root mean squared error : 72.11529287182815