471,344 Members | 1,248 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 14313 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
KaezarRex
52 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
KaezarRex
52 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

 70 posts views Thread by Roy Yao | last post: by 2 posts views Thread by Steve Richter | last post: by 32 posts views Thread by Protoman | last post: by 1 post views Thread by Frank Rizzo | last post: by 3 posts views Thread by Crirus | last post: by 8 posts views Thread by SpreadTooThin | last post: by 5 posts views Thread by prabu | last post: by 2 posts views Thread by king imran | last post: by reply views Thread by mrchatgroup | last post: by reply views Thread by RamLakshmanan | last post: by reply views Thread by KayQ | last post: by reply views Thread by Ronak mishra | last post: by reply views Thread by Yacine Si Tayeb | last post: by reply views Thread by leo001 | last post: by reply views Thread by leo001 | last post: by reply views Thread by chloesad91 | last post: by reply views Thread by SwissProgrammer | last post: by reply views Thread by kjhyder | last post: by