Bon mais j'ai tout de meme une beta-beta version a vous offrir (pour ces foules silencieuses qui attendaient un programme de regression lineaire..)
import sys
def main():
"""Will calculate a simple linear regression."""
try:
filename = raw_input("Which file to import? ")
f = open(filename, "r")
try:
stringdata = f.read()
f.close()
okdata = convert(stringdata)
xlist, ylist = getxnylist(okdata)
T = len(xlist)
Ex = sum(xlist)
Ey = sum(ylist)
Exsq = listsqsum(xlist)
Exy = listxysum(okdata)
b2 = (T*Exy - Ex*Ey)/(T*Exsq - Ex**2)
b1 = (Ey/T) - b2*(Ex/T)
print "yt = " + str(b1) + " + (" + str(b2) + ")Xt"
except ValueError:
print "Invalid values in there, sorry, only take numbers.."
sys.exit()
except IOError:
print "File does not exist!"
sys.exit()
def convert(ustring):
"""Formats input text file into a list of floating numbers.
Admits anything as separators between numbers but '.'. Will take all nu-
bers in consecutively, ignoring anything in between. Make sure your x-values
are first."""
accepted = ('.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
for char in ustring:
if not char in accepted:
ustring = ustring.split(char)#knock off anything not in
ustring = " ".join(ustring)#accepted, replace by whitespace
ustring = ustring.split()#then split on whitespace
ustring = [float(num) for num in ustring]#make sure numbers are decimals
return ustring
def alternativeconvert(ustring):
"""Formats through regular expression."""
pat = re.compile('[^0-9.]')#import re..
ustring = re.split(pat, ' ', ustring)
ustring = ustring.split()
ustring = [decimal.Decimal(num) for num in ustring]#import decimal..
return ustring
def getxlist(firstlist):
lx = []
if firstlist:
lx = [firstlist[0]] + getxlist(firstlist[2:])
return lx
def getylist(firstlist):
ly = []
if firstlist:
ly = [firstlist[1]] + getylist(firstlist[2:])
return ly
def getxnylist(firstlist):
"""Creates two lists. Each list has one out of two elements of the first list.
The first list will represent x-values, the second y-values."""
lx = getxlist(firstlist)
ly = getylist(firstlist)
if len(lx) != len(ly):
print "Missing coordinates.. Make sure you have pairs."
sys.exit()
else:
pass
return lx, ly
def listsqsum(l):
"""Sums the squares of the elements of a list."""
total = 0
if l:
total = (l[0])**2 + listsqsum(l[1:])
return total
def listxysum(l):
"""Sums the product of the l-list elements two by two."""
total = 0
if l:
total = l[0]*l[1] + listxysum(l[2:])
return total
if __name__ == "__main__":
main()
#TODO:
#code a general summation function --> summation(expression, indexstart, indexstop)
#as yet one can only calculate y=b1+b2x --> allow for different transformations on x
#expand simple linear regression to multiple regression
#add standard errors, R**2 and other interesting values
#def summation(expr, i=0, n):
Bon c'est un debut :-(
Sinon est-ce que quelqu'un connait un bon tuto sur les librairies standard, comme sys et os: ca m'a l'air vachement marrant, j'ai hate de tester les system calls et les flags et tout et tout (nohup here i come!).
Peace,