Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Matrix elements Frequencies Counter

Python – Matrix elements Frequencies Counter

Sometimes, while working with python Matrix, we can have a problem in which we need to find frequencies of all elements in Matrix. This kind of problem can have application in many domains. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using Counter() + sum() + map() The combination of above methods can be used to perform this task. In this, we perform task of counting elements using Counter() and extending the logic to each row is performed using sum() and map(). 

Python3




# Python3 code to demonstrate
# Matrix elements Frequencies Counter
# using Counter() + sum() + map()
from collections import Counter
 
# Initializing list
test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Matrix elements Frequencies Counter
# using Counter() + sum() + map()
res = dict(sum(map(Counter, test_list), Counter()))
 
# printing result
print ("The frequencies dictionary is : " + str(res))


Output : 

The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]]
The frequencies dictionary is : {2: 1, 4: 2, 5: 3, 6: 2, 7: 1}

Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.

Method #2 : Using chain() + Counter() The combination of above functions can be used to perform this task. In this, we flatten the matrix using chain() and compute frequency using Counter(). 

Python3




# Python3 code to demonstrate
# Matrix elements Frequencies Counter
# using Counter() + chain()
from collections import Counter
import itertools
 
# Initializing list
test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Matrix elements Frequencies Counter
# using Counter() + chain()
res = dict(Counter(itertools.chain(*test_list)))
 
# printing result
print ("The frequencies dictionary is : " + str(res))


Output : 

The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]]
The frequencies dictionary is : {2: 1, 4: 2, 5: 3, 6: 2, 7: 1}

Method #3 : Using extend(),set() and count() methods

Python3




# Python3 code to demonstrate
# Matrix elements Frequencies Counter
 
# Initializing list
test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
res=dict()
a=[]
for i in test_list:
    a.extend(i)
b=set(a)
for i in b:
    res[i]=a.count(i)
 
 
# printing result
print ("The frequencies dictionary is : " + str(res))


Output

The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]]
The frequencies dictionary is : {2: 1, 4: 2, 5: 3, 6: 2, 7: 1}

Time Complexity: O(nlogn)

Auxiliary Space: O(n)

Method #4 : Using extend(),set() and operator.countOf() methods

Python3




# Python3 code to demonstrate
# Matrix elements Frequencies Counter
import operator as op
# Initializing list
test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
res=dict()
a=[]
for i in test_list:
    a.extend(i)
b=set(a)
for i in b:
    res[i]=op.countOf(a,i)
 
 
# printing result
print ("The frequencies dictionary is : " + str(res))


Output

The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]]
The frequencies dictionary is : {2: 1, 4: 2, 5: 3, 6: 2, 7: 1}

Time Complexity: O(N*N)

Auxiliary Space:  O(N*N)

Method #5 :Using itertools.chain() and collections.Counter(): 

Algorithm :

  1. Import the itertools and collections modules.
  2. Initialize a list test_list with the input matrix.
  3. Flatten the input matrix using itertools.chain.from_iterable().
  4. Count the frequencies of each element in the flattened list using collections.Counter().
  5. Store the result in a dictionary res.
  6. Print the result.

Python3




import itertools
import collections
 
test_list = [[4, 5, 6], [2, 4, 5], [6, 7, 5]]
# printing original list
print("The original list is : " + str(test_list))
 
# flatten the list using chain()
flat_list = list(itertools.chain.from_iterable(test_list))
 
# count the frequencies using Counter()
res = collections.Counter(flat_list)
 
print("The frequencies dictionary is : " + str(res))
#This code is contributed by Jyothi pinjala


Output

The original list is : [[4, 5, 6], [2, 4, 5], [6, 7, 5]]
The frequencies dictionary is : Counter({5: 3, 4: 2, 6: 2, 2: 1, 7: 1})

Time complexity: The time complexity of this algorithm is O(n), where n is the total number of elements in the input matrix. The itertools.chain.from_iterable() function and the collections.Counter() function both have linear time complexity with respect to the number of elements in the input list.

Space complexity: The space complexity of this algorithm is O(n), where n is the total number of elements in the input matrix. This is because the flattened list and the dictionary both require memory proportional to the number of elements in the input matrix. Note that the space complexity could be reduced by using an in-place counting algorithm instead of creating a dictionary, but this would require more complex code.

RELATED ARTICLES

Most Popular

Recent Comments