Difference between revisions of "Least squares"

From Tech
Jump to navigationJump to search
(Created page with " def leastsquares(data): n=len(data)+0.0 avx=sum([d[0] for d in data])/n avy=sum([d[1] for d in data])/n do=[(d[0]-avx, d[1]-avy) for d in data] slope=sum([…")
 
 
Line 1: Line 1:
  +
Leastsquares implementation in python:
 
 
def leastsquares(data):
 
def leastsquares(data):
 
n=len(data)+0.0
 
n=len(data)+0.0

Latest revision as of 15:23, 7 March 2013

Leastsquares implementation in python:

def leastsquares(data):
   n=len(data)+0.0
   avx=sum([d[0] for d in data])/n
   avy=sum([d[1] for d in data])/n
   do=[(d[0]-avx, d[1]-avy) for d in data]
   slope=sum([d[0]*d[1] for d in do]) / sum([d[0]*d[0] for d in do])
   offset=avy-avx*slope
   return offset, slope
   #print "avx=", avx
   #print "avy=", avy
   #print "slope=", slope
   #print "offset=", offset