Aca*_*mia 7 python arrays numpy pytables
如何使用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()
Run Code Online (Sandbox Code Playgroud)
Jos*_*del 15
抄袭@ b1r3k的响应,创建一个你不会同时访问的数组(即将整个内容带入内存),你想要使用CArray(Chunked Array).我们的想法是,您将逐步填充并访问它:
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()
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用tables.CArray类,因为它支持压缩,但...
我认为问题更多的是关于numpy而不是pytables,因为你在用pytables存储它之前使用numpy创建数组.
以这种方式你需要很多ram来执行np.zeros((ndim,ndim) - 这可能是异常的地方:"ValueError:array太大了."引发了.
如果矩阵/数组不密集,那么你可以使用scipy中提供的稀疏矩阵表示:http://docs.scipy.org/doc/scipy/reference/sparse.html
另一个解决方案是尝试通过块访问您的数组,如果它不需要一次完整数组 - 请查看此主题:使用Python和NumPy的非常大的矩阵