471,344 Members | 1,248 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

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
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

70 posts views Thread by Roy Yao | last post: by
2 posts views Thread by Steve Richter | 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
reply views Thread by mrchatgroup | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.