Save Numpy Array using Pickle
I have no problems using pickle
:
In [126]: arr = np.zeros((1000,2))In [127]: with open('test.pkl','wb') as f: ...: pickle.dump(arr, f) ...: In [128]: with open('test.pkl','rb') as f: ...: x = pickle.load(f) ...: print(x.shape) ...: ...: (1000, 2)
pickle
and np.save/load
have a deep reciprocity. Like I can load this pickle with np.load
:
In [129]: np.load('test.pkl').shapeOut[129]: (1000, 2)
If I open the pickle file in the wrong I do get your error:
In [130]: with open('test.pkl','wb') as f: ...: x = pickle.load(f) ...: print(x.shape) ...: UnsupportedOperation: read
But that shouldn't be surprising - you can't read a freshly opened write file. It will be empty.
np.save/load
is the usual pair for writing numpy arrays. But pickle uses save
to serialize arrays, and save
uses pickle to serialize non-array objects (in the array). Resulting file sizes are similar. Curiously in timings the pickle version is faster.
It's been a bit but if you're finding this, Pickle completes in a fraction of the time.
with open('filename','wb') as f: pickle.dump(arrayname, f)with open('filename','rb') as f: arrayname1 = pickle.load(f)numpy.array_equal(arrayname,arrayname1) #sanity check
On the other hand, by default numpy compress took my 5.2GB down to .4GB and Pickle went to 1.7GB.