-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter12.3.py
More file actions
54 lines (47 loc) · 1.32 KB
/
Copy pathChapter12.3.py
File metadata and controls
54 lines (47 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
import numpy as np
import matplotlib.pyplot as plt
def kliep(k, r):
a0 = np.random.rand(k.shape[1], 1)
b = np.mean(r, 0).reshape(-1, 1)
c = sum(b ** 2)
for o in range(1000):
a = a0 + 0.01 * k.T.dot((1 / k).dot(a0))
a = a + b * (1 - sum(b * a)) / c
a = np.maximum(0, a)
a = a / sum(b * a)
if np.linalg.norm(a - a0) < 0.001:
break
a0 = a
return a0
if __name__ == '__main__':
n = 100
x = np.random.randn(n, 1)
y = np.random.randn(n, 1)
y[n - 1] = 5
hhs = 2 * np.array([1, 5, 10]) ** 2
m = 5
x2 = x ** 2
y2 = y ** 2
xx = np.tile(x2, (1, n)) + np.tile(x2.T, (n, 1)) - 2 * x.dot(x.T)
yx = np.tile(y2, (1, n)) + np.tile(x2.T, (n, 1)) - 2 * y.dot(x.T)
u = []
for i in range(n):
temp = int(m * (i - 1) / n)
u.append(temp)
u = np.asarray(u)
u = np.random.permutation(u)
g = np.zeros((3, 5))
for hk in range(len(hhs)):
hh = hhs[hk]
k = np.exp(-xx / hh)
r = np.exp(-yx / hh)
for i in range(m):
g[hk, i] = np.mean(k[u == i, :].dot(kliep(k[u != i, :], r)))
temp = g.mean(axis=1)
gh, ggh = temp.max(), temp.argmax()
HH = hhs[ggh]
k = np.exp(-xx / HH)
r = np.exp(-yx / HH)
s = r.dot(kliep(k, r))
plt.plot(y, s, 'rx')
plt.show()