gpt4 book ai didi

python - 来自缓冲区的 Numpy 二维数组?

转载 作者:行者123 更新时间:2023-12-04 14:29:12 24 4
gpt4 key购买 nike

我有一个内存映射,其中包含一个二维数组,我想从中创建一个 numpy 数组。理想情况下,我想避免复制,因为涉及的数组可能很大。

我的代码如下所示:

n_bytes = 10000
tagname = "Some Tag from external System"
map = mmap.mmap(-1, n_bytes, tagname)
offsets = [0, 5000]

columns = []
for offset in offsets:
#type and count vary in the real code, but for this dummy code I simply made them up. But I know the count and type for every column.
np_type = np.dtype('f4')
column_data = np.frombuffer(map, np_type, count=500, offset=offset)
columns.append(column_data)

# this line seems to copy the data, which I would like to avoid
data = np.array(columns).T

最佳答案

假设您有一个字节数组并且您知道它的维度,那么答案非常简单。
想象一下缓冲区中图像的原始 RGB 数据(每像素 24 位)(名为“buff”)
尺寸为 1024x768

#read the buffer into 1D byte array
arr = numpy.frombuffer(buff, dtype=numpy.uint8)
#now shape the array as you please
arr.shape = (768,1024,3)

关于python - 来自缓冲区的 Numpy 二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39092664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com