SerializationΒΆ

The good thing since we are working with Python, is that we can trivially save our models (any model) to binary files and load them in any other machine. This is achived through pickle or cPickle. cPickle is used in exactly the same way as pickle (you just change the name...), so we will only work with pickle here. Use cPickle when pickle becomes too slow or runs out of memory.

Now, assume that we are working with Best and we have an object obj (this can be a model, a function, etc.) and that we want to save it to a file. Here is how we do it:

import pickle
with open('foo.bin', 'wb') as fd:
    pickle.dump(obj, fd)

Now, when you want to load it, you just do:

with open('foo.bin', 'rb') as fd:
    obj = pickle.load(fd)

Previous topic

Linear Algebra

Next topic

Maps

This Page