Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
numpy.savez() / load() weirdness

Some weirdness about saving/loading some non-array/vector datatypes (int/str/float in numpy:

In [1]: import numpy as np

In [2]: np.savez('foo.npz', my_int=5, my_float=5.0, my_vec=np.array([0.2, 0.5, 0.9]), my_str='foo on
   ...:  the bar', my_dict={'foo': 'bar'}, my_list=[1, 2, 3])

In [3]: data = np.load('foo.npz')

In [4]: data.keys()
Out[4]: ['my_float', 'my_dict', 'my_int', 'my_list', 'my_vec', 'my_str']

In [5]: data['my_list']
Out[5]: array([1, 2, 3])

In [6]: data['my_list'].item(0)
Out[6]: 1

In [7]: data['my_list'].tolist()
Out[7]: [1, 2, 3]

In [8]: data['my_dict']
Out[8]: array({'foo': 'bar'}, dtype=object)

In [9]: data['my_dict'].item(0)
Out[9]: {'foo': 'bar'}

In [10]: data['my_int']
Out[10]: array(5)

In [11]: data['my_int'].item(0)
Out[11]: 5

In [12]: data['my_float']
Out[12]: array(5.0)

In [13]: data['my_float'].item(0)
Out[13]: 5.0

In [14]: data['my_str']
Out[14]: 
array('foo on the bar', 
      dtype='|S14')

In [15]: data['my_str'].item(0)
Out[15]: 'foo on the bar'

In [16]: data['my_vec']
Out[16]: array([ 0.2,  0.5,  0.9])

In [17]: data['my_vec'].item(0)
Out[17]: 0.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.