-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforce.py
90 lines (75 loc) · 2.34 KB
/
force.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Force-Scheme
"""
import numpy as np
tol = 1.e-6 # zero tolerance
def pdist(x):
"""
Pairwise distance between pairs of objects
TODO: find a fast function
"""
n, d = x.shape
dist = np.zeros((n, n))
for i in range(n):
for j in range(n):
dist[i][j] = np.linalg.norm(x[i] - x[j])
return dist
def project(x, dtype = "data", niter = 50, delta_frac = 8):
"""
Projection
"""
assert type(x) is np.ndarray, \
"*** ERROR (Force-Scheme): project input must be numpy.array type."
ninst, dim = x.shape # number of instances, dimension of the data
Y = np.random.random((ninst, 2)) # random initialization
# computes distance in R^n
if dtype == "data":
distRn = pdist(x)
elif dtype == "dmat":
distRn = x
else:
print "*** ERROR (Force-Scheme): Undefined data type."
assert type(distRn) is np.ndarray and distRn.shape == (ninst, ninst), \
"*** ERROR (Force-Scheme): project input must be numpy.array type."
idx = np.random.permutation(ninst)
for k in range(niter):
# for each x'
for i in range(ninst):
inst1 = idx[i]
# for each q' != x'
for j in range(ninst):
inst2 = idx[j]
if inst1 != inst2:
# computes direction v
v = Y[inst2] - Y[inst1]
distR2 = np.hypot(v[0], v[1])
if distR2 < tol:
distR2 = tol
delta = (distRn[inst1][inst2] - distR2) / delta_frac
v /= distR2
# move q' = Y[j] in the direction of v by a fraction
# of delta
Y[inst2] += delta * v
return Y
def plot(y, t):
import matplotlib.pyplot as mpl
mpl.scatter(y.T[0], y.T[1], c = t)
mpl.show()
def test():
import time, sys
print "Loading data set... ",
sys.stdout.flush()
data = np.loadtxt("mammals.data", delimiter=",")
print "Done."
n, d = data.shape
x = data[:, range(d-1)]
t = data[:, d-1]
start_time = time.time()
print "Projecting... ",
sys.stdout.flush()
y = project(x)
print "Done. Elapsed time:", time.time() - start_time, "s."
plot(y, t)
if __name__ == "__main__":
print "Running test..."
test()