两个numpy数组中所有行的组合


问题内容

我有两个数组,例如具有shape (3,2),另一个具有shape
(10,7)。我想要两个数组的所有组合,以便最终得到9列数组。换句话说,我想要第一个数组的每一行与第二个数组的行的所有组合。

我怎样才能做到这一点?据我所知,我没有正确使用Meshgrid。

根据以前的帖子,我的印象是

a1 = np.zeros((10,7))
a2 = np.zeros((3,2))
r = np.array(np.meshgrid(a1, a2)).T.reshape(-1, a1.shape[1] + a2.shape[1])

会起作用,但这使我的尺寸为(84,10)。


问题答案:

方法1

随着集中表现在这里是用一种方法array-initializationelement-broadcasting工作分配-

m1,n1 = a1.shape
m2,n2 = a2.shape
out = np.zeros((m1,m2,n1+n2),dtype=int)
out[:,:,:n1] = a1[:,None,:]
out[:,:,n1:] = a2
out.shape = (m1*m2,-1)

说明:

诀窍在于两个步骤:

out[:,:,:n1] = a1[:,None,:]
out[:,:,n1:] = a2

第1步 :

In [227]: np.random.seed(0)

In [228]: a1 = np.random.randint(1,9,(3,2))

In [229]: a2 = np.random.randint(1,9,(2,7))

In [230]: m1,n1 = a1.shape
     ...: m2,n2 = a2.shape
     ...: out = np.zeros((m1,m2,n1+n2),dtype=int)
     ...:

In [231]: out[:,:,:n1] = a1[:,None,:]

In [232]: out[:,:,:n1]
Out[232]: 
array([[[5, 8],
        [5, 8]],

       [[6, 1],
        [6, 1]],

       [[4, 4],
        [4, 4]]])

In [233]: a1[:,None,:]
Out[233]: 
array([[[5, 8]],

       [[6, 1]],

       [[4, 4]]])

因此,基本上,我们分配的元素是a1保持第一轴与输出中的相应轴对齐,同时使沿输出数组第二轴的元素以广播方式填充,该方式与沿该轴newaxis添加的方式相对应a1。这是这里的症结所在,它带来了性能,因为我们没有分配额外的内存空间,否则我们将需要使用显式的重复/平铺方法来分配它们。

第2步 :

In [237]: out[:,:,n1:] = a2

In [238]: out[:,:,n1:]
Out[238]: 
array([[[4, 8, 2, 4, 6, 3, 5],
        [8, 7, 1, 1, 5, 3, 2]],

       [[4, 8, 2, 4, 6, 3, 5],
        [8, 7, 1, 1, 5, 3, 2]],

       [[4, 8, 2, 4, 6, 3, 5],
        [8, 7, 1, 1, 5, 3, 2]]])

In [239]: a2
Out[239]: 
array([[4, 8, 2, 4, 6, 3, 5],
       [8, 7, 1, 1, 5, 3, 2]])

在这里,我们基本上是 沿输出数组的第一个轴广播该 a2,而没有明确地进行重复复制。

样本输入,输出的完整性-

In [242]: a1
Out[242]: 
array([[5, 8],
       [6, 1],
       [4, 4]])

In [243]: a2
Out[243]: 
array([[4, 8, 2, 4, 6, 3, 5],
       [8, 7, 1, 1, 5, 3, 2]])

In [244]: out
Out[244]: 
array([[[5, 8, 4, 8, 2, 4, 6, 3, 5],
        [5, 8, 8, 7, 1, 1, 5, 3, 2]],

       [[6, 1, 4, 8, 2, 4, 6, 3, 5],
        [6, 1, 8, 7, 1, 1, 5, 3, 2]],

       [[4, 4, 4, 8, 2, 4, 6, 3, 5],
        [4, 4, 8, 7, 1, 1, 5, 3, 2]]])

方法#2

另一个tiling/repeating-

parte1 = np.repeat(a1[:,None,:],m2,axis=0).reshape(-1,m2)
parte2 = np.repeat(a2[None],m1,axis=0).reshape(-1,n2)
out = np.c_[parte1, parte2]