Skip to content

Commit ec5a924

Browse files
committed
pca impl
1 parent 6f46833 commit ec5a924

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

unsupervised_class2/pca_impl.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python
2+
# https://www.udemy.com/unsupervised-deep-learning-in-python
3+
from __future__ import print_function, division
4+
from builtins import range, input
5+
# Note: you may need to update your version of future
6+
# sudo pip install -U future
7+
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
11+
from util import getKaggleMNIST
12+
13+
# get the data
14+
Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST()
15+
16+
# decompose covariance
17+
covX = np.cov(Xtrain.T)
18+
lambdas, Q = np.linalg.eigh(covX)
19+
20+
21+
# lambdas are sorted from smallest --> largest
22+
# some may be slightly negative due to precision
23+
idx = np.argsort(-lambdas)
24+
lambdas = lambdas[idx] # sort in proper order
25+
lambdas = np.maximum(lambdas, 0) # get rid of negatives
26+
Q = Q[:,idx]
27+
28+
29+
# plot the first 2 columns of Z
30+
Z = Xtrain.dot(Q)
31+
plt.scatter(Z[:,0], Z[:,1], s=100, c=Ytrain, alpha=0.3)
32+
plt.show()
33+
34+
35+
# plot variances
36+
plt.plot(lambdas)
37+
plt.title("Variance of each component")
38+
plt.show()
39+
40+
# cumulative variance
41+
plt.plot(np.cumsum(lambdas))
42+
plt.title("Cumulative variance")
43+
plt.show()

0 commit comments

Comments
 (0)