Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding row in Numpy array maximizing some function

What would be most elegant and efficient way to find the index of a row in a Numpy 2d array maximizing some function? In particular, I need to find the row with maximal dot product with a given vector. Say, I have

a = np.array([[1, 2, 3], [3, 2, 1]])
b = np.array([6, 5, 4])

Then the result should be 1 since np.dot(a[1],b) is greater than np.dot(a[0],b).

like image 801
Stas Busygin Avatar asked Dec 20 '25 18:12

Stas Busygin


1 Answers

simply:

>>> np.argmax(a.dot(b))
1

np.argmax docs

like image 93
behzad.nouri Avatar answered Dec 23 '25 08:12

behzad.nouri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!