How To Save And Load NumPy Objects?


How To Save And Load NumPy Objects?

Table Of Contents:

  1. np.save
  2. np.savez
  3. np.savetxt
  4. np.load
  5. np.loadtxt

(1) np.save():

  • Save an array to a binary file in NumPy .npy format.

Syntax:

numpy.save(file, arr, allow_pickle=True, fix_imports=True)

Parameters:

  • file: file, str, or pathlib.Path – File or filename to which the data is saved. If the file is a file object, then the filename is unchanged. If the file is a string or Path, a .npy the extension will be appended to the filename if it does not already have one.
  • arr: array_like – Array data to be saved.
  • allow_pickle: bool, optional – Allow saving object arrays using Python pickles. Reasons for disallowing pickles include security (loading pickled data can execute arbitrary code) and portability (pickled objects may not be loadable on different Python installations, for example, if the stored objects require libraries that are not available, and not all pickled data is compatible between Python 2 and Python 3). Default: True
  • fix_imports: bool, optional – Only useful in forcing objects in object arrays on Python 3 to be pickled in a Python 2 compatible way. If fix_imports is True, pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2.

(2)Examples Of np.save():

Example:

a = np.array([1, 2, 3, 4, 5, 6])
np.save('E:/Blogs/NumPy/Files/NumpyObject', a)

Note:

  • The file has been saved in ‘.npy’ format

(3) np.load():

  • Save an array to a binary file in NumPy .npy format.

Syntax:

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

Parameters:

  • file: file-like object, string, or pathlib.Path – The file to read. File-like objects must support the seek() and read() methods and must always be opened in binary mode. 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, 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, 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_imports: bool, 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.

  • encoding: str, 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’
  • max_header_size: int, optional – Maximum 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 when allow_pickle is passed. In that case, the file is by definition trusted and the limit is unnecessary.

Raises:

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

(4) Examples Of np.load():

Example:

b = np.load('E:/Blogs/NumPy/Files/NumpyObject.npy')
b
array([1, 2, 3, 4, 5, 6])

(5) np.savez():

  • Save an array to a binary file in NumPy .npy format.

Syntax:

numpy.save(file, arr, allow_pickle=True, fix_imports=True)

Parameters:

  • file:file, str, or pathlib.Path – File or filename to which the data is saved. If the file is a file object, then the filename is unchanged. If the file is a string or Path, a .npy the extension will be appended to the filename if it does not already have one.
  • arr: array_like – Array data to be saved.
  • allow_pickle: bool, optional – Allow saving object arrays using Python pickles. Reasons for disallowing pickles include security (loading pickled data can execute arbitrary code) and portability (pickled objects may not be loadable on different Python installations, for example, if the stored objects require libraries that are not available, and not all pickled data is compatible between Python 2 and Python 3). Default: True
  • fix_imports: bool, optionalOnly useful in forcing objects in object arrays on Python 3 to be pickled in a Python 2 compatible way. If fix_imports is True, pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2.

Example:

import numpy as np 
my_array_1 = np.array([[1,2,3],[4,5,6]])
my_array_2 = np.linspace(start = 0, stop = 100, num = 5)
np.savez('E:/Blogs/NumPy/Files/MergedNumpyObject.npz', my_array_1, my_array_2)

Example: Loading The .npz File

loaded_arrays = np.load('E:/Blogs/NumPy/Files/MergedNumpyObject.npz')
loaded_arrays.files
['arr_0', 'arr_1']
loaded_arrays['arr_0']
array([[1, 2, 3],
       [4, 5, 6]])
loaded_arrays['arr_1']
array([  0.,  25.,  50.,  75., 100.])

(6) np.savetxt():

  • Save an array to a text file.

Syntax:

numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', 
             header='', footer='', comments='# ', encoding=None)

Parameters:

  • fname: filename or file handle – If the filename ends in .gz, the file is automatically saved in compressed gzip format. loadtxt understands gzipped files transparently.
  • X: 1D or 2D array_like – Data to be saved to a text file.
  • fmt: str or sequence of strs, optional –

    A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. ‘Iteration %d – %10.5f’, in which case delimiter is ignored. For complex X, the legal options for fmt are:

    • a single specifier, fmt=’%.4e’, resulting in numbers formatted like ‘ (%s+%sj)’ % (fmt, fmt)

    • a full string specifying every real and imaginary part, e.g. ‘ %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej’ for 3 columns

    • a list of specifiers, one per column – in this case, the real and imaginary part must have separate specifiers, e.g. [‘%.3e + %.3ej’, ‘(%.15e%+.15ej)’] for 2 columns

  • delimiter: str, optional – String or character separating columns.
  • newline: str, optional – String or character separating lines.
  • header: str, optional –  String that will be written at the beginning of the file.
  • footer: str, optional – String that will be written at the end of the file.
  • comments: str, optional – String that will be prepended to the header and footer strings, to mark them as comments. Default: ‘# ‘, as expected by e.g. numpy.loadtxt.
  • encoding:{None, str}, optional – 

    Encoding used to encode the outputfile. Does not apply to output streams. If the encoding is something other than ‘bytes’ or ‘latin1’ you will not be able to load the file in NumPy versions < 1.14. Default is ‘latin1’.

Example: Saving Into A CSV File

csv_arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
np.savetxt('E:/Blogs/NumPy/Files/new_file.csv', csv_arr)

Example: Loading The CSV File

np.loadtxt('E:/Blogs/NumPy/Files/new_file.csv')
array([1., 2., 3., 4., 5., 6., 7., 8.])

Leave a Reply

Your email address will not be published. Required fields are marked *