BetterDocs
Home
Docs

Creation | np.frombuffer()

Method:

np.frombuffer(buffer, dtype=np.float64, count=-1, offset=0, *, like=None)

Interprets a buffer as a 1D array.

Returns:

np.ndarray

Parameters:

buffer: bytes-

Input buffer.

import numpy as np

buffer = bytes([1,2,3,4]) # Output: b''
arr_from_buffer = np.frombuffer(buffer=buffer, dtype=np.uint8)
print(arr_from_buffer) # Output: [1 2 3 4]

The dtype must be specified explicitly unless the buffer data-type is np.float64.

dtype: data-type, Optional-

Specifies the data-type of the array elements.

import numpy as np

buffer = bytes([1,2,3,4]) # Output: b''
arr_from_buffer = np.frombuffer(buffer=buffer, dtype=np.int8)
print(arr_from_buffer) # Output: [1 2 3 4]

Values: +

count: int, Optional-

Number of items to read.

import numpy as np

buffer = bytes([1,2,3,4]) # Output: b''
arr_from_buffer = np.frombuffer(buffer=buffer, dtype=np.int8, count=2)
print(arr_from_buffer) # Output: [1 2]

offset: int, Optional-

Start reading at this position.

import numpy as np

buffer = bytes([1,2,3,4]) # Output: b''
arr_from_buffer = np.frombuffer(buffer=buffer, dtype=np.int8, count=2, offset=2)
print(arr_from_buffer) # Output: [3 4]

like: None, Optional-

The like parameter in np.frombuffer() was introduced to make array creation more flexible, particularly for compatibility with libraries that extend NumPy, like Dask, CuPy, or other libraries that create arrays compatible with np.ndarray but optimized for different backends (e.g., parallel processing or GPU-based arrays).


Logo

BetterDocs

Support

EmailDiscordForms

Documentations

Python

Company

AboutDocs

Policies

Terms of ServicePrivacy Policy