numpy.frombuffer() in Python
The frombuffer() function of Python numpy class creates an array by using the given buffer.
Syntax
1 2 3 |
numpy.frombuffer(buffer, dtype=float, count=-1, offset=0) |
Parameter
The numpy.frombuffer() method consists of four parameters, which are as follows:
- buffer: This parameter represents an object that depicts a buffer interface.
- dtype: It represents the data type of the returned data type array. It is an optional parameter, and by default,its value is 0.
- count: It represents the length of the returned ndarray. It is an optional parameter, and its default value is -1.
- offset: It represents the starting position to read from. It is also an optional parameter, and its default value is 0.
Return
This function returns the array version of the buffer.
Example 1
1 2 3 4 5 6 7 8 9 10 |
# Python Programming giving an example for # numpy.frombuffer() function importnumpy as numpy obj = b'This is my numpy program' print(type(obj)) n = numpy.frombuffer(obj, dtype = "S1") print(n) print(type(n)) |
Output
1 2 3 4 5 6 |
<class 'bytes'> [b'T' b'h' b'i' b's' b' ' b'i' b's' b' ' b'm' b'y' b' ' b'n' b'u' b'm' b'p' b'y' b' ' b'p' b'r' b'o' b'g' b'r' b'a' b'm'] <class 'numpy.ndarray'> |