-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_reg.py
65 lines (49 loc) · 1.7 KB
/
log_reg.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
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
def read_and_process(filepath, delim=","):
data = pd.read_csv(filepath, delimiter=delim)
# min-max normalization
data = (data - data.min())/(data.max()-data.min())
# if label == 0, label = -1
data.loc[data["label"] == 0, "label"] = -1
# add column of 1s
data["bias"] = 1
# Multiply features by -1 if label == -1
z = data[["bias", "exam1", "exam2"]].multiply(data["label"], axis="index")
z = z.values
y = data["label"].values.reshape((z.shape[0], 1))
return data, z, y
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def g3(x):
return 0.5 - 1.20096*(x/8) + 0.81562*((x/8)**2)
def g7(x):
return 0.5 - 1.73496*(x/8) + 4.19407*(x/8)**3 - 5.43402*(x/8)**5 + 2.50739*(x/8)**7
def cost(x):
return np.log(1 + np.exp(-x)).mean()
def gradient_descent(Z, alpha=0.01, gamma=0.5, iters=100):
m = Z.shape[0]
# Initially v and beta are equal. v_0 == beta_0
v = np.ones((1, Z.shape[1]))
betas = np.ones((1, Z.shape[1]))
costs = []
for _ in range(iters):
# Uncomment sigmoid and comment next line for sigmoid
# g = sigmoid(-np.matmul(Z, v.T))
g = g7(np.matmul(Z, v.T))
betas_new = v + alpha * np.matmul(g.T, Z)
v = (1-gamma)*betas_new + gamma*betas
betas = betas_new
costs.append(cost(np.matmul(Z, v.T)))
return betas, costs
def plot_costs(costs):
plt.plot([i for i in range(len(costs))], costs)
plt.show()
def main():
data, z, y = read_and_process("exams.csv")
params, costs = gradient_descent(z, alpha=0.2, iters=50)
print(params)
plot_costs(costs)
if __name__ == "__main__":
main()