Skip to content

Instantly share code, notes, and snippets.

@jcchurch
Last active December 31, 2015 04:39
Show Gist options
  • Save jcchurch/7935241 to your computer and use it in GitHub Desktop.
Save jcchurch/7935241 to your computer and use it in GitHub Desktop.
Simple Linear Regression in Python using the vanilla language features.
def simple(x, y):
assert len(x) == len(y)
xavg = sum(x) / float(len(x))
yavg = sum(y) / float(len(y))
xdiff = [ xi - xavg for xi in x ]
ydiff = [ yi - yavg for yi in y ]
xy = [ xdiff[i] * ydiff[i] for i in range(len(x)) ]
xx = [ xdiff[i] * xdiff[i] for i in range(len(x)) ]
gradient = sum(xy) / sum(xx)
intercept = yavg - gradient * xavg
return (gradient, intercept)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment