【问题标题】:Building a huge numpy array using pytables使用 pytables 构建一个巨大的 numpy 数组
【发布时间】:2012-01-28 09:10:00
【问题描述】:

如何使用 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()

【问题讨论】:

    标签: python arrays numpy pytables


    【解决方案1】:

    利用@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()
    

    【讨论】:

      【解决方案2】:

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

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

      这样你需要大量的内存来执行 np.zeros((ndim,ndim) - 这可能是出现异常的地方:“ValueError: array is too big.”提出来。

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

      另一个解决方案是尝试通过块访问您的数组,如果您不需要一次整个数组 - 请查看此线程:Very large matrices using Python and NumPy

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-23
        • 1970-01-01
        • 2018-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多