-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsne_visualization.py
57 lines (43 loc) · 1.76 KB
/
tsne_visualization.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
from sklearn.manifold import TSNE
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import seaborn as sns
sns.set_style('darkgrid')
sns.set_palette('muted')
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})
def scatter(feature, labels):
"""
docstring
"""
# palette = np.array(sns.color_palette("hls", 10))
palette = np.array(sns.color_palette())
f = plt.figure(figsize=(8, 8))
ax = plt.subplot(aspect='equal')
# f = plt.figure()
# ax = plt.subplot(111)
# ac = ax.scatter(feature[:, 0], feature[:, 1], lw=0, s=40, c=palette[labels.astype(np.int)])
for i in range(len(labels)):
if labels[i] == 0:
s0 = ax.scatter(feature[i, 0], feature[i, 1], lw=0, s=40, c=palette[labels[i].astype(np.int)])
if labels[i] == 1:
s1 = ax.scatter(feature[i, 0], feature[i, 1], lw=0, s=40, c=palette[labels[i].astype(np.int)])
if labels[i] == 2:
s2 = ax.scatter(feature[i, 0], feature[i, 1], lw=0, s=40, c=palette[labels[i].astype(np.int)])
if labels[i] == 3:
s3 = ax.scatter(feature[i, 0], feature[i, 1], lw=0, s=40, c=palette[labels[i].astype(np.int)])
plt.xlim(feature[:, 0].min(), feature[:, 0].max())
plt.ylim(feature[:, 1].min(), feature[:, 1].max())
plt.legend((s0, s1, s2, s3),('moving', 'waiting', 'queueing', 'talking'), loc='best' )
ax.axis('off')
ax.axis('tight')
tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
feature_embed = np.load('feature.npy')
label = np.load('label.npy')
feature_embed = feature_embed
label = label
print('feature', feature_embed.shape)
print('label', label.shape)
low_dim_embed = tsne.fit_transform(feature_embed)
scatter(low_dim_embed, label)
plt.show()