473,382 Members | 1,731 Online

# Geometrical mean

16
I need to define a function "geomean(numbers)" that takes all the numbers in the list, adds then together then takes the sum of the numbers and puts it to the power of 1/how many numbers there are in the list. the resulty should be something like this:

geomean([1,2,3,4])

1+2+3+4=10

10^(1/4)

do yall get what im trying to say? when i read what i just wrote its hard for ME to follow it.

if u look up geometrical mean on wikepedia you'll get a better understanding of what im trying to do.
Oct 24 '07 #1
4 14458
ilikepython
844 Expert 512MB
I need to define a function "geomean(numbers)" that takes all the numbers in the list, adds then together then takes the sum of the numbers and puts it to the power of 1/how many numbers there are in the list. the resulty should be something like this:

geomean([1,2,3,4])

1+2+3+4=10

10^(1/4)

do yall get what im trying to say? when i read what i just wrote its hard for ME to follow it.

if u look up geometrical mean on wikepedia you'll get a better understanding of what im trying to do.
Like this?
Expand|Select|Wrap|Line Numbers
1. def geomean(num_list):
2.     return sum(num_list) ** (1.0/len(num_list))
3.
Oct 25 '07 #2
I need to define a function "geomean(numbers)" that takes all the numbers in the list, adds then together then takes the sum of the numbers and puts it to the power of 1/how many numbers there are in the list. the resulty should be something like this:

geomean([1,2,3,4])

1+2+3+4=10

10^(1/4)

do yall get what im trying to say? when i read what i just wrote its hard for ME to follow it.

if u look up geometrical mean on wikepedia you'll get a better understanding of what im trying to do.
I hate to be a stickler on math, but the geometric mean of [1, 2, 3, 4] is (1*2*3*4)^(1/4)
Expand|Select|Wrap|Line Numbers
1. def geomean(numbers):
2.     product = 1
3.     for n in numbers:
4.         product *= n
5.     return product ** (1.0/len(numbers))
Oct 25 '07 #3
bvdet
2,851 Expert Mod 2GB
I hate to be a stickler on math, but the geometric mean of [1, 2, 3, 4] is (1*2*3*4)^(1/4)
Expand|Select|Wrap|Line Numbers
1. def geomean(numbers):
2.     product = 1
3.     for n in numbers:
4.         product *= n
5.     return product ** (1.0/len(numbers))
Good work KaezarRex. Here is the same calculation using reduce():
Expand|Select|Wrap|Line Numbers
1. def geomean(nums):
2.     return (reduce(lambda x, y: x*y, nums))**(1.0/len(nums))
3.
4. nums = (1,2,3,4,5)
5. print geomean(nums)
>>> 2.6051710847
Oct 25 '07 #4
Good work KaezarRex. Here is the same calculation using reduce():
Expand|Select|Wrap|Line Numbers
1. def geomean(nums):
2.     return (reduce(lambda x, y: x*y, nums))**(1.0/len(nums))
3.
4. nums = (1,2,3,4,5)
5. print geomean(nums)
>>> 2.6051710847
Nice! I knew there had to be a way to use reduce, but I have never used lambda before. Thanks for the example.
Oct 25 '07 #5