-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegression_gh.py
More file actions
49 lines (38 loc) · 1.13 KB
/
Copy pathRegression_gh.py
File metadata and controls
49 lines (38 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 18 01:36:33 2019
@author: sarat
"""
import random
import pandas as pd
import numpy as np
from math import sqrt
from statistics import *
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import mse
data = pd.read_csv('C:/Users/sarat/Desktop/Spring/Causal Inference/BMI.csv')
data1 = data[['weight','height']]
## Using basic statistics
x_bar = data1.height.mean()
y_bar = data1.weight.mean()
num = sum((data1.height - x_bar)*(data1.weight-y_bar))
den = sum((data1.height - x_bar)*(data1.height - x_bar))
b1 = num/den
b0 = y_bar - b1*x_bar
y_pred = b1*data1.height +b0
rmse = sqrt(mean((y_pred - data1.weight)*(y_pred - data1.weight)))
## RMSE value is 40.8
###now lets use sklearn
model =LinearRegression()
x=np.array(data1.height)
y = np.array(data1.weight)
# Reshaping data to put in linear regression
x=x.reshape(-1,1)
y=y.reshape(-1,1)
model.fit(x,y)
y_pred = model.predict(x)
rmse = sqrt(mse(y_pred,y))
rmse
# 40.8
# the values from both methods match