-
Notifications
You must be signed in to change notification settings - Fork 0
/
t-SNE_PCA.py
50 lines (43 loc) · 1.38 KB
/
t-SNE_PCA.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
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from datetime import datetime
# # DATASET
mnist = input_data.read_data_sets(
'MNIST_data', one_hot=True) # read mnist data
testData = mnist.test.images
testLabelIndex = np.argmax(mnist.test.labels, axis=1)
# # PCA
start = datetime.now()
pca = PCA(n_components=2)
pca.fit(testData)
pca_result = pca.transform(testData)
print('PCA', datetime.now() - start)
# # t-SNE
start = datetime.now()
tsne = TSNE(n_components=2)
tsne_result = tsne.fit_transform(testData)
print('t-SNE', datetime.now() - start)
# # PCA + t-SNE
start = datetime.now()
pca = PCA(n_components=48)
pca.fit(testData)
pca_result = pca.transform(testData)
tsne = TSNE(n_components=2)
merge_result = tsne.fit_transform(pca_result)
print('PCA + t-SNE', datetime.now() - start)
# # PLOT result
fig = plt.figure()
subPlot = fig.add_subplot(1, 3, 1)
subPlot.scatter(pca_result[:, 0], pca_result[:, 1], c=testLabelIndex)
subPlot.set_title('PCA')
subPlot = fig.add_subplot(1, 3, 2)
subPlot.scatter(tsne_result[:, 0], tsne_result[:, 1], c=testLabelIndex)
subPlot.set_title('t-SNE')
subPlot = fig.add_subplot(1, 3, 3)
subPlot.scatter(merge_result[:, 0], merge_result[:, 1], c=testLabelIndex)
subPlot.set_title('PCA + t-SNE')
plt.show()