Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the most common subarray within a numpy array

Tags:

python

numpy

Example data:

array(
  [[ 1.,  1.],
   [ 2.,  1.],
   [ 0.,  1.],
   [ 0.,  0.],
   [ 0.,  0.]])

with a desired result of

>>> [0.,0.]

ie) The most common pair.

Approaches that don't seem to work:

Using statistics as numpy arrays are unhashable.

Using scipy.stats.mode as this returns the mode over each axis, eg) for our example it gives

mode=array([[ 0.,  1.]])
like image 831
draco_alpine Avatar asked Dec 19 '22 00:12

draco_alpine


1 Answers

You can do this efficiently with numpy using the unique function:

pairs, counts = np.unique(a, axis=0, return_counts=True)
print(pairs[counts.argmax()])

Returns: [ 0. 0.]

like image 108
piman314 Avatar answered Jan 12 '23 09:01

piman314