numpy.load(file, mmap_mode=None)

Load a pickled, .npy, or .npz binary file.

Parameters :

file : file-like object or string

The file to read. It must support seek() and read() methods. If the filename extension is .gz, the file is first decompressed.

mmap_mode: {None, ‘r+’, ‘r’, ‘w+’, ‘c’}, optional :

If not None, then memory-map the file, using the given mode (see numpy.memmap). The mode has no effect for pickled or zipped files. A memory-mapped array is stored on disk, and not directly loaded into memory. However, it can be accessed and sliced like any ndarray. Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory.

Returns :

result : array, tuple, dict, etc.

Data stored in the file.

Raises :

IOError :

If the input file does not exist or cannot be read.

See also

save, savez, loadtxt

Create a memory-map to an array stored in a file on disk.


  • If the file contains pickle data, then whatever is stored in the pickle is returned.
  • If the file is a .npy file, then an array is returned.
  • If the file is a .npz file, then a dictionary-like object is returned, containing {filename: array} key-value pairs, one for each file in the archive.


Store data to disk, and load it again:

>>>'/tmp/123', np.array([[1, 2, 3], [4, 5, 6]]))
>>> np.load('/tmp/123.npy')
array([[1, 2, 3],
       [4, 5, 6]])

Mem-map the stored array, and then access the second row directly from disk:

>>> X = np.load('/tmp/123.npy', mmap_mode='r')
>>> X[1, :]
memmap([4, 5, 6])

Next topic

This Page