Skip to content

Commit

Permalink
compare pca svd
Browse files Browse the repository at this point in the history
  • Loading branch information
bob7783 committed Jan 27, 2019
1 parent be04d8c commit 365e9e9
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions unsupervised_class2/compare_pca_svd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python
# https://www.udemy.com/unsupervised-deep-learning-in-python
from __future__ import print_function, division
from builtins import range, input
# Note: you may need to update your version of future
# sudo pip install -U future

import numpy as np
import matplotlib.pyplot as plt

from sklearn.decomposition import PCA, TruncatedSVD
from util import getKaggleMNIST


X, Y, _, _ = getKaggleMNIST()
m = X.mean(axis=0)
s = X.std(axis=0)
np.place(s, s == 0, 1)
X = (X - m) / s

pca = PCA()
svd = TruncatedSVD()

Z1 = pca.fit_transform(X)
Z2 = svd.fit_transform(X)

plt.subplot(1,2,1)
plt.scatter(Z1[:,0], Z1[:,1], c=Y)
plt.subplot(1,2,2)
plt.scatter(Z2[:,0], Z2[:,1], c=Y)
plt.show()

0 comments on commit 365e9e9

Please sign in to comment.