If you have spare memory, for some sizes of your array, I guess several columns and many rows, it can pay off to make a more countless intensive solution:
>>> rows, cols = matrix.shape >>> matches = np.empty((rows, cols, cols, 2), dtype=str) >>> matches[..., 0] = matrix[:, None, :] >>> matches[..., 1] = matrix[:, :, None] >>> matches = matches.view('S2') >>> matches = matches.reshape((rows, cols, cols))
And now in matches[:, i, j] you have unique pairs between columns i and j , and you can do the following:
>>> unique, idx = np.unique(matches[:, 0, 1], return_inverse=True) >>> counts = np.bincount(idx) >>> unique array(['AA', 'GL', 'LG'], dtype='|S2') >>> counts array([2, 1, 1])
Jaime source share