Saving dictionary of numpy arrays
When saving a dictionary with numpy, the dictionary is encoded into an array. To have what you need, you can do as in this example:
my_dict = {'a' : np.array(range(3)), 'b': np.array(range(4))}
np.save('my_dict.npy', my_dict)
my_dict_back = np.load('my_dict.npy')
print(my_dict_back.item().keys())
print(my_dict_back.item().get('a'))
So you are probably missing .item()
for the reloaded dictionary.
Check this out:
for key, key_d in data2.item().items():
print key, key_d
The comparison my_dict == my_dict_back.item()
works only for dictionaries that does not have lists or arrays in their values.
EDIT: for the item()
issue mentioned above, I think it is a better option to save dictionaries with the library pickle
rather than with numpy
.
SECOND EDIT: if not happy with pickle, and all the types in the dictionary are compatible with , json
is an option as well.
I really liked the deepdish
(it saves them in HDF5
format):
>>> import deepdish as dd
>>> d = {'foo': np.arange(10), 'bar': np.ones((5, 4, 3))}
>>> dd.io.save('test.h5', d)
$ ddls test.h5
/bar array (5, 4, 3) [float64]
/foo array (10,) [int64]
>>> d = dd.io.load('test.h5')
for my experience, it seems to be partially broken for large datasets, though :(
Let's look at a small example:
In [819]: N
Out[819]:
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])
In [820]: data={'N':N}
In [821]: np.save('temp.npy',data)
In [822]: data2=np.load('temp.npy')
In [823]: data2
Out[823]:
array({'N': array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])}, dtype=object)
np.save
is designed to save numpy arrays. data
is a dictionary. So it wrapped it in a object array, and used pickle
to save that object. Your data2
probably has the same character.
You get at the array with:
In [826]: data2[()]['N']
Out[826]:
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])