3

If I have a matrix A and I want to get the dot product of A with every row of B.

import numpy as np

a = np.array([[1.0, 2.0],
              [3.0, 4.0]])

b = np.array([[1.0, 1.0],
              [2.0, 2.0],
              [3.0, 3.0]])

If the goal was to do it manually (or in a loop):

c = np.array([np.dot(a, b[0])])
c = np.append(c, [np.dot(a, b[1])], axis=0)
c = np.append(c, [np.dot(a, b[2])], axis=0)

print(c)

c = [[  3.   7.]
     [  6.  14.]
     [  9.  21.]]
Divakar
  • 218,885
  • 19
  • 262
  • 358
dranobob
  • 796
  • 1
  • 5
  • 19

1 Answers1

2

With some transposing and matrix-multiplication using np.dot -

a.dot(b.T).T
b.dot(a.T)

With np.einsum -

np.einsum('ij,kj->ki',a,b)

With np.tensordot -

np.tensordot(b,a,axes=((1,1)))

Runtime test -

In [123]: a = np.random.rand(2000, 2000)
     ...: b = np.random.rand(3000, 2000)
     ...: 

In [124]: %timeit a.dot(b.T).T
     ...: %timeit b.dot(a.T)
     ...: %timeit np.einsum('ij,kj->ki',a,b)
     ...: %timeit np.tensordot(b,a,axes=((1,1)))
     ...: 
1 loops, best of 3: 234 ms per loop
10 loops, best of 3: 169 ms per loop
1 loops, best of 3: 7.59 s per loop
10 loops, best of 3: 170 ms per loop
Divakar
  • 218,885
  • 19
  • 262
  • 358