-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLR.py
More file actions
64 lines (52 loc) · 1.63 KB
/
LR.py
File metadata and controls
64 lines (52 loc) · 1.63 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
''' The plan is to rewrite and this time, calculate cost each time to ensure its reducing. Also make it enough to handle multiple variables '''
from __future__ import division
import os
import sys
def computecost(X,Y,theta):
#X is the feature vector, Y is the predicted variable
h_theta = calculatehTheta(X,theta)
delta = (h_theta - Y) * (h_theta - Y)
return (1/194) * delta
def allCost(f, no_features):
theta = [0,0]
sigma = 0
data = open(f,'r')
for line in data:
X=[]
Y=0
points=line.split(",")
for i in range(no_features):
X.append(float(points[i]))
Y = float(points[no_features].strip("\n"))
sigma = sigma + computecost(X, Y, theta)
return sigma
def calculatehTheta(points, theta):
#This takes a file which has (1,feature1,feature2,so ... on)
#print 'Points are',points
sigma = 0
for i in range(len(theta)):
sigma = sigma + theta[i] * float(points[i])
return sigma
def gradient_Descent(f, no_iters, no_features, theta):
''' Calculate ( h(x) - y ) * xj(i). And then subtract it from thetaj. Continue for 1500 iterations and you will have your answer'''
X = []
Y = 0
sigma = 0
alpha = 0.01
for i in range(no_iters):
for j in range(len(theta)):
data = open(f, 'r')
for line in data:
points = line.split(",")
for i in range(no_features):
X.append(float(points[i]))
Y = float(points[no_features].strip("\n"))
h_theta = calculatehTheta(points,theta)
delta = h_theta - Y
sigma = sigma + delta * float(points[j])
data.close()
theta[j] = theta[j] - (alpha/97) * sigma
sigma = 0
print theta
print allCost(sys.argv[1],2)
print gradient_Descent(sys.argv[1],1500,2,[0,0])