Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 19, 2015 00:31
Show Gist options
  • Save lettergram/b187112766ad8e5ddebe to your computer and use it in GitHub Desktop.
Save lettergram/b187112766ad8e5ddebe to your computer and use it in GitHub Desktop.
def LS(array):
sumxy = 0.0
sumx = 0.0
sumy = 0.0
sumxx = 0.0
x = 0
y = 1
for i in range(len(array)):
sumxy += (array[i][x] * array[i][y])
sumx += array[i][x]
sumy += array[i][y]
sumxx += array[i][x]**2
''' m is slope '''
slope = sumxy - ((sumx * sumy) / len(array))
slope /= sumxx - (sumx**2 / len(array))
''' b is the initial starting point '''
initial = (sumy - (slope * sumx)) / len(array)
LinearRegression = []
for i in range(len(array)):
LinearRegression.append([i, initial + (slope * i)])
return LinearRegression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment