numpy calculates the degree of repetition of two arrays

  • 2021-01-22 05:14:09
  • OfStack

Recently, there is a requirement to calculate the degree of repetition of two arrays. The trouble is that the elements of a single array may be repeated. The processing idea is as follows:

1. Find duplicate elements

2. Element number statistics, using np.bincount conversion, that is, element number statistics to element conversion index

3. Count the number of matches of the same element

The specific code is as follows:


# arr1, arr2 Are all np.array type 
#  Finding duplicate elements (intersections) 
inters = np.intersect1d(arr1, arr2)
#  Index conversion of number of elements 
bc1 = np.bincount(arr1)
bc2 = np.bincount(arr2)
#  Count the number of matches for the same element 
same_count_list = [min(bc1[x], bc2[x]) for x in inters]
same_count = sum(same_count_list)

Related articles: