Search code examples
arrayspython-3.xpython-2.7numpymode

Max frequency 1d array in a 2d numpy array


I've a 2d numpy array:

array([[21, 17, 11],
   [230, 231, 232],
   [21, 17, 11]], dtype=uint8)

I want to find the 1d array which is more frequent. For the above 2d array it is: [21, 17, 11]. It is something like mode in stats.


Solution

  • We can use np.unique with its optional arg return_counts to get the counts for each unique row and finally get the argmax() to choose the one with the max count -

    # a is input array
    unq, count = np.unique(a, axis=0, return_counts=True)
    out = unq[count.argmax()]
    

    For uint8 type data, we can also convert to 1D by reducing each row to a scalar each and then use np.unique -

    s = 256**np.arange(a.shape[-1])
    _, idx, count = np.unique(a.dot(s), return_index=True, return_counts=True)
    out = a[idx[count.argmax()]]
    

    If we are working with color images that are 3D (the last axis being the color channel) and want to get the most dominant color, we need to reshape with a.reshape(-1,a.shape[-1]) and then feed it to the proposed methods.