Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the majority element per row in numpy matrix

Is there a quick way (so no for loops) to find the majority element per row in a numpy array and create a new array out of this?

For example, if you have the following numpy array:

X = 
[[ 1.  1.  1.]
 [ 1.  0.  1.]
 [ 1.  0.  1.]
 [ 1.  1.  1.]
 [ 1.  0.  1.]
 [ 1.  0.  1.]
 [ 0.  0.  0.]
 [ 1.  1.  1.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]

you could do get_majority(X), which would output

[ 1.  1.  1.  1.  1.  1.  0.  1.  0.  0.  0.]

I've tried doing this by looping over the matrix and using a Counter from collections, but that's extremely slow for large matrices, so I want to find a vectorised way to do it.

like image 231
user5368737 Avatar asked Jan 04 '23 08:01

user5368737


1 Answers

You could use Scipy's mode -

from scipy.stats import mode

mode(X, axis=-1)[0]
like image 125
Divakar Avatar answered Jan 06 '23 22:01

Divakar