How To Save And Load Numpy Array in Python – With Examples

A numpy array is used to store numerical data for manipulation.

You can save and load a numpy array in python using the numpy.save() and numpy.load() methods.

Basic Example

import numpy as np

numpyArr = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])

np.save('myNumpyArray.npy', numpyArr)

loadedArray= np.load('myNumpyArray.npy')
  • The save method saves the array in a npy format, a standard binary format for storing the numpy arrays.

This tutorial teaches you the different methods to store the numpy array and load it for later use in machine-learning activities.

Using Numpy Save and Load

  • The NumPy save() method serializes the NumPy array into the disk
  • The NumPy load() method deserialises the array and converts its back to the NumPy array.

This method stores the endianness of the data, and precision is also kept.

Use this method when you want to store the array in a platform-independent format and want to preserve the precision of the data.

Code

import numpy as np

numpyArr = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])

np.save('myNumpyArray.npy', numpyArr)

loadedArray= np.load('myNumpyArray.npy')

print(loadedArray)

print(numpyArr == loadedArray)

Output

    [[1 2 3]
     [4 5 6]
     [7 8 9]]
    [[ True  True  True]
     [ True  True  True]
     [ True  True  True]]

Using SaveTxt and LoadTxt

  • The NumPy saveTxt() method writes the numpy array as a text file to the disk
  • The NumPy loadtxt() method reads the text file and converts it into a NumPy array.

The saveTxt() method writes data in “%.18e” format by default if the fmt parameter is not specified. You can also specify other formats such as decimal to store the number in decimal format

Use this method when you want to store the array in a text format and have numbers in different forms such as decimal, float, or String.

Code

import numpy as np

numpyArr = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])

np.savetxt('myNumpyArray.txt', numpyArr, fmt='%d')

loadedArray = np.loadtxt('myNumpyArray.txt', dtype=int)

print(loadedArray)

print(numpyArr == loadedArray)

Output

    [[1 2 3]
     [4 5 6]
     [7 8 9]]
    [[ True  True  True]
     [ True  True  True]
     [ True  True  True]]

Using ToFile and FromFile

  • The NumPy tofile() method writes the numpy array as a text file to the disk in an efficient way
  • The NumPy fromfile() method reads the text file and converts it into a NumPy array.

This is the highly efficient way to write and read a NumPy array. However, it doesn’t maintain the endianness and the precession of the data.

Use this method when storing the data, Byte order and data-type maintenance are unnecessary.

Code

import numpy as np

numpyArr = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])

numpyArr.tofile('numpyArr.dat')

loadedArray = np.fromfile('numpyArr.dat', dtype=int)

print(loadedArray)

Output

    [1 2 3 4 5 6 7 8 9]

Using savez_compressed And Load

The savez_compressed() saves the single/multiple NumPy array into a single compressed file.

Use this method when you want to store data in a compressed format or when you want to compress more than one NumPy array into a single file and load it back.

Code

import numpy as np

numpyArr = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])

np.savez_compressed('myNumpyArray.npz', numpyArr)

loadedArray = np.load('myNumpyArray.npz')

data = loadedArray['arr_0']

print(data)

Output

    [[1 2 3]
     [4 5 6]
     [7 8 9]]

Save Multiple Numpy Arrays

This section teaches you how to save multiple NumPy arrays into a single uncompressed NPY file using the savez() method.

  • Invoke the savez() method and pass the name for the npz file.
  • Pass the arrays you want to save and provide a key name for each array that needs to be stored in the file
  • While reading, a dictionary-like object is returned, and you can access each array using the key provided earlier.

Code

import numpy as np

array1 = np.asarray([ [1,2,3], [4,5,6] ])

array2 =  np.asarray([ [7,8,9], [10,11,12] ])

np.savez('myNumpyArray.npz', name1=array1, name2=array2)

data = np.load('myNumpyArray.npz')

print(data['name1'])

print(data['name2'])

Output

    [[1 2 3]
     [4 5 6]]
    [[ 7  8  9]
     [10 11 12]]

This is how you can save and load NumPy arrays for machine learning purposes properly.

Additional Resources

Leave a Comment