Home > Back-end >  How to calculate dot product of array and every array on nested array?
How to calculate dot product of array and every array on nested array?

Time:10-11

I have and array x: array([1,4,5]) and y: array([[1,1,2], [4,2,1], [0,0,1]])

I want to calculate dot product of x with every array in y. so output must shave shape of y. it must be:

array([[15], [17], [5]])

How to do that?

CodePudding user response:

You can use the matrix multiplication operator introduced in PEP 465:

>>> y @ x
[15 17  5]

And resize it using np.newaxis:

>>> (y @ x)[:, np.newaxis]
[[15]
 [17]
 [ 5]]

This is synonymous to np.dot:

>>> np.dot(y, x)[:, np.newaxis]
[[15]
 [17]
 [ 5]]
  • Related