Using Pickle to save and load the model

After using Pickle to save and load the model, we create a function to predict the probability that a single customer will subscribe.

For example, we have the following information about a new customer:

customer = {'age': 57,
            'job': 'blue-collar',
            'marital': 'married',
            'education': 'primary',
            'balance': 856,
            'housing': 'no',
            'contact': 'cellular',
            'day': 11,
            'month': 'aug',
            'duration': 194,
            'campaign': 6,
            'pdays': -1,
            'previous': 0,
            'poutcome': 'unknown'}

We use the model and the transformer that create in previous chapters. To achieve our objective, we use the following function:

def predict_subcription_proba(customer, dv, model):
    X_customer = dv.transform(customer)
    return model.predict_proba(X_customer)[:, 1][0]
    
predict_subcription_proba(customer, dv, model)
#0.03186216688676335

Now, let’s imagine we want to use this model and transformer in another project. We need to export them, and to do this, we can use the Pickle package.

Pickle is a serialization/deserialization module that’s already built into Python: using it, we can save an arbitrary Python object (with a few exceptions) to a file. Once we have a file, we can load the model from there in a different process.

Using Pickle to save the model

To save the model:

import pickle
with open('subscribe-model.bin', 'wb') as f_out:
    pickle.dump(model, f_out)

This way, we save the model in a binary file called susbscribe-model.bin, which can be exported and imported across multiple projects.

Because the new data is not been pre-processed, we need to save the DictVectorizer transformer that we trained.

To save the transformer:

with open('dv.bin', 'wb') as f_out:
    pickle.dump(dv, f_out) 

Also, we can save them in a single file:

with open('subscribe-model.bin', 'wb') as f_out:
    pickle.dump((dv, model), f_out)

Using Pickle to load the model

To load the model: