Python HDF5 Example | Save a NumPy Array to HDF5 File

In this python hdf5 example, we will introduce how to save a numpy array into a hdf5 file. It is very useful to share data with others.

In order to save numpy array to hdf5 file, we should know:

  • How to create a numpy array?
  • How to create a hdf5 file in python?
  • How to save numpy to this hdf5 file?

We will answer all question one by one.

Step 1: create a numpy array

There are some methods to create a numpy array, in this example, we will use numpy.random.randint() to create one.

For example:

>>> import numpy as np
>>> A = np.random.randint(100, size=(4,4))
>>> A
array([[ 1, 99, 79, 46],
       [69,  4, 29, 60],
       [56, 94, 16, 16],
       [52, 13, 37, 86]])

Step 2: how to create a hdf5 file in python?

In python, we can use h5py package and use h5py.File() to create a hdf5 file.

For example:

import h5py
f1 = h5py.File("data.hdf5", "w")

Step 3: how to save numpy data to hdf5 file?

We can use create_dataset() function in h5py to save a numpy array to hdf5 file

For example:

dset1 = f1.create_dataset("dataset_01", (4,4), dtype='i', data=A)

In this example, we will save A above to hdf5 file. dataset_01 is the name of this numpy array in hdf5, we can use it to read data in A.

Python Example | Read a HDF5 File

We also can save some metadata for numpy array.

For example:

>>> dset1.attrs['scale'] = 0.01
>>> dset1.attrs['offset'] = 15

Finally, we will close this file

>>> f1.close()