numpy.load(file, mmap_mode=None, allow_pickle=False, fix_imports=True, encoding='ASCII')
[source]
Load arrays or pickled objects from .npy
, .npz
or pickled files.
Warning
Loading files that contain object arrays uses the pickle
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.
Parameters: |
|
---|---|
Returns: |
|
Raises: |
|
See also
save
, savez
, savez_compressed
, loadtxt
memmap
lib.format.open_memmap
.npy
file..npy
file, then a single array is returned. .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.
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])
© 2005–2019 NumPy Developers
Licensed under the 3-clause BSD License.
https://docs.scipy.org/doc/numpy-1.17.0/reference/generated/numpy.load.html