After you have built a machine learning model which is doing a great job in prediction, you don’t have to retrain your model again and again for future usage. Instead, you can use Python pickle serialization for reusing this model in future and transferring it into a production environment where non modelers can also use this model to make predictions.
First let’s look at how Wikipedia defines a pickle
Pickling is the process of preserving or expanding the lifespan of food by either anaerobic fermentation in brine or immersion in vinegar. The resulting food is called a pickle.
Python pickling is the same process without brine or vinegar, whereas you will pickle your model for longer usage without the need for you to recook your models. In a “Pickling” process a Python object is converted into a byte stream. On the other hand, in an “Unpickling” process a byte stream is converted back into Python object.
I strongly recommend that you read Python Official Documentation on this topic before moving forward.
Now let’s see this live in action. We will first look at a simple example and then look at a model example.
Example 1- In this we will pickle and un-pickle a simple Python list
Example 2- In this we will pickle and un-pickle a Decision Tree classifier and use it later for making predictions on a new data
For more details, do check out this excellent presentation.
One thought on “Model Persistence Using Python Pickle”
Pingback: Learn Data Science using Python Step by Step | RP's Blog on data science