-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_DSPFP.py
58 lines (46 loc) · 1.62 KB
/
test_DSPFP.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
from create_dataset import create_dataset_artificial
import matplotlib.pyplot as plt
from DSPFP import DSPFP_faster, loss
if __name__ == '__main__':
np.random.seed(0)
plt.interactive(True)
size1 = 35
size2 = 30
same = True
print("Simple 2D example of DSPFP.")
print("Same: %s" % same)
A, B, X1, X2 = create_dataset_artificial(size1, size2, same)
C = X1
D = X2
lam = 1.0
print("DSPFP:")
X = DSPFP_faster(A, B, C=C, D=D, lam=lam, alpha=0.5,
threshold1=1.0e-4, threshold2=1.0e-4)
P = (X == X.max(1)[:, None])
loss_X = loss(A, B, X)
loss_P = loss(A, B, P)
print("Loss(X) = %s" % loss_X)
print("Loss(P) = %s" % loss_P)
print("")
print("Plotting.")
plt.figure()
# Adding some constant displacement, for visualization purpose:
X2 = X2 + np.array([1.0, 0.5])
plt.plot(X1[:, 0], X1[:, 1], 'ro', markersize=10)
plt.plot(X2[:, 0], X2[:, 1], '*b', markersize=10)
mapping12 = P.argmax(1)
if size2 >= size1:
for i in range(size1):
plt.plot([X1[i, 0], X2[mapping12[i], 0]],
[X1[i, 1], X2[mapping12[i], 1]], 'r-')
temp = X2[mapping12[i]] - X1[i]
plt.arrow(X1[i, 0], X1[i, 1], temp[0], temp[1], head_width=0.05,
head_length=0.05, length_includes_head=True)
else:
mapping21 = P.argmax(0)
for i in range(size2):
temp = X1[mapping21[i]] - X2[i]
plt.arrow(X2[i, 0], X2[i, 1], temp[0], temp[1], head_width=0.05,
head_length=0.05, length_includes_head=True)
plt.show()