def pair_counts(a, b):
    n = a.shape[0]
counts_a = np.bincount(a)
counts_b = np.bincount(b)
sorter_a = np.argsort(a)
n11 = 0
same_a_offset = np.cumsum(counts_a)
for indices in np.split(sorter_a, same_a_offset):
    b_check = b[indices]
n11 += np.count_nonzero(b_check == b_check[:, (None)])
n11 = (n11 - n) // 2
n10 = (np.sum(counts_a ** 2) - n) // 2 - n11
n01 = (np.sum(counts_b ** 2) - n) // 2 - n11
n00 = n ** 2 - n - n11 - n10 - n01
return n11, n10, n01, n00
Высокопроизводительное всеобщее сравнение векторов в Python