Jupyter Snippet CB2nd 08_memmap

Jupyter Snippet CB2nd 08_memmap

4.8. Processing large NumPy arrays with memory mapping

import numpy as np
nrows, ncols = 1000000, 100
f = np.memmap('memmapped.dat', dtype=np.float32,
              mode='w+', shape=(nrows, ncols))
for i in range(ncols):
    f[:, i] = np.random.rand(nrows)
x = f[:, -1]
del f
f = np.memmap('memmapped.dat', dtype=np.float32,
              shape=(nrows, ncols))
np.array_equal(f[:, -1], x)
True
del f