forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsne_visualization.py
55 lines (43 loc) · 1.4 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
# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python
# https://www.udemy.com/unsupervised-deep-learning-in-python
from __future__ import print_function, division
from builtins import range, input
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.manifold import TSNE
if __name__ == '__main__':
# define the centers of each Gaussian cloud
centers = np.array([
[ 1, 1, 1],
[ 1, 1, -1],
[ 1, -1, 1],
[ 1, -1, -1],
[-1, 1, 1],
[-1, 1, -1],
[-1, -1, 1],
[-1, -1, -1],
])*3
# create the clouds, Gaussian samples centered at
# each of the centers we just made
data = []
pts_per_cloud = 100
for c in centers:
cloud = np.random.randn(pts_per_cloud, 3) + c
data.append(cloud)
data = np.concatenate(data)
# visualize the clouds in 3-D
# add colors / labels so we can track where the points go
colors = np.array([[i]*pts_per_cloud for i in range(len(centers))]).flatten()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(data[:,0], data[:,1], data[:,2], c=colors)
plt.show()
# perform dimensionality reduction
tsne = TSNE()
transformed = tsne.fit_transform(data)
# visualize the clouds in 2-D
plt.scatter(transformed[:,0], transformed[:,1], c=colors)
plt.show()