排序/词典/二进制计数/等,用于不适合内存的数组



我正试图扩大用numpy编写的库的规模,这样它就可以处理不适合内存的数组(大约10个100亿元素的数组)

hdf5(h5py)是一个临时解决方案,但我在很大程度上依赖排序和索引(b=a[a>5]),这两种方法在h5py中都不可用,写起来很痛苦。

有没有一个库可以提供这些工具?

具体来说,我需要基本的数学、排序、lexsort、argsort、bincount、np.diff和索引(包括布尔值和索引数组)。

PyTables正是为此而设计的(也是基于hdf5)。首先将阵列存储到磁盘

import numpy as np
import tables as tb
# Write big numpy array to disk
rows, cols = 80000000, 2
h5file = tb.open_file('test.h5', mode='w', title="Test Array")
root = h5file.root
array_on_disk = h5file.create_carray(root,
         'array_on_disk',tb.Float64Atom(),shape=(rows,cols))
# Fill part of the array
rand_array = np.random.rand(1000)
array_on_disk[10055:11055] = rand_array
array_on_disk[12020:13020] = 2.*rand_array  
h5file.close()

然后直接在文件中包含的数组(或其一部分)上执行计算

h5file = tb.open_file('disk_array.h5', mode='r')
print h5file.root.array_on_disk[10050:10065,0]
# in-place sort
h5file.root.array_on_disk[100000:10000000,:].sort(axis=0)
h5file.close()

最新更新