When we train a model, it would be nice if we could save it as a file so that it can be used later by simply loading it again.
Achieving model persistence
Getting ready
Let's see how to achieve model persistence programmatically. To do this, the pickle module can be used. The pickle module is used to store Python objects. This module is a part of the standard library with your installation of Python.
How to do it...
Let's see how to achieve model persistence in Python:
- Add the following lines to the regressor.py file:
import pickle
output_model_file = "3_model_linear_regr.pkl"
with open(output_model_file, 'wb') as f:
pickle.dump(linear_regressor, f)
- The regressor object will be saved in the saved_model.pkl file. Let's look at how to load it and use it, as follows:
with open(output_model_file, 'rb') as f:
model_linregr = pickle.load(f)
y_test_pred_new = model_linregr.predict(X_test)
print("New mean absolute error =", round(sm.mean_absolute_error(y_test, y_test_pred_new), 2))
The following result is returned:
New mean absolute error = 241907.27
Here, we just loaded the regressor from the file into the model_linregr variable. You can compare the preceding result with the earlier result to confirm that it's the same.
How it works...
The pickle module transforms an arbitrary Python object into a series of bytes. This process is also called the serialization of the object. The byte stream representing the object can be transmitted or stored, and subsequently rebuilt to create a new object with the same characteristics. The inverse operation is called unpickling.
There's more...
In Python, there is also another way to perform serialization, by using the marshal module. In general, the pickle module is recommended for serializing Python objects. The marshal module can be used to support Python .pyc files.
See also
- Python's official documentation of the pickle module: https://docs.python.org/3/library/pickle.html