我有一个非常大的数据文件(1000 x 1400000 数组),其中包含整数 0、1、2 和 4。使用 h5py 将这些大数据加载到 numpy 数组中需要很长时间,因为我的内存(4GB)无法容纳这么多并且程序使用交换空间。由于数据中只有 4 个数字,因此我想使用 8 位整数数组。目前我加载数据并将其转换为 8 位 int 数组。
with h5py.File("largedata", 'r') as f:
variables = f.items()
# extract all data
for name, data in variables:
# If DataSet pull the associated Data
if type(data) is h5py.Dataset:
value = data.value
if(name == 'foo'):
# convert to 8 bit int
nparray = np.array(value, dtype=np.int8)
是否可以将数据直接加载到8位int数组中以在加载时节省内存?
来自数据集文档页面
astype(dtype)
Return a context manager allowing you to read data as a particular type.
Conversion is handled by HDF5 directly, on the fly:
>>> dset = f.create_dataset("bigint", (1000,), dtype='int64')
>>> with dset.astype('int16'):
out = dset[:]
>>> out.dtype
=dtype('int16')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)