473,289 Members | 1,953 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,289 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 14453
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.