Python:numpy数组数组外部产品的所有排列的总和

问题描述 投票:1回答:1

我有一个numpy数组阵列Ai和我希望每个外部产品(np.outer(Ai [i],Ai [j]))与缩放倍数相加以产生H.我可以单步执行然后使它们成为tensordot它们具有缩放因子矩阵。我认为事情可以大大简化,但还没有想出一个通用/有效的方法为ND做这件事。如何更容易地生产Arr2D和H?注意:Arr2D可以是64个2D阵列而不是8x8 2D阵列。

Ai = np.random.random((8,101))
Arr2D = np.zeros((Ai.shape[0], Ai.shape[0], Ai.shape[1], Ai.shape[1]))
Arr2D[:,:,:,:] = np.asarray([ np.outer(Ai[i], Ai[j]) for i in range(Ai.shape[0]) 
    for j in range(Ai.shape[0]) ]).reshape(Ai.shape[0],Ai.shape[0],Ai[0].size,Ai[0].size)
arr = np.random.random( (Ai.shape[0] * Ai.shape[0]) )
arr2D = arr.reshape(Ai.shape[0], Ai.shape[0])
H = np.tensordot(Arr2D, arr2D, axes=([0,1],[0,1]))
python numpy numpy-broadcasting numpy-ndarray numpy-einsum
1个回答
1
投票

良好的设置,以利用einsum

np.einsum('ij,kl,ik->jl',Ai,Ai,arr2D,optimize=True)

计时 -

In [71]: # Setup inputs
    ...: Ai = np.random.random((8,101))
    ...: arr = np.random.random( (Ai.shape[0] * Ai.shape[0]) )
    ...: arr2D = arr.reshape(Ai.shape[0], Ai.shape[0])

In [74]: %%timeit # Original soln
    ...: Arr2D = np.zeros((Ai.shape[0], Ai.shape[0], Ai.shape[1], Ai.shape[1]))
    ...: Arr2D[:,:,:,:] = np.asarray([ np.outer(Ai[i], Ai[j]) for i in range(Ai.shape[0]) 
    ...:     for j in range(Ai.shape[0]) ]).reshape(Ai.shape[0],Ai.shape[0],Ai[0].size,Ai[0].size)
    ...: H = np.tensordot(Arr2D, arr2D, axes=([0,1],[0,1]))
100 loops, best of 3: 4.5 ms per loop

In [75]: %timeit np.einsum('ij,kl,ik->jl',Ai,Ai,arr2D,optimize=True)
10000 loops, best of 3: 146 µs per loop

30x+加速!

© www.soinside.com 2019 - 2024. All rights reserved.