python - 是否可以使用h5py将大数据直接加载到numpy int8数组中?

标签 python arrays numpy h5py

我有一个非常大的数据文件(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')

关于python - 是否可以使用h5py将大数据直接加载到numpy int8数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42413209/

相关文章:

Python:与始终运行的子进程通信

python - 使用 Python 请求模块时尝试/除外

PHP删除数组中的重复项

python - 将 numpy 数组保存到不带括号和空格的文件中

python - 使用 numpy 进行图像翻译

arrays - PIL 类型错误 : Cannot handle this data type: (1, 1, 1), |u1

python - 如何在 Python 的二维数组中查找值的索引?

python - 在python中绘制具有相同标签和颜色的序列

javascript - 如何使用 JS/Jquery 将 UTC 日期数组转换为毫秒

c++ - 在数组中存储整数存储随机值