w3resource

NumPy: Save a given array to a binary file

NumPy: Basic Exercise-35 with Solution

Save Array to Binary File

Write a NumPy program to save a given array to a binary file.

This problem involves writing a NumPy program to save a given array to a binary file. The task requires utilizing NumPy's built-in functions to efficiently store the array's data in a binary format, which allows for compact storage and quick loading. By saving the array to a binary file, the program ensures data persistence and facilitates efficient data handling for future use.

Sample Solution :

Python Code :

# Importing the NumPy library with an alias 'np'
import numpy as np

# Importing the 'os' module for operating system-dependent functionality
import os

# Creating a NumPy array 'a' with integers from 0 to 19 using np.arange()
a = np.arange(20)

# Saving the NumPy array 'a' as a file named 'temp_arra.npy' using np.save()
np.save('temp_arra.npy', a)

# Printing a message checking if the file 'temp_arra.npy' exists or not
print("Check if 'temp_arra.npy' exists or not?")

# Checking if the file 'temp_arra.npy' exists using os.path.exists()
if os.path.exists('temp_arra.npy'):
    # Loading the data from 'temp_arra.npy' into 'x2' using np.load()
    x2 = np.load('temp_arra.npy')
    
    # Checking if the loaded array 'x2' is equal to the original array 'a' using np.array_equal()
    print(np.array_equal(a, x2)) 

Output:

Check if 'temp_arra.npy' exists or not?
True                         

Explanation:

The above code creates a NumPy array, saves it to a file, loads it back into another NumPy array, and checks if the original and loaded arrays are equal.

‘a = np.arange(20)’ creates a 1D array 'a' with elements ranging from 0 to 19.

np.save('temp_arra.npy', a): This statement saves the NumPy array 'a' to a binary file named 'temp_arra.npy' using NumPy's native file format.

if os.path.exists('temp_arra.npy'):: This statement checks if the file 'temp_arra.npy' exists in the current directory using the os.path.exists() function.

x2 = np.load('temp_arra.npy'): If the file exists, this line loads the contents of 'temp_arra.npy' back into a NumPy array named 'x2'.

Finally ‘print(np.array_equal(a, x2))’ uses the np.array_equal() function to check if the original array 'a' and the loaded array 'x2' are element-wise equal. If they are equal, it prints 'True', otherwise, it prints 'False'.

Python-Numpy Code Editor:

Previous: NumPy program to add a vector to each row of a given matrix.
Next: NumPy program to save two given arrays into a single file in compressed format (.npz format) and load it.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/numpy/basic/numpy-basic-exercise-35.php