Here's a use approach NumPy viewsthat should be pretty effective -
def mode_rows(a):
a = np.ascontiguousarray(a)
void_dt = np.dtype((np.void, a.dtype.itemsize * np.prod(a.shape[1:])))
_,ids, count = np.unique(a.view(void_dt).ravel(), \
return_index=1,return_counts=1)
largest_count_id = ids[count.argmax()]
most_frequent_row = a[largest_count_id]
return most_frequent_row
Run Example -
In [45]: # Let have a random arrayb with three rows(2,4,8) and two rows(1,7)
...: # being duplicated. Thus, the most freequent row must be 2 here.
...: a = np.random.randint(0,9,(9,5))
...: a[4] = a[8]
...: a[2] = a[4]
...:
...: a[1] = a[7]
...:
In [46]: a
Out[46]:
array([[8, 8, 7, 0, 7],
[7, 8, 2, 6, 1],
[2, 2, 5, 7, 6],
[6, 5, 8, 8, 5],
[2, 2, 5, 7, 6],
[5, 7, 3, 6, 3],
[2, 8, 7, 2, 0],
[7, 8, 2, 6, 1],
[2, 2, 5, 7, 6]])
In [47]: mode_rows(a)
Out[47]: array([2, 2, 5, 7, 6])