![]() |
VOOZH | about |
Sometimes, we encounter a problem where we deal with a complex type of matrix column summation in which we are given a tuple and we need to perform the summation 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 summation logic and zip function is used to bind the elements as a result and also at the time of vertical summation.
The original list : [[(1, 4), (2, 3), (5, 2)], [(3, 7), (1, 9), (10, 5)]] The summation of columns of tuple list : [(4, 11), (3, 12), (15, 7)]
Time complexity: O(nm), where n is the number of sublists in the input list and m is the length of the sublists (assuming that the length of all sublists is the same).
Auxiliary space: O(nm), since it creates a new list of tuples with the same dimensions as the 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 summation tuples. Both logics bound by list comprehension.
The original list : [[(1, 4), (2, 3), (5, 2)], [(3, 7), (1, 9), (10, 5)]] The summation of columns of tuple list : [(4, 11), (3, 12), (15, 7)]
Time Complexity : O(m*n), where m is the number of rows and n is the number of columns in the list of tuples.
Auxiliary Space : O(n), where n is the number of columns in the list of tuples.
Method #3: Using for loop and append()
Using a for loop and append() method to iterate through the tuples in each list and sum up the elements in each column.
The summation of columns of tuple list : [(4, 11), (3, 12), (15, 7)]
Time Complexity: O(n^2), where n is the number of elements in the longest inner tuple. This is because the code uses two nested loops to iterate through the tuples in the input list.
Auxiliary Space: O(n), where n is the number of elements in the longest inner tuple. This is because the code creates a new tuple for each column in the input list to store the sum of the elements in that column.
Method #4: Using NumPy library
NumPy is a popular library for numerical computations in Python. It provides many efficient and convenient functions for working with arrays, matrices, and other data structures. We can use the NumPy library to solve the given
step-by-step explanation of the program:
OUTPUT:
The summation of columns of tuple list : [[ 4 11] [ 3 12] [15 7]]
Time complexity: O(n^2), where n is the length of the inner tuples. This is because we need to iterate over all the elements in the input list and perform a constant number of operations (addition) on each element.
Auxiliary space: O(n), where n is the length of the inner tuples. This is because we need to create a new array of zeros to initialize the result, and a new array to store the transposed input. However, these arrays have a fixed size that depends only on the length of the inner tuples, so the space complexity is O(n).
Method 5: Using the built-in function reduce() from the functools module.
Step-by-step approach:
The original list : [[(1, 4), (2, 3), (5, 2)], [(3, 7), (1, 9), (10, 5)]] The summation of columns of tuple list : [(4, 11), (3, 12), (15, 7)]
Time complexity: O(nm), where n is the number of rows and m is the number of columns in the input list of tuples.
Auxiliary space: O(m), where m is the number of columns in the input list of tuples.