内置
.npy文件格式非常适合处理小型数据集,而无需依赖外部模块
numpy。
但是,当您开始拥有大量数据时,首选使用旨在处理此类数据集的文件格式(例如HDF5
例如,以下是
numpy使用PyTables在HDF5中保存数组的解决方案,
步骤1:创建可扩展EArray
存储
import tablesimport numpy as npfilename = 'outarray.h5'ROW_SIZE = 100NUM_COLUMNS = 200f = tables.open_file(filename, mode='w')atom = tables.Float64Atom()array_c = f.create_earray(f.root, 'data', atom, (0, ROW_SIZE))for idx in range(NUM_COLUMNS): x = np.random.rand(1, ROW_SIZE) array_c.append(x)f.close()
步骤2:将行追加到现有数据集(如果需要)
f = tables.open_file(filename, mode='a')f.root.data.append(x)
步骤3:读回一部分数据
f = tables.open_file(filename, mode='r')print(f.root.data[1:10,2:20]) # e.g. read from disk only this part of the dataset



