NumPy Files

A file is a container in computer storage devices used for storing data.

In the context of NumPy, a file can be thought of as a container used for storing NumPy arrays in computer storage devices.


Save Single NumPy Array File

In NumPy, we use the save() function to save a single NumPy array to a binary file in the .npy format.

Here's the syntax of the save() function.

np.save(file, array)
  • file - specifies the file name (along with path if required)
  • array - specifies the NumPy array to save

Now, let's see an example.

import numpy as np

array1 = np.array([[2, 4, 6], 
                  [8, 10, 12]])

# save the array to a file
np.save('file1.npy', array1)

Here, we saved the NumPy array array1 to the binary file file1.npy in our current directory.

Note: The .npy extension is automatically appended to the file name if it's not already present.


Load Single NumPy Array File

In the previous example, we saved an array to a binary file. Now we'll load that saved file using the load() function.

Let's see an example.

import numpy as np

# load the saved NumPy array
loaded_array = np.load('file1.npy')

# display the loaded array
print(loaded_array)

Output

[[ 2  4  6]
 [ 8  10 12]]

Here, we have used the load() function to read a binary file file1.npy. This is the same file we created and saved using save() function in the last example.


Save Multiple NumPy Array File

To save multiple NumPy arrays into a single file we use the savez() function.

The savez() function is similar to the save() function, but it can save multiple arrays at once in the .npz format.

Here's the syntax of the savez() function.

np.savez(file, *args, **kwds)
  • file - name of the file where the arrays will be saved
  • *args - list of arrays to be saved (comma-separated)
  • **kwds - keyword arguments to give custom names of the files to be saved

Now, let's see an example.

import numpy as np

# create two NumPy arrays
array1 = np.array([2, 6, 10])
array2 = np.array([4, 8, 12])

# save the two arrays into a single file
np.savez('file2.npz', file1 = array1, file2 = array2)

In this example, we have used the np.savez() function to save the two NumPy arrays array1 and array2 into a single file named file2.npz.

The file1 and file2 are the names given to the arrays using keyword arguments.

Note: We can savez_compressed() to save multiple arrays in a compressed file format. This function is similar to savez() but uses compression to reduce the size of the saved file.


Load Multiple NumPy Array File

In the previous example, we saved multiple arrays to a single file. Now we'll load that saved file using the load() function.

import numpy as np

# load the saved arrays 
load_data = np.load('file2.npz')

# retrieve the arrays using their names
array1 = load_data['file1']
array2 = load_data['file2']

# display the loaded arrays
print(array1)
print(array2)

Output

[ 2  6 10]
[ 4  8 12]

In the above example, first we loaded the saved arrays from the file using the np.load() function.

Then, we retrieved the arrays using their names file1 and file2 (mentioned in the previous example) as: data['file1'] and data['file2'], respectively.

Finally, we displayed the loaded arrays.