-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gradient_descent_with_bias.py
170 lines (111 loc) · 3.32 KB
/
Gradient_descent_with_bias.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 12 17:17:17 2018
@author: sanath
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dfs=pd.read_excel("Activity_2_Data.xlsx",sheetname="Sheet1")
x=np.array(dfs.iloc[0:,0])
y=np.array(dfs.iloc[0:,4])
plt.figure(1)
plt.xlabel("X")
plt.ylabel("Y")
plt.plot(x,y)
plt.show()
m=len(x)
X=np.array(x)
Y=np.array(y)
theta0=100
init_theta0=theta0
theta1=25
init_theta1=theta1
m=len(X)
alpha=0.0001
print("Learning rate is ",alpha)
print("Number of samples or data is",m)
count=0
while(True):
cost_J0=0
cost_J1=0
for i in range(m):
h_x=theta0+theta1*X[i]
cost_J0=cost_J0+(h_x-Y[i])
cost_J1=cost_J1+((h_x-Y[i])*X[i])
count+=1
#uncomment to see variation of theta
#print("iteration",count,"theta0",theta0,"theta1",theta1)
temp0=theta0-((alpha*cost_J0)/m)
temp1=theta1-((alpha/m)*cost_J1)
if((np.abs(temp0-theta0)<0.00001)and(np.abs(temp1-theta1)<0.00001)):
break
theta0=temp0
theta1=temp1
h=[]
for i in range(m):
h.append(theta1*X[i]+theta0)
h=np.array(h)
plt.figure(2)
plt.xlabel("X")
plt.ylabel("Hypothesis")
plt.plot(X,Y)
plt.plot(X,h)
plt.show()
print ("Assumed theta0 is ",init_theta0,"predicted theta0 is ",theta0)
print("Assumed theta1 is ",init_theta1,"predicted theta1 is ",theta1)
test_x=float(input("Enter a test sample:"))
predicted_y=(test_x*theta1)+theta0
#implementation of gradient descent for 1 st order curve(straight line)
print ("The value of predicted y is",predicted_y)
print("Importance feature scaling ,here you can see that the number of iteration it took to calculate the theta0 and theta1 are ",count," iterations ,so here if we scale down the x and y values to certain range i.e by normalisation,we can reduce the number of iterations it took to calculate the theta0 and theta1 ,or the other way is to judge the random theta0 and theta1 values by seeing the plot which are very close to each other ,so by this also we can reduce the number of iterations and computing time of the program")
"""
#dummy code (test code)
theta0=4
theta1=3
m=len(X)
alpha=0.001
while(True):
cost_J0=0
cost_J1=0
for i in range(m):
h_x=theta0+theta1*X[i]
cost_J0=cost_J0+(h_x-Y[i])
cost_J1=cost_J1+((h_x-Y[i])*X[i])
temp0=theta0-((alpha*cost_J0)/m)
temp1=theta1-((alpha/m)*cost_J1)
if((np.abs(temp0-theta0)<0.01)and(np.abs(temp1-theta1)<0.01)):
break
theta0=temp0
theta1=temp1
h=[]
for i in range(m):
h.append(theta1*X[i]+theta0)
h=np.array(h)
plt.xlabel("X")
plt.ylabel("Hypothesis")
plt.plot(X,h)
print ("theta0={0} theta1={1}").format(theta0,theta1)
test_x=input("Enter a test sample:" )
predicted_y=(test_x*theta1)+theta0
print ("The value of predicted y is"),predicted_y
"""
"""
#
for j in range(m):
res=res+((theta0+theta1*x[j])-y[j])
new_theta0=theta0-((alpha/m)*(res))
print("Assumed Theta0 is ",theta0," New theta1 is",new_theta0 )
for j in range(m):
res_theta1=res_theta1+((theta0+theta1*x[j])-y[j])*x[j]
new_theta1=theta1-((alpha/m)*res_theta1)
print("Assumed Theta1 is ",theta1,"new theta1 is",new_theta1)
hyp=[]
for ele in x:
hyp.append((new_theta0+(new_theta1*ele)))
hyp=np.array(hyp)
plt.figure(2)
plt.plot(x,y)
plt.plot(x,hyp)
plt.show()
"""