-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradient.py
40 lines (33 loc) · 994 Bytes
/
gradient.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
import numpy as np
import matplotlib.pyplot as plt
#http://blog.csdn.net/programmer_wei/article/details/51941358
#y=2 * (x1) + (x2) + 3
rate = 0.001
x_train = np.array([ [1, 2], [2, 1], [2, 3], [3, 5], [1, 3], [4, 2], [7, 3], [4, 5], [11, 3], [8, 7] ])
y_train = np.array([7, 8, 10, 14, 8, 13, 20, 16, 28, 26])
x_test = np.array([ [1, 4], [2, 2], [2, 5], [5, 3], [1, 5], [4, 1] ])
a = np.random.normal()
b = np.random.normal()
c = np.random.normal()
def h(x):
return a*x[0]+b*x[1]+c
for i in range(10000):
sum_a=0
sum_b=0
sum_c=0
for x, y in zip(x_train, y_train):
sum_a = sum_a + rate*(y-h(x))*x[0]
sum_b = sum_b + rate*(y-h(x))*x[1]
sum_c = sum_c + rate*(y-h(x))
a = a + sum_a
b = b + sum_b
c = c + sum_c
plt.plot([h(xi) for xi in x_test])
print(a)
print(b)
print(c)
result=[h(xi) for xi in x_train]
print(result)
result=[h(xi) for xi in x_test]
print(result)
plt.show()