次の例を検討してください。
import numpy as np
a = np.array(1)
np.save("a.npy", a)
a = np.load("a.npy", mmap_mode='r')
print(type(a))
b = a + 2
print(type(b))
出力する
<class 'numpy.core.memmap.memmap'>
<class 'numpy.int32'>
したがって、これはもはや でb
はないようです。これにより、全体を読み取る必要があり、memmap の目的が無効になると思います。したがって、私の質問は、アクセス時間まで操作を延期できますか?memmap
numpy
a.npy
memmaps
ndarray
サブクラス化またはサブクラス化がうまくいくと信じてmemmap
いますが、それを試すほどの Python スキルに自信がありません。
これは私の問題を示す拡張された例です:
import numpy as np
# create 8 GB file
# np.save("memmap.npy", np.empty([1000000000]))
# I want to print the first value using f and memmaps
def f(value):
print(value[1])
# this is fast: f receives a memmap
a = np.load("memmap.npy", mmap_mode='r')
print("a = ")
f(a)
# this is slow: b has to be read completely; converted into an array
b = np.load("memmap.npy", mmap_mode='r')
print("b + 1 = ")
f(b + 1)