473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 14461
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

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

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
2
by: Steve Richter | last post by:
What does the "." mean in the following sql script stmts? use GO if exists (select * from dbo.sysobjects where id = object_id(N'.') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table ....
32
by: Protoman | last post by:
I have a function that calculates the mean of the some numbers; I need it to accept any number of parameters (one needs to be the number of the other parameters) Can you help me out here? Here's...
1
by: Frank Rizzo | last post by:
Some of the classes in the framework are marked as thread-safe in the documentation. In particular the docs say the following: "Any public static (*Shared* in Visual Basic) members of this type...
3
by: Crirus | last post by:
There are some classes to reproduce geometrical shapes like circles or squares? as there is rectangle -- Cheers, Crirus ------------------------------ If work were a good thing, the boss...
8
by: SpreadTooThin | last post by:
import array a = array.array('f', ) print a.mean() print a.std_dev() Is there a way to calculate the mean and standard deviation on array data? Do I need to import it into a Numeric Array...
5
by: prabu | last post by:
Hi all, I want to know, what does the value of the array in the following statement mean and How to print the content of the array SMB_Negotiate using printf? unsigned char SMB_Negotiate = ...
2
by: king imran | last post by:
I am giving u complete code. It works properly But add max and mean functions so that it can work properly. * /LinkList.h/ * #include "Node.h" * /* The LinkList class declaration*/ class**...
0
by: mrchatgroup | last post by:
news from http://www.mrchat.net/myblog/myblog/small-accidents-mean-big-trouble-for-supercollider.html Small Accidents Mean Big Trouble for Supercollider Image Scientists expect startup...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.