-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFLD.py
More file actions
35 lines (26 loc) · 1.01 KB
/
FLD.py
File metadata and controls
35 lines (26 loc) · 1.01 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
import numpy as np
import matplotlib.pyplot as plt
def __main__():
x_train = [[10, 50], [20, 30], [25, 30], [20, 60], [15, 70], [40, 40], [30, 45], [20, 45], [40, 30], [7, 35]]
x_train = [x + [1] for x in x_train]
x_train = np.array(x_train)
y_train = np.array([-1, 1, 1, -1, -1, 1 ,1, -1, 1, -1])
pt = np.sum([x * y for x, y in zip(x_train, y_train)], axis = 0) # sum xi(transp) * yi
xxt = np.sum([np.outer(x, x) for x in x_train], axis = 0) # xi * xi(transp)
w = np.dot(pt, np.linalg.inv(xxt))
print(w)
line_x = list(range(max(x_train[:, 0])))
line_y = [-x * w[0] / w[1] - w[2] 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__()