| | |
calculating standard deviation
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 2
Reputation:
Solved Threads: 0
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?
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.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
As well he should 
Here's what wooee means:
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:
What you actually want to print is the result of calling the function. So that needs this:
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

Here's what wooee means:
Python Syntax (Toggle Plain Text)
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
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:
Python Syntax (Toggle Plain Text)
def MyFunc(): pass print MyFunc
What you actually want to print is the result of calling the function. So that needs this:
Python Syntax (Toggle Plain Text)
def SD(...): .... 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
![]() |
Similar Threads
- Calculating average and standard deviation using python? (Python)
- Figure out deviation from the average of the given array" (C++)
- calculations with random #'s (C++)
- Need Help with functions program (C++)
- Array data not setting (C++)
Other Threads in the Python Forum
- Previous Thread: 'None' in Python and 'Nothing' in VB
- Next Thread: expanding music chooser
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti approximation assignment avogadro backend basic beginner binary bluetooth calculator character code customdialog decimals dictionaries dictionary drive dynamic examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime program programming progressbar projects py2exe pygame pyqt python random recursion recursive refresh schedule scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






