![](/img/trans.png)
[英]How to use np.save to save files in different directory in python?
[英]Is the bytes offset in files produced by np.save always 128?
我一直在玩numpy memmaps,我注意到如果生成了一些數據並將其像這樣轉儲到磁盤中:
from os.path import join
import numpy as np
import tempfile
print('Generate dummy data')
N = 4
D = 3
x, y = np.meshgrid(np.arange(0, N), np.arange(0, N))
data = np.ascontiguousarray((np.dstack([x] * D) % 256).astype(np.uint8))
print('Make temp directory')
dpath = tempfile.mkdtemp()
mem_fpath = join(dpath, 'foo.npy')
print('Dump memmap')
np.save(mem_fpath, data)
那么np.memmap
和np.load
產生的數據是不同的。
file1 = np.memmap(mem_fpath, dtype=data.dtype.name, shape=data.shape,
mode='r')
file2 = np.load(mem_fpath)
print('file1 =\n{!r}'.format(file1[0]))
print('file2 =\n{!r}'.format(file2[0]))
導致
file1 =
memmap([[147, 78, 85],
[ 77, 80, 89],
[ 1, 0, 118],
[ 0, 123, 39]], dtype=uint8)
file2 =
array([[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3]], dtype=uint8)
這讓我感到困惑,但是最終我發現我需要將np.memmap中的offset參數設置為128才能起作用:
for i in range(0, 1000):
file1 = np.memmap(mem_fpath, dtype=data.dtype.name, shape=data.shape,
offset=i, mode='r')
if np.all(file1 == data):
print('i = {!r}'.format(i))
break
print('file1 =\n{!r}'.format(file1[0]))
導致執行
i = 128
file1 =
memmap([[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3]], dtype=uint8)
我的問題是,這128個數字是哪里來的。 我檢查了np.save文檔,但沒有看到對它的引用。 我也嘗試過修改數據的dtype和形狀,但是我始終發現偏移量是128。我是否可以假設使用np.save
保存的任何單個數組將始終具有該128偏移量? 如果沒有,如何確定偏移量。
我問的原因是因為我發現從磁盤上較大的文件中裁剪小區域的特定用例中,使用np.memmap比np.load快得多。
感謝您的任何幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.