Skip to content

Instantly share code, notes, and snippets.

@lettergram
Last active August 29, 2015 14:17
Show Gist options
  • Save lettergram/966d9f13cd773177bcab to your computer and use it in GitHub Desktop.
Save lettergram/966d9f13cd773177bcab to your computer and use it in GitHub Desktop.
Run PCA on Digits
'''
Full code on: https://github.com/lettergram/PCA
read() obtains every image from training data
http://abel.ee.ucla.edu/cvxopt/_downloads/mnist.py
img = single image from read()
img[0] - id
img[1] - image of digit
'''
# Normalize the image
m = np.subtract(img[1], np.mean(img[1]))
# Turn 100 samples of every digit
# (28 by 28 image) and vectorizes it
matrix[img[0]] = np.reshape(m, -1).T
# Generate a new 784 x 1000 matrix
# for each centered, vectorized digit
matrix[img[0]] = np.vstack((matrix[img[0]], centered))
# Run PCA on this 784 x 1000 matrix
pca = (mlab.PCA(num.T, standardize=False)).Y.T
# Grab the top attributes/components
pca = pca[0:attrCount]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment