NumPy frombuffer() Function:
np.frombuffer: The frombuffer() function of the NumPy module is used to interpret a buffer as a 1-dimensional array.
Using the given buffer, this function creates an array.
Syntax:
numpy.frombuffer(buffer, dtype=float, count=-1, offset=0)
Parameters
buffer: This is Required. It is an object that exposes the buffer interface.
dtype: This is optional. It denotes the data type of the array returned. float is the default value.
count: This is optional. It denotes the number of items to read. The default value is -1, which means all the data present.
offset: This is optional. Begin reading from this offset in the buffer (in bytes). The default value is 0.
Return Value:
The buffer’s array version is returned.
NumPy frombuffer() Function in Python
Example
Approach:
- Import numpy module using the import keyword
- Give some random string and keep prefix as ‘b’ to it
- Store it in a variable.
- Pass the given array and datatype as S1 as arguments to the frombuffer() function
of the numpy module and store it in another variable. - Here it creates a 1-Dimensional NumPy array from the above buffer
- Print the 1-Dimensional numpy array from the above buffer.
- Pass the given array, datatype as S1 and some random count as arguments to the
frombuffer() function of the numpy module and store it in another variable. - Print the above-obtained array.
- Pass the given array, datatype as S1, some random count and offset values as arguments to the
frombuffer() function of the numpy module and store it in another variable. - Print the above-obtained array(with count and offset).
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword
import numpy as np
# Give some random string and keep prefix as 'b' to it
# Store it in a variable.
gvn_buffr = b"Btechgeeks"
# Pass the given array and datatype as S1 as arguments to the frombuffer() function
# of the numpy module and store it in another variable.
# Here it creates 1-Dimensional numpy array from the above buffer
rslt_arry1 = np.frombuffer(gvn_buffr, dtype='S1')
# Print the 1-Dimensional numpy array from the above buffer.
print("The 1-Dimensional numpy array from the above buffer:\n", rslt_arry1)
# Pass the given array, datatype as S1 and some random count as arguments to the
# frombuffer() function of the numpy module and store it in another variable.
rslt_arry2 = np.frombuffer(gvn_buffr, dtype='S1', count=4)
# Print the above obtained array.
print("The above obtained Array:\n", rslt_arry2)
# Pass the given array, datatype as S1, some random count and offset values as arguments to the
# frombuffer() function of the numpy module and store it in another variable.
rslt_arry3 = np.frombuffer(gvn_buffr, dtype='S1', count=4, offset=5)
# Print the above obtained array(with count and offset).
print("The above obtained array(with count and offset).:\n", rslt_arry3)
Output:
The 1-Dimensional numpy array from the above buffer: [b'B' b't' b'e' b'c' b'h' b'g' b'e' b'e' b'k' b's'] The above obtained Array: [b'B' b't' b'e' b'c'] The above obtained array(with count and offset).: [b'g' b'e' b'e' b'k']