-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
37 lines (26 loc) · 958 Bytes
/
main.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
32
33
34
35
36
37
import numpy as np
import sklearn.datasets
# For verificatoin.
import sklearn.decomposition
from models import PCA
def main(n_components=2):
# Fisher iris dataset.
data = sklearn.datasets.load_iris()
X = data['data']
pca = PCA(n_components=n_components, method='eig')
eig_pc = pca.fit_transform(X)
pca = PCA(n_components=n_components, method='svd')
svd_pc = pca.fit_transform(X)
pca = sklearn.decomposition.PCA(n_components=n_components)
sklearn_pc = pca.fit_transform(X)
# Principal components are the same except for the signs.
sklearn_pc[:, 1] = -sklearn_pc[:, 1]
# Assert that SVD and Eig results the same.
assert np.allclose(eig_pc, svd_pc)
assert np.allclose(sklearn_pc, svd_pc)
pca = PCA(n_components=X.shape[1], method='svd')
svd_pc = pca.fit_transform(X)
# Verify the restoration.
assert np.allclose(X, pca.restore(svd_pc))
if __name__ == '__main__':
main()