-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_classification.py
More file actions
52 lines (38 loc) · 1.3 KB
/
binary_classification.py
File metadata and controls
52 lines (38 loc) · 1.3 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
import numpy as np
import matplotlib.pyplot as plt
def __main__():
x_train = np.array([[10, 50], [20, 30], [25, 30], [20, 60], [15, 70], [40, 40], [30, 45], [20, 45], [40, 30], [7, 35]])
y_train = np.array([-1, 1, 1, -1, -1, 1 ,1, -1, 1, -1])
n_train = len(x_train)
omega = [0, -1]
a = lambda x : np.sign(x[0] * omega[0] + x[1] * omega[1])
N = 50
L = 0.1
e = 0.1
last_error_index = -1
for n in range(N):
for i in range(n_train):
if y_train[i] * a(x_train[i]) < 0:
omega[0] = omega[0] + L * y_train[i]
last_error_index = i
Q = sum([1 for i in range(n_train) if y_train[i] * a(x_train[i]) < 0])
if Q == 0:
break
if last_error_index > -1:
omega[0] = omega[0] + e * y_train[last_error_index]
print(omega)
line_x = list(range(max(x_train[:, 0])))
line_y = [omega[0] * x for x in line_x]
x_0 = x_train[y_train == 1]
x_1 = x_train[y_train == -1]
plt.scatter(x_0[:, 0], x_0[:, 1], color = 'red')
plt.scatter(x_1[:, 0], x_1[:, 1], color = 'blue')
plt.plot(line_x, line_y, color = 'green')
plt.xlim([0, 45])
plt.ylim([0, 75])
plt.ylabel('length')
plt.xlabel('width')
plt.grid(True)
plt.show()
if __name__ == '__main__':
__main__()