Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vectorized implementation to get index of minimum element in each row

Tags:

python

numpy

I have a 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 minimum element in ith row of above matrix. Such as this: array([1, 1, 1, 1, 1, 0, 1])

I can do it with for loops with argmin, but since I want this algorithm to be scalable, I am looking for a way to do it using a vectorized implementation. I guess numpy would offer such a feature, but I am new to numpy, so I am not sure where to look.

like image 577
yasar Avatar asked Dec 21 '22 03:12

yasar


1 Answers

If X is your array,

X.argmin(axis=1)
like image 89
Fred Foo Avatar answered May 09 '23 23:05

Fred Foo