Python pickle

Python pickle #

Python pickle for object seralization. Often used for machine learning models.

Module #

import pickle

Serialize pickle #

Let’s say the thing we want to pickle is a linear regression model.

model = LinearRegression()
model.fit(X,y)

Time to pickle, place model in place of <object>:

pickle.dump(<object>, '<file>.pkl')

To make it a bit more explicit, where wb means write, binary:

pickle.dump(<object>, open('<file>.pkl', 'wb'))

To serialize to a bytes string format:

pickle.dumps(<object>, '<file>')

Load pickle #

Now that we have a pickle, we can load it up elsewhere:

<object> = pickle.load('<file>.pkl')

More explicitly, where rb is read, binary:

object = pickle.load(open('<file>', 'rb'))

To load from a bytes string format:

<object> = pickle.loads('<file>.pkl')

Reading Python2 pickles into Python3 #

There are Python2 pickles out there. Python3 is what should be used today.

Handle Python2 pickles in Python3 with encoding='latin1'

<object> = pickle.load('<file>.pkl', encoding = 'latin1')

Resources #