-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestThree.py
More file actions
61 lines (45 loc) · 1.13 KB
/
Copy pathTestThree.py
File metadata and controls
61 lines (45 loc) · 1.13 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
import numpy as np
import matplotlib.pyplot as plt
X = np.array([[1, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 1],
[1, 1, 0, 1, 0, 0],
[1, 1, 1, 1, 1, 1]])
Y = np.array([-1, 1, 1, -1])
W = (np.random.random(6) - 0.5) * 2
lr = 0.11
n = 0
O = 0
def update():
global X, Y, W, lr, n
n += 1
O = np.dot(X, W.T)
# print("O: ", O)
# print("Y-O.T : ", Y - O.T)
W_C = lr * ((Y - O.T).dot(X)) / X.shape[0]
# print("W_C * Y - O.T: ", (Y - O.T).dot(X))
# print("W_C * Y - O.T: ", X.dot(Y - O.T))
W = W + W_C
for _ in range(1000):
update()
# print("W: ", W)
# print("n: ", n)
x1 = [0, 1]
y1 = [1, 0]
x2 = [0, 1]
y2 = [0, 1]
def calculate(x, root):
a = W[5]
b = W[2] + x * W[4]
c = W[0] + x * W[1] + x * x * W[3]
if root == 1:
return (-b + np.sqrt(b * b - 4 * a * c)) / (2 * a)
elif root == 2:
return (-b - np.sqrt(b * b - 4 * a * c)) / (2 * a)
xdata = np.linspace(-1, 2)
print(xdata)
plt.figure()
plt.plot(xdata, calculate(xdata, 1), 'r')
plt.plot(xdata, calculate(xdata, 2), 'r')
plt.plot(x1, y1, 'bo')
plt.plot(x2, y2, 'yo')
plt.show()