Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Plotting PCAs of pairs of MNIST digit classes
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 =[:60000] / 255.,[: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)
for i, j in product(xrange(10), repeat=2):
if i > j:
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_)

This comment has been minimized.

Copy link

haraldschilly commented Dec 16, 2012

i think the j,i plot should not be the same as the i,j plot, but a transposed image of that. (line 24 vs 28)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.