维度无关(通用)笛卡尔produ

2024-06-09 20:10:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望生成一个相对大的数组的笛卡尔积来跨越一个高维网格。由于高维性,笛卡尔积计算的结果不可能存储在内存中,而是写入硬盘。由于这个限制,我需要在生成中间结果时访问它们。到目前为止我一直在做的是:

for x in xrange(0, 10):
    for y in xrange(0, 10):
        for z in xrange(0, 10):
            writeToHdd(x,y,z)

除了我,我不需要很多维度。我曾尝试使用here提出的解决方案,但这是一个递归的解决方案,因此很难在生成结果时即时获得结果。除了每个维度都有一个硬编码的循环之外,有没有其他“简洁”的方法来实现这一点?在


Tags: 方法内存in网格编码forhere数组
3条回答

我想我想出了一个很好的方法来使用内存映射文件:

def carthesian_product_mmap(vectors, filename, mode='w+'):
    '''
    Vectors should be a tuple of `numpy.ndarray` vectors. You could
    also make it more flexible, and include some error checking
    '''        
    # Make a meshgrid with `copy=False` to create views
    grids = np.meshgrid(*vectors, copy=False, indexing='ij')

    # The shape for concatenating the grids from meshgrid
    shape = grid[0].shape + (len(vectors),)

    # Find the "highest" dtype neccesary
    dtype = np.result_type(*vectors)

    # Instantiate the memory mapped file
    M = np.memmap(filename, dtype, mode, shape=shape)

    # Fill the memmap with the grids
    for i, grid in enumerate(grids):
        M[...,i] = grid

    # Make sure the data is written to disk (optional?)
    M.flush()

    # Reshape to put it in the right format for Carthesian product
    return M.reshape((-1, len(vectors)))

但我想知道您是否真的需要存储整个卡提西亚产品(有很多重复数据)。在需要的时候生成产品中的行不是一个选项吗?在

似乎你只想在任意数量的维度上循环。我对此的一般解决方案是使用索引字段和增量索引以及处理溢出。在

示例:

n = 3 # number of dimensions
N = 1 # highest index value per dimension

idx = [0]*n
while True:
    print(idx)
    # increase first dimension
    idx[0] += 1
    # handle overflows
    for i in range(0, n-1):
        if idx[i] > N:
            # reset this dimension and increase next higher dimension
            idx[i] = 0
            idx[i+1] += 1
    if idx[-1] > N:
        # overflow in the last dimension, we are finished
        break

给出:

^{pr2}$

Numpy有类似的内置功能:ndenumerate。在

在纯Python中,可以使用^{}生成iterable集合的笛卡尔积。在

>>> arrays = range(0, 2), range(4, 6), range(8, 10)
>>> list(itertools.product(*arrays))
[(0, 4, 8), (0, 4, 9), (0, 5, 8), (0, 5, 9), (1, 4, 8), (1, 4, 9), (1, 5, 8), (1, 5, 9)]

在Numpy中,可以将^{}(传递sparse=True以避免在内存中扩展产品)与^{}

^{pr2}$

相关问题 更多 >