原始
np.save(out_file,data)#原始npy
压缩
np.savez_compressed(out_file,data)#压缩npy
arrays = np.load(out_file)
arr = arrays['arr']
结果
原始数组采用npy保存,文件为5409kb。使用npz保存,仅为19kb。
减小内存大小
import io
import numpy as np
import os
A = np.arange(10000)
compressed_array = io.BytesIO() # np.savez_compressed() requires a file-like object to write to
np.savez_compressed(compressed_array, A)
# load it back
compressed_array.seek(0) # seek back to the beginning of the file-like object
decompressed_array = np.load(compressed_array)['arr_0']
from sys import getsizeof as getsize
var = object()
print(getsize(compressed_array))#80
print(getsize(decompressed_array))#40112