Python | Harmonic Mean of List
While working with Python, we can have a problem in which we need to find harmonic mean of a list cumulative. This problem is common in Data Science domain. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using loop + formula The simpler manner to approach this problem is to employ the formula for finding harmonic mean and perform using loop shorthands. This is the most basic approach to solve this problem.
Python3
test_list = [ 6 , 7 , 3 , 9 , 10 , 15 ]
print ("The original list is : " + str (test_list))
sum = 0
for ele in test_list:
sum + = 1 / ele
res = len (test_list) / sum
print ("The harmonic mean of list is : " + str (res))
|
Output :
The original list is : [6, 7, 3, 9, 10, 15]
The harmonic mean of list is : 6.517241379310345
Time Complexity: O(n) where n is the number of elements in the string list. The loop + formula is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) constant additional space is required.
Method #2 : Using statistics.harmonic_mean() This task can also be performed using inbuilt function of harmonic_mean(). This is new in Python versions >= 3.8.
Python3
import statistics
test_list = [ 6 , 7 , 3 , 9 , 10 , 15 ]
print ("The original list is : " + str (test_list))
res = statistics.harmonic_mean(test_list)
print ("The harmomin mean of list is : " + str (res))
|
Output :
The original list is : [6, 7, 3, 9, 10, 15]
The harmonic mean of list is : 6.517241379310345
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3 : Using reduce+lambda
Approach
the reduce() function from the functools module along with a lambda function that performs the reciprocal sum of the list elements.
Algorithm
1. Start with an initial value of 0 for the reduction
2. For each number in the list, add 1 divided by the number to the reduction
3. Divide the length of the list by the result of the reduction to get the harmonic mean
Python3
from functools import reduce
numbers = [ 1 , 2 , 3 , 4 , 5 ]
hm = len (numbers) / reduce ( lambda x, y: x + ( 1 / y), numbers, 0 )
print (hm)
|
Time complexity: O(n), where n is the length of the numbers list. The lambda function has a constant time complexity of O(1), as it performs a simple arithmetic operation.
Auxiliary Space: O(n),where n is the length of the numbers list. This is because the reduce() function creates an intermediate result for each element of the numbers list, which requires additional memory.
Method #4: Using list comprehension:
Algorithm:
1.Initialize the list.
2.Calculate the reciprocal of each element in the list.
3.Sum all the reciprocals.
4.Divide the length of the list by the sum obtained in step 3.
5.The result obtained in step 4 is the harmonic mean of the list.
Python3
test_list = [ 6 , 7 , 3 , 9 , 10 , 15 ]
print ( "The original list is : " + str (test_list))
res = len (test_list) / sum ([ 1 / ele for ele in test_list])
print ( "The harmonic mean of list is: " + str (res))
|
Output
The original list is : [6, 7, 3, 9, 10, 15]
The harmonic mean of list is: 6.517241379310345
Time Complexity: O(n), where n is the length of the list. This is because the algorithm iterates through the list once to calculate the reciprocal of each element and then sums the reciprocals.
Auxiliary Space: O(1), because the algorithm only uses a constant amount of extra memory to store the sum and the length of the list.
Last Updated :
17 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...