Vectorized implementation to get the index of the minimum element in each row

I have numpy.ndarray like this:

array([[ 11.18033989, 0. ], [ 8.24621125, 3. ], [ 13.03840481, 5. ], [ 6. , 5.38516481], [ 11.18033989, 3.16227766], [ 0. , 11.18033989], [ 8.06225775, 4.24264069]]) 

I want to get a new array A, such that A [i] is the index of the minimum element in the ith row of the above matrix. For example: array([1, 1, 1, 1, 1, 0, 1])

I can do this with loops with argmin, but since I want this algorithm to be scalable, I am looking for a way to do this using a vectorized implementation. I assume numpy will offer such a function, but I'm new to numpy, so I'm not sure where to look.

+4
source share
1 answer

If X is your array,

 X.argmin(axis=1) 
+11
source

Source: https://habr.com/ru/post/1390311/


All Articles