-
Notifications
You must be signed in to change notification settings - Fork 0
/
embeddings_vis.py
48 lines (40 loc) · 1.04 KB
/
embeddings_vis.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
import glob
import numpy as np
import plotly
from sklearn.decomposition import PCA
data_map = {}
embeddings = []
labels = []
ix = 0
n = 10
for face in glob.glob("data/embeddings/*.npy")[:n]:
samples = np.load(face)
embeddings.append(samples)
labels.append(np.full(len(samples), ix))
ix += 1
embeddings = np.concatenate(embeddings)
labels = np.concatenate(labels)
pca = PCA(n_components=3)
reduced = pca.fit_transform(embeddings)
print(np.cumsum(pca.explained_variance_ratio_))
plot = plotly.graph_objs.Scatter(
x=reduced[:, 0],
y=reduced[:, 1],
mode="markers",
marker=dict(
color=labels, # set color equal to a variable
colorscale='Rainbow',
)
)
threeplot = plotly.graph_objs.Scatter3d(
x=reduced[:, 0],
y=reduced[:, 1],
z=reduced[:, 2],
mode="markers",
marker=dict(
color=labels, # set color equal to a variable
colorscale='Rainbow',
)
)
plotly.offline.plot([plot], filename="embeddings.html")
plotly.offline.plot([threeplot], filename="embeddings3d.html")