Geometrical mean

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DDCane
    New Member
    • Oct 2007
    • 16

    Geometrical mean

    I need to define a function "geomean(number s)" 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.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by DDCane
    I need to define a function "geomean(number s)" 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?
    [code=python]
    def geomean(num_lis t):
    return sum(num_list) ** (1.0/len(num_list))
    [/code]

    Comment

    • KaezarRex
      New Member
      • Sep 2007
      • 52

      #3
      Originally posted by DDCane
      I need to define a function "geomean(number s)" 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)
      [CODE=python]def geomean(numbers ):
      product = 1
      for n in numbers:
      product *= n
      return product ** (1.0/len(numbers))[/CODE]

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by KaezarRex
        I hate to be a stickler on math, but the geometric mean of [1, 2, 3, 4] is (1*2*3*4)^(1/4)
        [CODE=python]def geomean(numbers ):
        product = 1
        for n in numbers:
        product *= n
        return product ** (1.0/len(numbers))[/CODE]
        Good work KaezarRex. Here is the same calculation using reduce():[code=Python]def geomean(nums):
        return (reduce(lambda x, y: x*y, nums))**(1.0/len(nums))

        nums = (1,2,3,4,5)
        print geomean(nums)[/code]

        >>> 2.6051710847

        Comment

        • KaezarRex
          New Member
          • Sep 2007
          • 52

          #5
          Originally posted by bvdet
          Good work KaezarRex. Here is the same calculation using reduce():[code=Python]def geomean(nums):
          return (reduce(lambda x, y: x*y, nums))**(1.0/len(nums))

          nums = (1,2,3,4,5)
          print geomean(nums)[/code]

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

          Comment

          Working...