Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@spillai
Forked from amueller/gist:4299381
Created April 5, 2013 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spillai/5321478 to your computer and use it in GitHub Desktop.
Save spillai/5321478 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from itertools import product
from sklearn.decomposition import RandomizedPCA
from sklearn.datasets import fetch_mldata
from sklearn.utils import shuffle
mnist = fetch_mldata("MNIST original")
X_train, y_train = mnist.data[:60000] / 255., mnist.target[:60000]
X_train, y_train = shuffle(X_train, y_train)
X_train, y_train = X_train[:5000], y_train[:5000] # lets subsample a bit for a first impression
pca = RandomizedPCA(n_components=2)
fig, plots = plt.subplots(10, 10)
fig.set_size_inches(50, 50)
plt.prism()
for i, j in product(xrange(10), repeat=2):
if i > j:
continue
X_ = X_train[(y_train == i) + (y_train == j)]
y_ = y_train[(y_train == i) + (y_train == j)]
X_transformed = pca.fit_transform(X_)
plots[i, j].scatter(X_transformed[:, 0], X_transformed[:, 1], c=y_)
plots[i, j].set_xticks(())
plots[i, j].set_yticks(())
plots[j, i].scatter(X_transformed[:, 0], X_transformed[:, 1], c=y_)
plots[j, i].set_xticks(())
plots[j, i].set_yticks(())
if i == 0:
plots[i, j].set_title(j)
plots[j, i].set_ylabel(j)
#plt.scatter(X_transformed[:, 0], X_transformed[:, 1], c=y_)
plt.tight_layout()
plt.savefig("mnist_pairs.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment