NumPy: Convert a given array into bytes, and load it as array
NumPy: Basic Exercise-38 with Solution
Convert Array to Bytes and Back
Write a NumPy program to convert a given array into bytes, and load it as an array.
This problem involves writing a NumPy program to convert a given array into a bytes object and then load it back as an array. The task requires using NumPy's "tobytes()" method to serialize the array into a bytes object and the "frombuffer()" method to deserialize the bytes back into an array. This process demonstrates how to efficiently convert array data to a byte representation for storage or transmission and then restore it to its original form.
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' containing integers from 1 to 6
a = np.array([1, 2, 3, 4, 5, 6])
# Printing a message indicating the original array 'a'
print("Original array:")
print(a)
# Converting the array 'a' to bytes using a.tobytes() method and storing it in 'a_bytes'
a_bytes = a.tobytes()
# Creating a new NumPy array 'a2' by reading from 'a_bytes' using np.frombuffer()
# Specifying the datatype of 'a2' as the same as 'a' using a.dtype
a2 = np.frombuffer(a_bytes, dtype=a.dtype)
# Printing a message indicating the content of the array after loading from bytes
print("After loading, content of the text file:")
print(a2)
# Checking if both arrays 'a' and 'a2' are equal using np.array_equal()
print(np.array_equal(a, a2))
Output:
Original array: [1 2 3 4 5 6] After loading, content of the text file: [1 2 3 4 5 6] True
Explanation:
In the above code -
np.array([1, 2, 3, 4, 5, 6]) creates a 1D array and stores in the variable 'a' with elements 1 to 6.
a_bytes = a.tobytes(): This statement converts the NumPy array 'a' into a bytes object named 'a_bytes'.
a2 = np.frombuffer(a_bytes, dtype=a.dtype): This statement reconstructs a NumPy array 'a2' from the bytes object 'a_bytes' using the 'frombuffer' function. The 'dtype' argument is set to the same data type as the original array 'a' to ensure that the elements are interpreted correctly.
print(np.array_equal(a, a2)): This statement uses the 'np.array_equal()' function to check if the original array 'a' and the reconstructed array 'a2' are element-wise equal. If they are equal, it prints 'True'; otherwise, it prints 'False'.
Python-Numpy Code Editor:
Previous: NumPy program to save a given array to a text file and load it.
Next: NumPy program to convert a given list into an array, then again convert it into a list. Check initial list and final list are equal or not.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
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-38.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics