Last active
March 14, 2017 05:30
-
-
Save tjwei/efda2b213052fb43359f5c9c707bfcd8 to your computer and use it in GitHub Desktop.
This file contains 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
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