7

如何使用 pytables 创建一个巨大的 numpy 数组。我试过这个,但给了我“ValueError:数组太大”。错误:

import numpy as np
import tables as tb
ndim = 60000
h5file = tb.openFile('test.h5', mode='w', title="Test Array")
root = h5file.root
h5file.createArray(root, "test", np.zeros((ndim,ndim), dtype=float))
h5file.close()
4

2 回答 2

15

捎带@b1r3k 的响应,创建一个你不会一次访问的数组(即把整个东西放到内存中),你想使用一个CArray(分块数组)。这个想法是您将逐步填充和访问它:

import numpy as np
import tables as tb
ndim = 60000
h5file = tb.openFile('test.h5', mode='w', title="Test Array")
root = h5file.root
x = h5file.createCArray(root,'x',tb.Float64Atom(),shape=(ndim,ndim))
x[:100,:100] = np.random.random(size=(100,100)) # Now put in some data
h5file.close()
于 2011-12-27T14:39:45.350 回答
8

您可以尝试使用 tables.CArray 类,因为它支持压缩,但是...

我认为问题更多的是关于 numpy 而不是 pytables,因为您在使用 pytables 存储数组之前使用 numpy 创建数组。

这样,您需要大量 ram 来执行np.zeros((ndim,ndim) - 这可能是引发异常的地方:“ValueError: array is too big.”。

如果矩阵/数组不密集,那么您可以使用 scipy 中可用的稀疏矩阵表示:http: //docs.scipy.org/doc/scipy/reference/sparse.html

另一个解决方案是尝试通过块访问您的数组,如果它不需要一次整个数组 - 查看这个线程:使用 Python 和 NumPy 的非常大的矩阵

于 2011-12-27T10:12:25.097 回答