numpy 1.22.4 Pypi GitHub Homepage
Other Docs
NotesParametersRaisesReturnsBackRef
load(file, mmap_mode=None, allow_pickle=False, fix_imports=True, encoding='ASCII')
warning

module, which is not secure against erroneous or maliciously constructed data. Consider passing allow_pickle=False to load data that is known not to contain object arrays for the safer handling of untrusted sources.

Notes

Parameters

file : file-like object, string, or pathlib.Path

The file to read. File-like objects must support the seek() and read() methods. Pickled files require that the file-like object support the readline() method as well.

mmap_mode : {None, 'r+', 'r', 'w+', 'c'}, optional

If not None, then memory-map the file, using the given mode (see numpy.memmap for a detailed description of the modes). A memory-mapped array is kept on disk. 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.

allow_pickle : bool, optional

Allow loading pickled object arrays stored in npy files. Reasons for disallowing pickles include security, as loading pickled data can execute arbitrary code. If pickles are disallowed, loading object arrays will fail. Default: False

versionchanged

Made default False in response to CVE-2019-6446.

fix_imports : bool, optional

Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays. If :None:None:`fix_imports` is True, pickle will try to map the old Python 2 names to the new names used in Python 3.

encoding : str, optional

What encoding to use when reading Python 2 strings. Only useful when loading Python 2 generated pickled files in Python 3, which includes npy/npz files containing object arrays. Values other than 'latin1', 'ASCII', and 'bytes' are not allowed, as they can corrupt numerical data. Default: 'ASCII'

Raises

OSError

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

UnpicklingError

If allow_pickle=True , but the file cannot be loaded as a pickle.

ValueError

The file contains an object array, but allow_pickle=False given.

Returns

result : array, tuple, dict, etc.

Data stored in the file. For .npz files, the returned instance of NpzFile class must be closed to avoid leaking file descriptors.

Load arrays or pickled objects from .npy , .npz or pickled files.

See Also

lib.format.open_memmap

Create or load a memory-mapped .npy file.

loadtxt
memmap

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

save
savez
savez_compressed

Examples

Store data to disk, and load it again:

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

Store compressed data to disk, and load it again:

>>> a=np.array([[1, 2, 3], [4, 5, 6]])
... b=np.array([1, 2])
... np.savez('/tmp/123.npz', a=a, b=b)
... data = np.load('/tmp/123.npz')
... data['a'] array([[1, 2, 3], [4, 5, 6]])
>>> data['b']
array([1, 2])
>>> data.close()

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])
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

numpy.fromfile scipy.sparse._matrix_io.load_npz numpy.loadtxt numpy.savez_compressed matplotlib.cbook.get_sample_data numpy.savez numpy.save

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /numpy/lib/npyio.py#272
type: <class 'function'>
Commit: