The frombuffer()
function in NumPy is a powerful tool for converting data that resides in a buffer, like bytes or a byte-like object, into a NumPy array. This is especially useful when dealing with binary data stored in files or received from network connections where performance is critical. This function allows for a direct read of the buffer’s contents into a NumPy array structure, avoiding the overhead of copying data and thereby enhancing the efficiency of memory usage.
In this article, you will learn how to utilize the frombuffer()
function to convert various types of buffers into NumPy arrays. Explore how this function works with different data types and how to specify data types to correctly interpret the buffer's contents.
Obtain or create a buffer containing byte data.
Use the frombuffer()
function to convert this buffer to a NumPy array.
import numpy as np
buffer = b'\x01\x02\x03\x04'
array = np.frombuffer(buffer, dtype=np.uint8)
print(array)
This code converts a buffer of bytes to a NumPy array of unsigned 8-bit integers. The dtype=np.uint8
parameter tells frombuffer()
how to interpret each byte in the buffer.
Define a buffer with data in a specific byte order.
Specify the byte order in the data type during conversion.
buffer = b'\x01\x00\x00\x00\x02\x00\x00\x00'
array = np.frombuffer(buffer, dtype=np.uint32)
print(array)
The provided buffer is interpreted as a 32-bit unsigned integer array assuming the system's default byte order. If necessary, you can explicitly set the byte order (little-endian or big-endian) in the dtype
parameter.
Encode a string to bytes.
Convert these bytes into a NumPy array.
string = "example"
buffer = string.encode('utf-8') # encoding string to bytes
array = np.frombuffer(buffer, dtype=np.uint8)
print(array)
Encoding the string as UTF-8 converts it into a byte buffer, which frombuffer()
then turns into an array of 8-bit unsigned integers.
frombuffer()
with MemoryviewCreate a memoryview from a byte array.
Convert this memoryview to a NumPy array.
byte_arr = bytearray([1, 2, 3, 4, 255])
mview = memoryview(byte_arr)
array = np.frombuffer(mview, dtype=np.uint8)
print(array)
A memoryview is an intermediate step that allows you to handle the buffer without copying it. frombuffer()
can take this memoryview directly and create a NumPy array from it.
Using the frombuffer()
function from NumPy efficiently converts data stored in buffers into NumPy arrays, facilitating rapid data processing and manipulation. Whether working with raw binary files, processing strings for machine learning inputs, or interfacing with low-level network protocols, .frombuffer()
ensures you have the tools to efficiently move data into the powerful computational environment provided by NumPy. Understand and exploit these techniques to enhance the performance and efficiency of your data-driven Python applications.