Is it possible to annotate a numpy array when saving with savez

Suppose my program creates a large array of data, which I then save using the numpy savez procedure. However, I would also like to store additional information along with this array. Examples are the git commit tags of the current version and the input parameters used to generate the data, so that later I can view the data and know exactly how I created it.

Is there a way to save this information directly with the array in an npz file, or do I need to create a separate file?

+5
source share
2 answers

You must be able to:

In [2]: a = np.arange(10)

In [3]: b = 'git push'

In [5]: np.savez('file',a=a,b=b)

In [7]: data = np.load('file.npz')

In [8]: data.keys()
Out[8]: ['a', 'b']

In [9]: data['a']
Out[9]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [10]: str(data['b'])
Out[10]: 'git push'

, , . , , , - hdf5 h5py pytables:

http://h5py.alfven.org/docs/

http://www.pytables.org/

+4

(.npz ), , , - . (, @JoshAdel , .npz.)

HDF - - .

hdf .

h5py numpy hdf .

:

import numpy as np
import h5py

somearray = np.random.random(100)

f = h5py.File('test.hdf', 'w')

dataset = f.create_dataset('my_data', data=somearray)

# Store attributes about your dataset using dictionary-like access
dataset.attrs['git id'] = 'yay this is a string'

f.close()
+7
source

All Articles