|
| 1 | +# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python |
| 2 | +# https://www.udemy.com/unsupervised-deep-learning-in-python |
| 3 | +from __future__ import print_function, division |
| 4 | +from builtins import range, input |
| 5 | +# Note: you may need to update your version of future |
| 6 | +# sudo pip install -U future |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import matplotlib.pyplot as plt |
| 10 | + |
| 11 | +from mpl_toolkits.mplot3d import Axes3D |
| 12 | +from sklearn.manifold import TSNE |
| 13 | + |
| 14 | + |
| 15 | +if __name__ == '__main__': |
| 16 | + # define the centers of each Gaussian cloud |
| 17 | + centers = np.array([ |
| 18 | + [ 1, 1, 1], |
| 19 | + [ 1, 1, -1], |
| 20 | + [ 1, -1, 1], |
| 21 | + [ 1, -1, -1], |
| 22 | + [-1, 1, 1], |
| 23 | + [-1, 1, -1], |
| 24 | + [-1, -1, 1], |
| 25 | + [-1, -1, -1], |
| 26 | + ])*3 |
| 27 | + |
| 28 | + # create the clouds, Gaussian samples centered at |
| 29 | + # each of the centers we just made |
| 30 | + data = [] |
| 31 | + pts_per_cloud = 100 |
| 32 | + for c in centers: |
| 33 | + cloud = np.random.randn(pts_per_cloud, 3) + c |
| 34 | + data.append(cloud) |
| 35 | + data = np.concatenate(data) |
| 36 | + |
| 37 | + # visualize the clouds in 3-D |
| 38 | + # add colors / labels so we can track where the points go |
| 39 | + colors = np.array([[i]*pts_per_cloud for i in range(len(centers))]).flatten() |
| 40 | + fig = plt.figure() |
| 41 | + ax = fig.add_subplot(111, projection='3d') |
| 42 | + ax.scatter(data[:,0], data[:,1], data[:,2], c=colors) |
| 43 | + plt.show() |
| 44 | + |
| 45 | + |
| 46 | + # perform dimensionality reduction |
| 47 | + tsne = TSNE() |
| 48 | + transformed = tsne.fit_transform(data) |
| 49 | + |
| 50 | + # visualize the clouds in 2-D |
| 51 | + plt.scatter(transformed[:,0], transformed[:,1], c=colors) |
| 52 | + plt.show() |
| 53 | + |
| 54 | + |
| 55 | + |
0 commit comments