numpy 任意维数组中任意维坐标的笛卡尔积

ztigrdn8  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(97)

这与Numpy: cartesian product of x and y array points into single array of 2D points有点关系。
我正在寻找一种简洁的方法来创建两个任意维数的数组的乘积。
示例如下:
类似于相关的线程,我想

x = numpy.array([1,2,3]) #ndim 1
y = numpy.array([4,5])  #ndim 1
cartesian_product(x,y) == numpy.array([[[1, 4],
                                        [2, 4],
                                        [3, 4]],
                                       [[1, 5],
                                        [2, 5],
                                        [3, 5]]]) #ndim "2" = ndim x + ndim y

字符串
结果数组是二维的,因为[1,4],[2,4]等是坐标,因此不是真正的维度。为了推广,最好将x/y写为[[1],[2],[3]]。
上述等于

numpy.dstack(numpy.meshgrid(x,y))


但我也想

x2 = numpy.array([[1,1], [2,2], [3,3]]) #ndim "1", since [1, 1] is a coordinate
cartesian_product(x2,y) == numpy.array([[[1, 1, 4],
                                         [2, 2, 4],
                                         [3, 3, 4]],

                                        [[1, 1, 5],
                                         [2, 2, 5],
                                         [3, 3, 5]]]) #ndim 2 = ndim x2 + ndim y

y2 = numpy.array([[10, 11], [20, 21]]) #ndim 1
(cartesian_product(x2, y2) ==
numpy.array([[[1, 1, 10, 11],
              [2, 2, 10, 11],
              [3, 3, 10, 11]],

             [[1, 1, 20, 21],
              [2, 2, 20, 21],
              [3, 3, 20, 21]]])) #ndim x2 + ndim y2

x3 = numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) #ndim 2
(cartesian_product(x3, y) ==
numpy.array([[[[1, 2, 4], [3, 4, 4]], [[5, 6, 4], [7, 8, 4]]],
             [[[1, 2, 5], [3, 4, 5]], [[5, 6, 5], [7, 8, 5]]]]) #ndim 3


想象我在做什么:正如我所说的,[[0,0],[0,1],[1,1],[1,0]]应该被解释为一个一维坐标列表,对应于一条直线。如果我用[1,2,3,4]做一个笛卡尔积,我会在z方向上挤出这条直线,变成一个表面(即二维)。但现在数组当然是三维的。
我想我可以用循环来解决这个问题,但是有没有办法用numpy/scipy工具来实现呢?

j0pj023g

j0pj023g1#

一种内存高效的方式是广播分配:

def cartesian_product(x, y):
    if x.ndim < 2:
        x = np.atleast_2d(x).T
    if y.ndim < 2:
        y = np.atleast_2d(y).T

    sx, sy = x.shape, y.shape
    sz = sy[:-1] + sx[:-1] + (sy[-1] + sx[-1],)
    z = np.empty(sz, np.result_type(x, y))

    # Broadcasted assignment
    z[...,:sx[-1]] = x
    z[...,sx[-1]:] = y.reshape(sy[:-1] + (x.ndim-1)*(1,) + (sy[-1],))

    return z

字符串
如果你需要广播的细节,this page已经覆盖了。

icomxhvb

icomxhvb2#

基于@user6758673的答案,一个更通用的方法(适用于任何维度的数组)是:

import numpy as np

def cartesian_product(x, y, *other_arrays):
    if x.ndim < 2:
        x = np.atleast_2d(x).T
    if y.ndim < 2:
        y = np.atleast_2d(y).T

    sx, sy = x.shape, y.shape
    sz = sy[:-1] + sx[:-1] + (sy[-1] + sx[-1],)
    z = np.empty(sz, np.result_type(x, y))

    # Broadcasted assignment
    z[...,:sx[-1]] = x
    z[...,sx[-1]:] = y.reshape(sy[:-1] + (x.ndim-1)*(1,) + (sy[-1],))

    answer = z.reshape((x.shape[0]*y.shape[0], x.shape[1]+y.shape[1]))
    if len(other_arrays) > 0:
        return cartesian_product(answer, *other_arrays)

    return answer

# Usage example:
array1 = np.array([[1,2], [3,4], [5,6]])
array2 = np.array([['a'], ['b']])
array3 = np.array([[8], [9]])
arrays = [ array1, array2, array3 ]
cartesian_product(*arrays) # or simply: cartesian_product(array1, array2, array3)
# array([['1', '2', 'a', '8'],
#     ['3', '4', 'a', '8'],
#     ['5', '6', 'a', '8'],
#     ['1', '2', 'b', '8'],
#     ['3', '4', 'b', '8'],
#     ['5', '6', 'b', '8'],
#     ['1', '2', 'a', '9'],
#     ['3', '4', 'a', '9'],
#     ['5', '6', 'a', '9'],
#     ['1', '2', 'b', '9'],
#     ['3', '4', 'b', '9'],
#     ['5', '6', 'b', '9']], dtype='<U21')

字符串

相关问题