bundles / numpy 2.4.4 / numpy / load
function
numpy:load
source: /numpy/lib/_npyio_impl.py :311
Signature
def load ( file , mmap_mode = None , allow_pickle = False , fix_imports = True , encoding = ASCII , * , max_header_size = 10000 ) Summary
Load arrays or pickled objects from .npy, .npz or pickled files.
Extended Summary
Parameters
file: file-like object, string, or pathlib.PathThe file to read. File-like objects must support the
seek()andread()methods and must always be opened in binary mode. Pickled files require that the file-like object support thereadline()method as well.mmap_mode: {None, 'r+', 'r', 'w+', 'c'}, optionalIf 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, optionalAllow 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
fix_imports: bool, optionalOnly useful when loading Python 2 generated pickled files, which includes npy/npz files containing object arrays. If
fix_importsis True, pickle will try to map the old Python 2 names to the new names used in Python 3.encoding: str, optionalWhat encoding to use when reading Python 2 strings. Only useful when loading Python 2 generated pickled files, 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'
max_header_size: int, optionalMaximum allowed size of the header. Large headers may not be safe to load securely and thus require explicitly passing a larger value. See
ast.literal_eval()for details. This option is ignored whenallow_pickleis passed. In that case the file is by definition trusted and the limit is unnecessary.
Returns
result: array, tuple, dict, etc.Data stored in the file. For
.npzfiles, the returned instance of NpzFile class must be closed to avoid leaking file descriptors.
Raises
: OSErrorIf the input file does not exist or cannot be read.
: UnpicklingErrorIf
allow_pickle=True, but the file cannot be loaded as a pickle.: ValueErrorThe file contains an object array, but
allow_pickle=Falsegiven.: EOFErrorWhen calling
np.loadmultiple times on the same file handle, if all data has already been read
Notes
If the file contains pickle data, then whatever object is stored in the pickle is returned.
If the file is a
.npyfile, then a single array is returned.If the file is a
.npzfile, then a dictionary-like object is returned, containing{filename: array}key-value pairs, one for each file in the archive.If the file is a
.npzfile, the returned value supports the context manager protocol in a similar fashion to the open functionwith load('foo.npz') as data: a = data['a']
The underlying file descriptor is closed when exiting the 'with' block.
Examples
import numpy as np
✓np.save('/tmp/123', np.array([[1, 2, 3], [4, 5, 6]])) np.load('/tmp/123.npy')✓
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'] data['b'] data.close()✓
X = np.load('/tmp/123.npy', mmap_mode='r') X[1, :]✓
See also
- lib.format.open_memmap
Create or load a memory-mapped
.npyfile.- loadtxt
- memmap
Create a memory-map to an array stored in a file on disk.
- save
- savez
- savez_compressed
Aliases
-
numpy.load