Skip to content

Instantly share code, notes, and snippets.

@jesuscast
Last active August 29, 2015 14:05
Show Gist options
  • Save jesuscast/dbab7faa156638de4c77 to your computer and use it in GitHub Desktop.
Save jesuscast/dbab7faa156638de4c77 to your computer and use it in GitHub Desktop.
Given a set of x values and y values, this functions returns another function with the best fit line
def LFIT(x,y):
N = len(x)
if N != len(y):
return 0
Sx = sum(x)
Sy = sum(y)
Sx2 = sum([n**2 for n in x])
Sxy = sum([x[i]*y[i] for i in range(N)])
b = (N*Sxy-Sx*Sy)/(N*Sx2-Sx**2)
a = (Sy-b*Sx)/N
def bestFit(k):
if k=='a':
return a
elif k=='b':
return b
else:
return b*k+a
return bestFit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment