Python | Harmonic Mean of List
Last Updated :
17 Apr, 2023
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.
Similar Reads
Find Median of List in Python
Sometimes, while working with Python list we can have a problem in which we need to find Median of list. This problem is quite common in the mathematical domains and generic calculations. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + "~" operator This task
4 min read
Mean of Tuple List - Python
We are give list of tuples we need to find the mean of each tuples. For example, a = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] we need to find mean of each tuples so that output should be (4.0, 5.0, 6.0). Using sum() and len()We can find the mean of a tuple list by summing corresponding elements using sum()
2 min read
Python - Mean deviation of Elements
Given a list, the task is to write a Python program to compute how deviated are each of them from its list mean. Examples: Input : test_list = [7, 5, 1, 2, 10, 3] Output : [2.333333333333333, 0.33333333333333304, 3.666666666666667, 2.666666666666667, 5.333333333333333, 1.666666666666667] Explanation
2 min read
Python | Column Mean in tuple list
Sometimes, while working with records, we can have a problem in which we need to average all the columns of a container of lists which are tuples. This kind of application is common in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using sum() + l
4 min read
Python - Multimode of List
Sometimes, while working with Python lists we can have a problem in which we need to find mode in list i.e most frequently occurring character. But sometimes, we can have more than 1 modes. This situation is called multimode. Lets discuss certain ways in which this task can be performed. Method #1 :
2 min read
Python | Average of two lists
The problem of finding a average values in a list is quite common. But sometimes this problem can be extended in two lists and hence becomes a modified problem. This article discusses shorthands by which this task can be performed easily. Letâs discuss certain ways in which this problem can be solve
5 min read
Average of Float Numbers - Python
The task of calculating the average of float numbers in Python involves summing all the numbers in a given list and dividing the total by the number of elements in the list. For example, given a list of float numbers a = [6.1, 7.2, 3.3, 9.4, 10.6, 15.7], the goal is to compute the sum of the numbers
3 min read
Python | Mean of consecutive Sublist
Some of the classical problems in programming domain comes from different categories and one among them is finding the mean of subsets. This particular problem is also common when we need to compute the average and store consecutive group mean. Letâs try different approaches to this problem in pytho
3 min read
Python - Get Matrix Mean
Given a Matrix, Find its mean. Input : test_list = [[5, 6, 7], [7, 5, 6]] Output : 6.0 Explanation : 36 / 6 = 6.0 Input : test_list = [[5, 6, 7, 4, 8]] Output : 6.0 Explanation : 30 / 5 = 6.0 Method #1 : Using list comprehension + sum() + len() + zip() The combination of above functions can be used
7 min read
Python - Kth Index Tuple List Mean
Sometimes, while working with Python tuple, we can have a problem in which we need to compute average of any particular index of tuples in a list. This kind of problem can have application in data domain such as web development. Let's discuss certain ways in which this task can be performed. Input :
6 min read