如何使用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()
pigbacking @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()
您可以尝试使用表。类,因为它支持压缩,但是…
我认为问题更多的是关于numpy而不是pytables,因为你在用pytables存储它之前使用numpy创建数组。
在这种情况下,你需要大量的内存来执行np.zero ((ndim,ndim) -这可能是异常:"ValueError: array is too big."被引发的地方。
如果矩阵/数组不是密集的,那么你可以使用scipy中提供的稀疏矩阵表示:http://docs.scipy.org/doc/scipy/reference/sparse.html
另一个解决方案是尝试通过块访问你的数组,如果你不需要一次整个数组-看看这个线程:非常大的矩阵使用Python和NumPy