-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolice.py
More file actions
62 lines (44 loc) · 1.55 KB
/
police.py
File metadata and controls
62 lines (44 loc) · 1.55 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
# A binary classifier.
import numpy as np
# Applying Logistic Regression
def sigmoid(z):
return 1 / (1 + np.exp(-z))
# Basically doing prediction but named forward as its
# performing Forward-Propagation
def forward(x, w):
weighted_sum = np.matmul(x, w)
return sigmoid(weighted_sum)
# Calling the predict() function
def classify(x, w):
return np.round(forward(x, w))
# Computing Loss over using logistic regression
def loss(x, y, w):
y_hat = forward(x, w)
first_term = y * np.log(y_hat)
second_term = (1 - y) * np.log(1 - y_hat)
return -np.average(first_term + second_term)
# calculating gradient
def gradient(x, y, w):
return np.matmul(x.T, (forward(x, w) - y)) / x.shape[0]
# calling the training function for 10,000 iterations
def train(x, y, iterations, lr):
w = np.zeros((x.shape[1], 1))
for i in range(iterations):
if (i % 2000 == 0 or i == 9999):
print("Iteration %4d => Loss: %.20f" % (i, loss(x, y, w)))
w -= gradient(x, y, w) * lr
return w
# Doing inference to test our model
def test(x, y, w):
total_examples = x.shape[0]
correct_results = np.sum(classify(x, w) == y)
success_percent = correct_results * 100 / total_examples
print("\nSuccess: %d/%d (%.2f%%)" %
(correct_results, total_examples, success_percent))
# Prepare data
x1, x2, x3, y_truth = np.loadtxt("police.txt", skiprows=1, unpack=True)
X = np.column_stack((np.ones(x1.size), x1, x2, x3))
Y = y_truth.reshape(-1, 1)
weight = train(X, Y, iterations=10000, lr=0.001)
# Test it
test(X, Y, weight)