w3resource

NumPy Input and Output: load() function

numpy.load() function

The load() function is used to load arrays or pickle objects from .npy, .npz or pickled files.

Syntax:

numpy.load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding='ASCII')

Version: 1.15.0

Parameter:

Name Description Required /
Optional
file 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.
file-like object, string, or pathlib.Path
Required
mmap_mode 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.
{None, ‘r+’, ‘r’, ‘w+’, ‘c’}
Optional
allow_pickle 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: True
bool
Optional
fix_imports Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays.
If fix_imports is True, pickle will try to map the old Python 2 names to the new names used in Python 3.
bool
Optional
encoding What encoding to use when reading Python 2 strings. Only useful when loading Python 2 generated pickled files on 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’
str
Optional

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.

Raises:

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

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

Notes:

  • If the file contains pickle data, then whatever object is stored in the pickle is returned.
  • If the file is a .npy file, then a single 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.
  • If the file is a .npz file, the returned value supports the context manager protocol in a similar fashion to the open function:
    with load('foo.npz') as data:
        a = data['a']

The underlying file descriptor is closed when exiting the ‘with’ block.

NumPy.load() method Example-1:

Store data to disk, and load it again:

>>> import numpy as np
>>> np.save('/tmp/123', np.array([[2, 4, 6], [8, 10, 12]]))
>>> np.load('/tmp/123.npy')

Output:

array([[ 2,  4,  6],
       [ 8, 10, 12]])

NumPy.load() method Example-2:

Store compressed data to disk, and load it again:

>>> import numpy as np
>>> x=np.array([[2, 4, 6], [8, 10, 12]])
>>> y=np.array([1, 3])
>>> np.savez('/tmp/123.npz', x=x, y=y)
>>> data = np.load('/tmp/123.npz')
>>> data['x']

Output:

array([[ 2,  4,  6],
       [ 8, 10, 12]])

NumPy.load() method Example-3:

>>> import numpy as np
>>> x=np.array([[2, 4, 6], [8, 10, 12]])
>>> y=np.array([1, 3])
>>> np.savez('/tmp/123.npz', x=x, y=y)
>>> data = np.load('/tmp/123.npz')
>>> data['y']

Output:

array([1, 3])

NumPy.load() method Example-4:

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

>>> import numpy as np
>>> X = np.load('/tmp/123.npy', mmap_mode='r')
>>> X[1, :]

Output:

memmap([8, 10, 12])

Python - NumPy Code Editor:

Previous: NumPy Home
Next: save() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/numpy/input-and-output/load.php