Skip to content

Instantly share code, notes, and snippets.

@tjwei
Last active March 14, 2017 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjwei/efda2b213052fb43359f5c9c707bfcd8 to your computer and use it in GitHub Desktop.
Save tjwei/efda2b213052fb43359f5c9c707bfcd8 to your computer and use it in GitHub Desktop.
import csv
dot = lambda a,b: sum(i*j for i,j in zip(a,b))
mean = lambda a: sum(a)/len(a)
rows = list(csv.DictReader(open("insurance.csv")))
data= {k: [float(i[k]) for i in rows] for k in rows[0]}
x, y = data['X'], data['Y']
n = len(x)
beta1 = (n*dot(x,y)-sum(x)*sum(y))/(n*dot(x,x)-sum(x)**2)
beta0 = mean(y)-beta1*mean(x)
print("beta0 = ",beta0,",beta1 = ",beta1)
# result:
# beta0 = 19.994485759114784 ,beta1 = 3.413823560066368
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment