NumPy: Convert a given array into bytes, and load it as array
Write a NumPy program to convert a given array into bytes, and load it as an array.
Sample Solution :
Python Code :
import numpy as np import os a = np.array([1, 2, 3, 4, 5, 6]) print("Original array:") print(a) a_bytes = a.tobytes() a2 = np.frombuffer(a_bytes, dtype=a.dtype) print("After loading, content of the text file:") print(a2) print(np.array_equal(a, a2))
Original array: [1 2 3 4 5 6] After loading, content of the text file: [1 2 3 4 5 6] True
Explanation:
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:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource’s quiz.
Follow us on Facebook and Twitter for latest update.
numpy.ndarray.tobytes#
Construct Python bytes containing the raw data bytes in the array.
Constructs Python bytes showing a copy of the raw contents of data memory. The bytes object is produced in C-order by default. This behavior is controlled by the order parameter.
Controls the memory layout of the bytes object. ‘C’ means C-order, ‘F’ means F-order, ‘A’ (short for Any) means ‘F’ if a is Fortran contiguous, ‘C’ otherwise. Default is ‘C’.
Returns : s bytes
Python bytes exhibiting a copy of a’s raw data.
Inverse of this operation, construct a 1-dimensional array from Python bytes.
>>> x = np.array([[0, 1], [2, 3]], dtype=') >>> x.tobytes() b'\x00\x00\x01\x00\x02\x00\x03\x00' >>> x.tobytes('C') == x.tobytes() True >>> x.tobytes('F') b'\x00\x00\x02\x00\x01\x00\x03\x00'
numpy.ndarray.tobytes#
Construct Python bytes containing the raw data bytes in the array.
Constructs Python bytes showing a copy of the raw contents of data memory. The bytes object is produced in C-order by default. This behavior is controlled by the order parameter.
Controls the memory layout of the bytes object. ‘C’ means C-order, ‘F’ means F-order, ‘A’ (short for Any) means ‘F’ if a is Fortran contiguous, ‘C’ otherwise. Default is ‘C’.
Returns : s bytes
Python bytes exhibiting a copy of a’s raw data.
Inverse of this operation, construct a 1-dimensional array from Python bytes.
>>> x = np.array([[0, 1], [2, 3]], dtype=') >>> x.tobytes() b'\x00\x00\x01\x00\x02\x00\x03\x00' >>> x.tobytes('C') == x.tobytes() True >>> x.tobytes('F') b'\x00\x00\x02\x00\x01\x00\x03\x00'
NumPy Array to Bytes and Bytes to NumPy Array – NumPy Tutorial
In order to save numpy array to lmdb, we have to convert numpy array to bytes. Meanwhile, if you plan to read numpy array from lmdb, you have to convert a bytes to numpy array. In this tutorial, we will introduce you how to convert.
Preliminary
If you want to know how to use lmdb to save data, you can read:
Convert numpy array to bytes
It is easy to convert numpy array to bytes, we can use ndarray.tobytes() method.
import librosa wav_file = "music-jamendo-0039.wav" wav_data, sr = librosa.load(wav_file, mono=True) print(wav_data.dtype) import numpy as np print(wav_data[0:50]) b = wav_data.tobytes() print(type(b))
Here wav_data is a numpy array, which contains the data of an audio.
Run this code, we will see:
float32 [ 3.1595556e-07 1.0924321e-06 -2.0543989e-06 1.4881829e-06 1.0925655e-06 -5.4893881e-06 1.2117634e-05 3.0912117e-05 8.0338878e-06 -9.6241101e-06 7.1520894e-06 3.9458875e-08 -2.7768758e-05 -1.9811972e-05 2.3305599e-06 2.1289316e-06 -2.1991723e-06 4.1150400e-07 9.9875649e-07 -7.9325520e-07 -6.2810352e-07 1.6572957e-06 -8.0126512e-07 -2.0320670e-06 5.1378197e-06 -5.1078187e-06 -2.8517738e-05 -1.7178845e-05 7.3522529e-06 -6.6071664e-07 -4.5573697e-06 8.0500204e-06 -8.8221705e-06 -3.0072155e-05 -1.2288952e-05 5.9455974e-06 -6.2966063e-07 -2.5200138e-06 2.6032403e-06 -4.4498762e-07 -1.7536241e-06 1.9865117e-06 -3.9684135e-08 -2.2182173e-06 2.2615259e-06 8.9228968e-07 -5.3025328e-06 6.0253165e-06 2.5768280e-05 1.8642553e-05]
We can find the type of b is bytes.
Convert bytes to numpy array
In numpy, we can use numpy.frombuffer() to convert a bytes to array.
x = np.frombuffer(b, dtype = np.float32) print(type(x)) print(x[0:50])
Here we should notice the dtype = np.float32 , which is the same to wav_data.dtype .
Run this code, we will see:
[ 3.1595556e-07 1.0924321e-06 -2.0543989e-06 1.4881829e-06 1.0925655e-06 -5.4893881e-06 1.2117634e-05 3.0912117e-05 8.0338878e-06 -9.6241101e-06 7.1520894e-06 3.9458875e-08 -2.7768758e-05 -1.9811972e-05 2.3305599e-06 2.1289316e-06 -2.1991723e-06 4.1150400e-07 9.9875649e-07 -7.9325520e-07 -6.2810352e-07 1.6572957e-06 -8.0126512e-07 -2.0320670e-06 5.1378197e-06 -5.1078187e-06 -2.8517738e-05 -1.7178845e-05 7.3522529e-06 -6.6071664e-07 -4.5573697e-06 8.0500204e-06 -8.8221705e-06 -3.0072155e-05 -1.2288952e-05 5.9455974e-06 -6.2966063e-07 -2.5200138e-06 2.6032403e-06 -4.4498762e-07 -1.7536241e-06 1.9865117e-06 -3.9684135e-08 -2.2182173e-06 2.2615259e-06 8.9228968e-07 -5.3025328e-06 6.0253165e-06 2.5768280e-05 1.8642553e-05]
From the result, we can find x is numpy.ndarray, it is same to wav_data .