nlcpy.load

nlcpy.load(file, mmap_mode=None, allow_pickle=False, fix_imports=True, encoding='ASCII')[ソース]

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

警告

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
filefile-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, memory-map the file to construct an intermediate numpy.ndarray object and create nlcpy.ndarray from it.

allow_picklebool, 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

fix_importsbool, optional

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.

encodingstr, 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'

Returns
resultndarray, 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.

参考

loadtxt

Loads data from a text file.

注釈

  • 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.

Examples

Store data to disk, and load it again:

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

Store compressed data to disk, and load it again:

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