Python | Matrix Tuple pair Column product
Sometimes, we encounter a problem where we deal with a complex type of matrix column product in which we are given a tuple and we need to perform the product of its like elements. This has a good application in Machine Learning domain. Let’s discuss certain ways in which this can be done.
Method #1 : Using zip() + list comprehension This problem can be resolved using the list comprehension which could perform the column product logic and zip function is used to bind the elements as a result and also at the time of vertical product.
Python3
# Python3 code to demonstrate # Matrix Tuple pair Column product # using list comprehension + zip() # getting Product def prod(val) : res = 1 for ele in val: res * = ele return res # initializing list test_list = [[( 1 , 4 ), ( 2 , 3 ), ( 5 , 2 )], [( 3 , 7 ), ( 1 , 9 ), ( 10 , 5 )]] # printing original list print ( "The original list : " + str (test_list)) # using list comprehension + zip() # Matrix Tuple pair Column product res = [ tuple (prod(j) for j in zip ( * i)) for i in zip ( * test_list)] # print result print ( "The product of columns of tuple list : " + str (res)) |
The original list : [[(1, 4), (2, 3), (5, 2)], [(3, 7), (1, 9), (10, 5)]] The product of columns of tuple list : [(3, 28), (2, 27), (50, 10)]
Time Complexity: O(m*n), where m and n is the length of rows and columns respectively of the input list. This is because we’re using the zip() + list comprehension which has a time complexity of O(m*n) in the worst case.
Auxiliary Space: O(k), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2 : Using zip() + map() The task of binding the column elements can also be performed using the map function and the zip function performs the task of binding the product tuples. Both logics bound by list comprehension.
Python3
# Python3 code to demonstrate # Matrix Tuple pair Column product # using zip() + map() # getting Product def prod(val) : res = 1 for ele in val: res * = ele return res # initializing list test_list = [[( 1 , 4 ), ( 2 , 3 ), ( 5 , 2 )], [( 3 , 7 ), ( 1 , 9 ), ( 10 , 5 )]] # printing original list print ( "The original list : " + str (test_list)) # using zip() + map() # Matrix Tuple pair Column product res = [ tuple ( map (prod, zip ( * i))) for i in zip ( * test_list)] # print result print ( "The product of columns of tuple list : " + str (res)) |
The original list : [[(1, 4), (2, 3), (5, 2)], [(3, 7), (1, 9), (10, 5)]] The product of columns of tuple list : [(3, 28), (2, 27), (50, 10)]
Time Complexity: O(m*n), where m and n is the length of rows and columns respectively of the input list.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3 : Using numpy()
Note: Install numpy module using command “pip install numpy”
This approach uses the numpy library to perform the column-wise product of the matrix. The numpy.prod() function is used to find the product of all elements in the array along a given axis. In this case, the axis is set to 0 (columns) so it returns the product of each column.
Python3
# Using numpy library import numpy as np #initializing list test_list = [[( 1 , 4 ), ( 2 , 3 ), ( 5 , 2 )], [( 3 , 7 ), ( 1 , 9 ), ( 10 , 5 )]] #converting list of tuples to numpy array arr = np.array(test_list) #column wise product using numpy result = np.prod(arr, axis = 0 ) print ( "The product of columns of tuple list : " + str (result)) #This code is contributed by Edula Vinay Kumar Reddy |
Output:
The product of columns of tuple list : [[ 3 28]
[ 2 27]
[50 10]]
Time complexity of this approach is O(m * n) where m is the number of rows and n is the number of columns in the matrix.
Auxiliary Space is O(m * n) as a new numpy array is created.