calculating standard deviation

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 2
Reputation: lcc_551 is an unknown quantity at this point 
Solved Threads: 0
lcc_551 lcc_551 is offline Offline
Newbie Poster

calculating standard deviation

 
0
  #1
Feb 20th, 2009
Hi, I'm in first year computer science and could use some help on a program where I have to calculate the standard deviation from data on a txt file. Using some online help I've gotten somewhere but to be honest don't really know what's going on myself.

The program is meant to use data from an outside file so I can't hard code numbers or have the user input data.

data = open("datafile1.txt")
print data.read()

~stuff in between~

# a = numbers
# r = number of values in data
# m = mean (already calculated earlier in the programming)

def SD():
....b = []
....for n in range(r-1):
...........if r[n] > a:
.................b.append((r[n] - a)**2)
...........if r[n] < m:
.................b.append((a - r[n])**2)
...........SD = (float(b)/r)**0.5 #float because the data includes decimal values
....return SD
print "The standard deviation is", SD

Unfortunatly this is the result I get, including the number of values and mean which I had to calculate as well:

There are 4 records
The mean is 3.1422
The standard deviation is <function SD at 0x020EB630>
The standard deviation is <function SD at 0x058F6F30>
The standard deviation is <function SD at 0x020EB630>
The standard deviation is <function SD at 0x0552BA70>
The standard deviation is <function SD at 0x020EB630>

Could someone help me this?
Last edited by lcc_551; Feb 20th, 2009 at 12:08 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,045
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 294
woooee woooee is offline Offline
Veteran Poster

Re: calculating standard deviation

 
0
  #2
Feb 20th, 2009
The standard deviation is The standard deviation is <function SD at 0x020EB630>
Don't use the same name for the function and the variable returned, hence "<function SD" at 0x020EB630>
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: lcc_551 is an unknown quantity at this point 
Solved Threads: 0
lcc_551 lcc_551 is offline Offline
Newbie Poster

Re: calculating standard deviation

 
0
  #3
Feb 21st, 2009
I'm very new to python so I don't quite understand the answer given, I've tried juggling around the names but still get the same result.

I would like to post all the code I have but my prof would probably peg me for it if I did :s
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: calculating standard deviation

 
0
  #4
Feb 22nd, 2009
As well he should

Here's what wooee means:

  1. def SD():
  2. b = []
  3. for n in range(r-1):
  4. if r[n] > a:
  5. b.append((r[n] - a)**2)
  6. if r[n] < m:
  7. b.append((a - r[n])**2)
  8. SD = (float(b)/r)**0.5 #float because the data includes decimal values
  9. return SD
  10. print "The standard deviation is", SD

Note that "SD" is a function. So when you print the function, you get, quite literally, the function: <function SD" at 0x020EB630>

Try this with a simpler example:

  1. def MyFunc():
  2. pass
  3.  
  4. print MyFunc

What you actually want to print is the result of calling the function. So that needs this:

  1. def SD(...):
  2. ....
  3.  
  4. print SD()

The extra () mean "call the function and accept the result."

Mathematical aside: Your test 'if r[n] < a' is superfluous. For any values of r[n] and a, it will be true that

(r[n] - a) ** 2 == (a - r[n]) ** 2

which is why standard deviations are calculated using square residues in the first place (so that under- and over-values don't cancel each other out).

Jeff
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC