Can I store a dictionary using np.savez? The results are surprising (to me at least) and I cannot find a way to get my data back by key.
In [1]: a = {'0': {'A': array([1,2,3]), 'B': array([4,5,6])}}
In [2]: a
Out[2]: {'0': {'A': array([1, 2, 3]), 'B': array([4, 5, 6])}}
In [3]: np.savez('model.npz', **a)
In [4]: a = np.load('model.npz')
In [5]: a
Out[5]: <numpy.lib.npyio.NpzFile at 0x7fc9f8acaad0>
In [6]: a['0']
Out[6]: array({'B': array([4, 5, 6]), 'A': array([1, 2, 3])}, dtype=object)
In [7]: a['0']['B']
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-16-c916b98771c9> in <module>()
----> 1 a['0']['B']
ValueError: field named B not found
In [8]: dict(a['0'])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-17-d06b11e8a048> in <module>()
----> 1 dict(a['0'])
TypeError: iteration over a 0-d array
I do not understand exactly what is going on. It seems that my data becomes a dictionary inside a 0-dimensional array, leaving me with no way to get my data back by key. Or am I missing something?
So my questions are:
- What happens here? If I can still access my data by key, how?
- What is the best way to store data of this type? (a dict with str as key and other dicts as value)
Thanks!