How to save numpy masked array to file

import numpy as np
a = np.ma.zeros((500, 500))
a.dump('test')

then read it with

a = np.load('test')

The current accepted answer is somewhat obsolete, and badly inefficient if the array being stored is sparse (it relies on uncompressed pickling of the array).

A better way to save/load a masked array would be to use an npz file:

import numpy as np

# Saving masked array 'arr':
np.savez_compressed('test.npz', data=arr.data, mask=arr.mask)

# Loading array back
with np.load('test.npz') as npz:
    arr = np.ma.MaskedArray(**npz)

Tags:

Python

Numpy