forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpca.py
31 lines (25 loc) · 841 Bytes
/
pca.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python
# https://www.udemy.com/unsupervised-deep-learning-in-python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from util import getKaggleMNIST
def main():
Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST()
pca = PCA()
reduced = pca.fit_transform(Xtrain)
plt.scatter(reduced[:,0], reduced[:,1], s=100, c=Ytrain, alpha=0.5)
plt.show()
plt.plot(pca.explained_variance_ratio_)
plt.show()
# cumulative variance
# choose k = number of dimensions that gives us 95-99% variance
cumulative = []
last = 0
for v in pca.explained_variance_ratio_:
cumulative.append(last + v)
last = cumulative[-1]
plt.plot(cumulative)
plt.show()
if __name__ == '__main__':
main()