Last active
December 31, 2015 04:39
-
-
Save jcchurch/7935241 to your computer and use it in GitHub Desktop.
Simple Linear Regression in Python using the vanilla language features.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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