numpy.load#
- numpy.load(file,mmap_mode=None,allow_pickle=False,fix_imports=True,encoding='ASCII',*,max_header_size=10000)[source]#
Load arrays or pickled objects from
.npy,.npzor pickled files.Warning
Loading files that contain object arrays uses the
picklemodule, which is not secure against erroneous or maliciouslyconstructed data. Consider passingallow_pickle=Falsetoload data that is known not to contain object arrays for thesafer handling of untrusted sources.- Parameters:
- filefile-like object, string, or pathlib.Path
The file to read. File-like objects must support the
seek()andread()methods and must alwaysbe opened in binary mode. Pickled files require that thefile-like object support thereadline()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.memmapfor a detailed description of the modes). Amemory-mapped array is kept on disk. However, it can be accessedand sliced like any ndarray. Memory mapping is especially usefulfor accessing small fragments of large files without reading theentire file into memory.- allow_picklebool, optional
Allow loading pickled object arrays stored in npy files. Reasons fordisallowing pickles include security, as loading pickled data canexecute arbitrary code. If pickles are disallowed, loading objectarrays will fail. Default: False
- fix_importsbool, optional
Only useful when loading Python 2 generated pickled files,which includes npy/npz files containing object arrays. Iffix_importsis True, pickle will try to map the old Python 2 names to the new namesused in Python 3.
- encodingstr, optional
What encoding to use when reading Python 2 strings. Only useful whenloading Python 2 generated pickled files, which includesnpy/npz files containing object arrays. Values other than ‘latin1’,‘ASCII’, and ‘bytes’ are not allowed, as they can corrupt numericaldata. Default: ‘ASCII’
- max_header_sizeint, optional
Maximum allowed size of the header. Large headers may not be safeto load securely and thus require explicitly passing a larger value.See
ast.literal_evalfor details.This option is ignored whenallow_pickle is passed. In that casethe file is by definition trusted and the limit is unnecessary.
- Returns:
- resultarray, tuple, dict, etc.
Data stored in the file. For
.npzfiles, the returned instanceof NpzFile class must be closed to avoid leaking file descriptors.
- 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=Falsegiven.- EOFError
When calling
np.loadmultiple times on the same file handle,if all data has already been read
See also
save,savez,savez_compressed,loadtxtmemmapCreate a memory-map to an array stored in a file on disk.
lib.format.open_memmapCreate or load a memory-mapped
.npyfile.
Notes
If the file contains pickle data, then whatever object is storedin 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 isreturned, containing{filename:array}key-value pairs, one foreach file in the archive.If the file is a
.npzfile, the returned value supports thecontext manager protocol in a similar fashion to the open function:withload('foo.npz')asdata:a=data['a']
The underlying file descriptor is closed when exiting the ‘with’block.
Examples
>>>importnumpyasnp
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 rowdirectly from disk:
>>>X=np.load('/tmp/123.npy',mmap_mode='r')>>>X[1,:]memmap([4, 5, 6])