I have a dict of arrays
{1:array([...]), 2:array([...]), 3:array([...])}
I'd like to save it to a file and load it back later.
I found a numpy had a list of Input and output methods, but seems they only deal with arrays.
Thanks.
The following script will save an dict of numpy arrays to disk and then load it back in to memory.
import numpy as np
arr1 = np.arange(0, 10, 1)
arr2 = np.arange(10, 20, 1)
arr3 = np.arange(20, 30, 1)
dct = {'1': arr1, '2': arr2, '3':arr3}
outfile = 'dict_of_arrays.npz'
np.savez(outfile, **dct)
npzfile = np.load(outfile)
print('npzfile.files: {}'.format(npzfile.files))
print('npzfile["1"]: {}'.format(npzfile["1"]))
Running this script shows the following:
npzfile.files: ['1', '3', '2']
npzfile["1"]: [0 1 2 3 4 5 6 7 8 9]
Note that your dict keys must be strings. Perhaps that was the problem?
I am running numpy 1.10.4
Actually you can use built-in pickle
library to serialize and deserialize your objects without using numpy.
Here is a mock code
import pickle
data1 = {'a': [1, 2.0, 3, 4 + 6j],
'b': ('string', u'Unicode string'),
'c': None}
print data1, type(data1)
with open('data.pkl', 'wb') as output:
# Pickle dictionary using protocol 0.
pickle.dump(data1, output)
# load data from pkl file
with open("data.pkl", "rb") as fp:
loaded_data1 = pickle.load(fp)
print loaded_data1, type(loaded_data1)
Results
Before: {'a': [1, 2.0, 3, (4+6j)], 'c': None, 'b': ('string', u'Unicode string')} <type 'dict'>
After: {'a': [1, 2.0, 3, (4+6j)], 'c': None, 'b': ('string', u'Unicode string')} <type 'dict'>
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With