How to save variables to a .pickle file:
1 2 3 |
import pickle with open(filename, ‘wb’) as f: pickle.dump(your_content, f) |
Example: here’s how to save training datasets into a pickle file for machine learning:
1 2 |
with open('train.pickle', 'wb') as f: pickle.dump([X_train, y_train], f) |
How to load variables from a .pickle file:
1 2 |
with open(filename, ‘rb’) as f: var_you_want_to_load_into = pickle.load(f) |
This is often called ‘unpickling’ a pickle file. The r stands for read and the b stands for binary mode (for non-text files).
Example: here’s how to retrieve training datasets from a pickle file for machine learning:
1 2 |
with open('train.pickle', 'rb') as f: X_train, y_train = pickle.load(f) |
Aside: ‘w’ vs ‘wb’
The ‘b’ in indicates we want to open the file for writing in binary mode. This matters only for dealing with non-text files on Windows machines, where text files are written with slightly modified line endings.
So we could use only (vs Unix or Linux machines) open(filename, 'w') for all files on Unix or Linux machines, but for compatibility it’s always safer to use open(filename, 'wb') for non-text files and open(filename, 'w') for text files.
References: