Last active
December 1, 2019 06:28
-
-
Save tfolkman/8f9b92425d8ba2480ff1ae9616424a56 to your computer and use it in GitHub Desktop.
tsne gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.datasets import fetch_mldata | |
from sklearn.manifold import TSNE | |
from sklearn.decomposition import PCA | |
import seaborn as sns | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# get mnist data | |
mnist = fetch_mldata("MNIST original") | |
X = mnist.data / 255.0 | |
y = mnist.target | |
# first reduce dimensionality before feeding to t-sne | |
pca = PCA(n_components=50) | |
X_pca = pca.fit_transform(X) | |
# randomly sample data to run quickly | |
rows = np.arange(70000) | |
np.random.shuffle(rows) | |
n_select = 10000 | |
# reduce dimensionality with t-sne | |
tsne = TSNE(n_components=2, verbose=1, perplexity=50, n_iter=1000, learning_rate=200) | |
tsne_results = tsne.fit_transform(X_pca[rows[:n_select],:]) | |
# visualize | |
df_tsne = pd.DataFrame(tsne_results, columns=['comp1', 'comp2']) | |
df_tsne['label'] = y[rows[:n_select]] | |
sns.lmplot(x='comp1', y='comp2', data=df_tsne, hue='label', fit_reg=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment