-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
72 lines (44 loc) · 1.75 KB
/
utils.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
from sklearn.manifold import MDS
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
def get_stress_for_different_dimensions(dm, max_dim=5):
# Initialize stress vector
stresses = np.empty((max_dim, ))
for dim in range(max_dim):
# For each dim, perform MDS analysis
mds_ = MDS(n_components=dim+1, metric=True,
dissimilarity="precomputed")
e = mds_.fit_transform(dm)
# Fetch stresses
stresses[dim] = mds_.stress_
return stresses
def get_embedding(dm, dim):
# Return embedding space
return MDS(n_components=dim, metric=True, dissimilarity="precomputed").fit_transform(dm)
def visualize(data, dim=2):
# Visualize the new embeddig
fig = plt.figure()
if dim == 1:
# 1D plot is not useful
for k in range(data.shape[0]):
plt.scatter(data[k:k+1, 0], None)
elif dim == 2:
ax = plt.axes()
for k in range(data.shape[0]):
ax.scatter(data[k:k+1, 0], data[k:k+1, 1])
# Line segments are only applicable for 2D
segments = [[data[i, :], data[j, :]]
for i in range(data.shape[0]) for j in range(data.shape[0])]
lc = LineCollection(segments,
zorder=0, cmap=plt.cm.Blues)
lc.set_linewidths(np.full(len(segments), 0.5))
ax.add_collection(lc)
elif dim == 3:
ax = fig.add_subplot(111, projection='3d')
for k in range(data.shape[0]):
ax.scatter(data[k:k+1, 0], data[k:k + 1, 1], data[k:k+1, 2])
plt.title("Embeddings in {0} dimension".format(str(dim)))
plt.tight_layout()
plt.savefig("embedding_example_{0}.png".format(str(dim)))
plt.close("all")