baxx
baxx

Reputation: 4725

Find dot product with every row in numpy

I'm wondering if there's a more "numpy"ish approach to the following:

T = np.array([[0.9, 0.44], [0.6, 0.8]])
L = np.array([[0.83, 0.55], [0.2, 0.98]])
x = np.array([[1, 2], [3, 4], [6, 7]])
transformed = [np.dot(T, k) for k in x]

This works, but it seems like something that might be built into numpy, in pandas there's transform/apply for similar operations. What's the recommended approach with numpy?

Upvotes: 2

Views: 400

Answers (1)

Ehsan
Ehsan

Reputation: 12407

As suggested by @Divakar, you can use dot to get an array:

T.dot(x.T).T

Which is same as:

np.dot(T,x.T).T

Same as this (suggested by @MrNobody33):

np.dot(x,T.T)
#or x.dot(T.T)

output:

[[1.78 2.2 ]
 [4.46 5.  ]
 [8.48 9.2 ]]

If you necessarily need a list of arrays like you have in your code (which in my opinion, you should use arrays only), you can convert it like this:

[x for x in T.dot(x.T).T]
#[array([1.78, 2.2 ]), array([4.46, 5.  ]), array([8.48, 9.2 ])]

Upvotes: 1

Related Questions