-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinear2.py
More file actions
71 lines (56 loc) · 1.32 KB
/
Copy pathlinear2.py
File metadata and controls
71 lines (56 loc) · 1.32 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
63
64
65
66
67
68
69
70
71
import numpy as np
import dataset
import matplotlib.pyplot as plt
from scipy.ndimage.filters import gaussian_filter1d
n= 14
#Xi = np.arange(n+2).reshape(1, n+2)
#Q = np.arange(n+2).reshape(1, n+2)
#Xi = np.ones(n+2, dtype=float)
#Xi[n+1] = 1
#Y = np.arange(500).reshape(1, 500)
Q = np.ones(n+2, dtype=float)
A = np.ones(n+2, dtype=float)
a = []
b = []
def h(Q,Xi): # COMPLETE
Xi_T = np.transpose(Xi)
F = np.dot(Q, Xi_T)
return F
def J(K,Q,X,Y):
V = 0
for i in range(K):
V = (h(Q,X[i])-Y[i])**2 + V
F = (1/(2*K))*V
print(F)
return F
def derivative_J(K,Q,X,Y,j):
V = 0
Z = 0
for i in range(K):
V = (h(Q,X[i]) - Y[i])*X[i][j] + V
Z = (1/K) * V + Z
return Z
def linearization(n,Xi,Y,K,train_rate=500,alfa=0.01):
for j in range(train_rate):
Q = np.copy(A)
a.append(J(K, Q, Xi, Y))
b.append(j)
for i in range(n+1):
A[i] = A[i] - alfa*derivative_J(K,Q,Xi,Y,i)
def plotting(a,b):
ysmoothed = gaussian_filter1d(a[1:], sigma=2)
plt.plot(b[1:], ysmoothed, '-', color='red')
axes = plt.gca()
axes.set_ylim([0, 20])
plt.autoscale(enable=True, axis='x')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
n = 15
k = 500
x,y = dataset.dataset_n()
#print(x)
linearization(n,x,y,k)
print(a)
print(len(b))
plotting(a,b)