Save Array to File & Read Array from File - NumPy


NumPy - Save array to file & read array from file

You can save a NumPy array to a file using numpy.save() and later load it back into an array using numpy.load().

Below is an example of saving an array to a file and then reading it back using the save() and load() functions.

# save array to file
numpy.save(file, array)
# load file to array
array = numpy.load(file)

Examples

1. Save NumPy array to file

In the following example, we will initialize an array, open a file in write binary mode, and then save the array to the file using the numpy.save() method.

Python Program

import numpy as np

#initialize an array
arr = np.array([[[11, 11, 9, 9],
                  [11, 0, 2, 0]],
                 [[10, 14, 9, 14],
                  [0, 1, 11, 11]]])

# open a binary file in write mode
file = open("arr", "wb")
# save array to the file
np.save(file, arr)
# close the file
file.close()

Explanation:

  1. We initialize a multi-dimensional NumPy array arr with random values.
  2. The numpy.save() function saves the array to a binary file named arr.
  3. The file is closed after saving.
  4. You can observe that a new file named arr will be created in the current working directory.

Once you have saved the array, ensure that the file is properly closed.


2. Load the saved NumPy array from file

In this example, we will load the saved array from the file. We will use the previously saved file and read it back into a NumPy array.

Python Program

import numpy as np

#initialize an array
arr = np.array([[[11, 11, 9, 9],
                  [11, 0, 2, 0]],
                 [[10, 14, 9, 14],
                  [0, 1, 11, 11]]])

# open a binary file in write mode
file = open("arr", "wb")
# save array to the file
np.save(file, arr)
# close the file
file.close()

# open the file in read binary mode
file = open("arr", "rb")
#read the file to numpy array
arr1 = np.load(file)
#close the file
file.close()
# display loaded array
print(arr1)

Explanation:

  1. The array arr is first saved to the arr file.
  2. Then, the file is reopened in read binary mode "rb".
  3. We load the data from the file using np.load(file) into a new variable arr1.
  4. The loaded array is displayed using print(arr1).

Output

[[[11 11  9  9]
  [11  0  2  0]]

 [[10 14  9 14]
  [ 0  1 11 11]]]

3. Save and load multiple arrays

If you need to save and load multiple arrays, you can save them into a single file using numpy.savez() and load them using numpy.load().

Python Program

import numpy as np

# initialize two arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

# save both arrays in one file
np.savez("arrays.npz", array1=arr1, array2=arr2)

# load the saved arrays
loaded_arrays = np.load("arrays.npz")
print(loaded_arrays["array1"])
print(loaded_arrays["array2"])

Explanation:

  1. We initialize two one-dimensional arrays arr1 and arr2.
  2. We save both arrays into a single file using np.savez() and assign each array a unique keyword.
  3. We then load the arrays back using np.load() and print their contents using their respective keys.

Output

[1 2 3]
[4 5 6]

Summary

In this NumPy Tutorial, we learned how to save a NumPy array to a file using numpy.save() and load it back using numpy.load(). Additionally, we explored how to save multiple arrays in a single file with numpy.savez() and load them together.


Python Libraries