简体   繁体   中英

Numpy Dot Product of two 2-d arrays in numpy to get 3-d array

Sorry for the badly explained title. I am trying to parallelise a part of my code and got stuck on a dot product. I am looking for an efficient way of doing what the code below does, I'm sure there is a simple linear algebra solution but I'm very stuck:

puy = np.arange(8).reshape(2,4)
puy2 = np.arange(12).reshape(3,4)

print puy, '\n'
print puy2.T

zz = np.zeros([4,2,3])

for i in range(4):
    zz[i,:,:] = np.dot(np.array([puy[:,i]]).T,
                np.array([puy2.T[i,:]]))

One way would be to use np.einsum , which allows you to specify what you want to happen to the indices:

>>> np.einsum('ik,jk->kij', puy, puy2)
array([[[ 0,  0,  0],
        [ 0, 16, 32]],

       [[ 1,  5,  9],
        [ 5, 25, 45]],

       [[ 4, 12, 20],
        [12, 36, 60]],

       [[ 9, 21, 33],
        [21, 49, 77]]])
>>> np.allclose(np.einsum('ik,jk->kij', puy, puy2), zz)
True

这是broadcasting的另一种方式 -

(puy[None,...]*puy2[:,None,:]).T

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM