-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
144 lines (117 loc) · 4.07 KB
/
code.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
import math
import numpy as np
import matplotlib.pyplot as plt
# Plots Graph with Separating line on 0.5 based on given Coefficients and Data X, Y
def plot_reg(X, Y, fitted_values):
i = 0
xp = []
xn = []
# Separating positive and negative data
for x in X:
if (Y[i] == 0):
xp.append([x[1], x[2]])
else:
xn.append([x[1], x[2]])
i += 1
xp = np.asarray(xp)
xn = np.asarray(xn)
# Creating Boundary
ex1 = np.linspace(min(X[:, 1]), max(X[:, 1]), 100)
ex2 = -(fitted_values[1] * ex1 + fitted_values[0]) / fitted_values[2]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(xp[:, 0], xp[:, 1], s=30, c='green', marker="s", label='Positive')
ax1.scatter(xn[:, 0], xn[:, 1], s=30, c='red', marker="o", label='Negative')
plt.plot(ex1, ex2, color='black', label='decision boundary');
plt.legend(loc='upper left')
return plt
# Implementation of Sigmoid function
def logistic_func(theta, x):
return float(1) / (1 + math.e**(-x.dot(theta)))
# returns Gradient of given theta, x, y
def gradient(theta, x, y):
first_calc = logistic_func(theta, x) - y
final_calc = first_calc.T.dot(x)
return final_calc
# Function Returns Cost for given theta,X,Y
def cost_func(theta, x, y):
log_func_v = logistic_func(theta,x)
y = np.squeeze(y)
step1 = y * np.log(log_func_v)
step2 = (1-y) * np.log(1 - log_func_v)
final = -step1 - step2
return np.sum(final)
# Implementation of Gradient Descent Method
def grad_desc(theta_values, X, y, lr=.001, converge_change=1e-8):
#theta_values=np.zeros(X[1].shape)
#setup cost iter
cost_iter = []
cost = cost_func(theta_values, X, y)
cost_iter.append([0, cost])
change_cost = 100000
i = 1
while(change_cost > converge_change):
old_cost = cost
theta_values = theta_values - (lr * gradient(theta_values, X, y))
cost = cost_func(theta_values, X, y)
cost_iter.append([i, cost])
change_cost = old_cost - cost
i+=1
return theta_values, np.array(cost_iter)
# Implementation of Finding Hessian Matrix
def hessian(theta, X):
ans = np.zeros((len(X[0]),len(X[0])))
for xi in X:
xit = xi.reshape(len(xi),1)
ans = ans + logistic_func(theta, xi)*(1-logistic_func(theta, xi))*xi*xit
return ans
# Implementation of Newton's Method
def newtons(theta_values,X,Y,converge_change=1e-8):
# setup cost iter
cost_iter = []
cost = cost_func(theta_values, X, Y)
cost_iter.append([0, cost])
change_cost = 100000.0
i = 1
while (change_cost > converge_change):
old_cost = cost
h = hessian(theta_values, X)
theta_values = theta_values - np.dot(np.linalg.inv(h),gradient(theta_values,X,Y))
cost = cost_func(theta_values, X, Y)
cost_iter.append([i, cost])
change_cost = old_cost - cost
i += 1
return theta_values, np.array(cost_iter)
# function used to Predict
def pred_values(theta, X, hard=True):
pred_prob = logistic_func(theta, X)
pred_value = np.where(pred_prob >= .5, 1, 0)
if hard:
return pred_value
return pred_prob
X = np.genfromtxt('q1x.dat')
Y = np.genfromtxt('q1y.dat')
intercept = np.ones((X.shape[0], 1))
X = np.hstack((intercept, X))
betas = np.zeros(X[1].shape)
# Applying GD to optimize theta
fitted_values, cost_iter = grad_desc(betas, X, Y)
print "For Gradient Descent Method"
print "Intercept\tCoef1\t\tCoef2"
print(fitted_values)," Iteration To Converge", len(cost_iter)
# Plot Graph for GD
plt = plot_reg(X, Y, fitted_values)
plt.title("Boundary using Coefficient of Gradient Descent Method")
plt.savefig("GD Method")
# Applying NR Method for optimization
fitted_values, cost_iter = newtons(betas, X, Y)
print "For NR Method"
print "Intercept\tCoef1\t\tCoef2"
print(fitted_values)," Iteration To converge", len(cost_iter)
# Plot Graph for Newton's
plt = plot_reg(X, Y, fitted_values)
plt.title("Boundary using Coefficient of NR Method")
plt.savefig("NR Method")
# Prediction Of each data in X
predicted_y = pred_values(fitted_values, X)
print predicted_y