Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 275
Sheikh Arbaz Avatar asked Jun 02 '26 15:06

Sheikh Arbaz


1 Answers

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.

like image 107
Divakar Avatar answered Jun 05 '26 08:06

Divakar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!