Python - Most common Combination in Matrix

When it is required to find the most common combination in a matrix, a simple iteration, along with the sort() method and Counter method is used.

Example

Below is a demonstration of the same ?

from collections import Counter
from itertools import combinations

my_list = [[31, 25, 77, 82], [96, 15, 23, 32]]

print("The list is :")
print(my_list)

my_result = Counter()

for elem in my_list:
    if len(elem) < 2:
        continue
    
    elem.sort()
    
    for size in range(2, len(elem) + 1):
        for comb in combinations(elem, size):
            my_result[comb] += 1

my_result = [elem for elem, my_count in my_result.items() if my_count ==
my_result.most_common(1)[0][1]]

print("The result is :")
print(my_result)

Output

The list is :
[[31, 25, 77, 82], [96, 15, 23, 32]]
The result is :
[(25, 31), (25, 77), (25, 82), (31, 77), (31, 82), (77, 82), (25, 31, 77), (25, 31, 82), (25, 77, 82), (31, 77, 82), (25, 31, 77, 82), (15, 23), (15, 32), (15, 96), (23, 32), (23, 96), (32, 96), (15, 23, 32), (15, 23, 96), (15, 32, 96), (23, 32, 96), (15, 23, 32, 96)]

How It Works

The algorithm generates all possible combinations of elements from each sublist and counts their frequency using Counter. Since each sublist appears only once, all combinations have the same frequency (count of 1), making all combinations equally "most common".

Example with Repeated Sublists

To better demonstrate finding the most common combination, let's use a matrix with repeated patterns ?

from collections import Counter
from itertools import combinations

# Matrix with repeated patterns
matrix = [[1, 2, 3], [2, 3, 4], [1, 2, 3], [3, 4, 5]]

print("The matrix is :")
print(matrix)

result = Counter()

for row in matrix:
    if len(row) < 2:
        continue
    
    row_sorted = sorted(row)  # Don't modify original
    
    # Generate all combinations of size 2 and above
    for size in range(2, len(row_sorted) + 1):
        for comb in combinations(row_sorted, size):
            result[comb] += 1

# Find the maximum frequency
max_count = result.most_common(1)[0][1]

# Get all combinations with maximum frequency
most_common_combinations = [comb for comb, count in result.items() 
                           if count == max_count]

print("Most common combinations:")
for comb in most_common_combinations:
    print(f"{comb}: {result[comb]} times")
The matrix is :
[[1, 2, 3], [2, 3, 4], [1, 2, 3], [3, 4, 5]]
Most common combinations:
(1, 2): 2 times
(1, 3): 2 times
(2, 3): 2 times
(1, 2, 3): 2 times

Key Points

  • Counter tracks the frequency of each combination

  • combinations() generates all possible subsets of specified size

  • Sorting ensures consistent ordering for duplicate detection

  • The algorithm considers combinations of all sizes (2 to n elements)

Conclusion

This approach uses Counter and combinations to find the most frequently occurring element combinations across matrix rows. It's useful for pattern detection in data analysis tasks.

Updated on: 2026-03-26T02:10:04+05:30

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements