forked from frovedis/frovedis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pca_demo.py
executable file
·95 lines (72 loc) · 2.3 KB
/
pca_demo.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
import sys
import numpy as np
from frovedis.matrix.dense import FrovedisRowmajorMatrix
from frovedis.exrpc.server import FrovedisServer
from frovedis.mllib.decomposition import PCA
#from sklearn.decomposition import PCA
# initializing the Frovedis server
argvs = sys.argv
argc = len(argvs)
if (argc < 2):
print ('Please give frovedis_server calling command as the first argument \n(e.g. "mpirun -np 2 -x /opt/nec/nosupport/frovedis/ve/bin/frovedis_server")')
quit()
FrovedisServer.initialize(argvs[1])
# sample numpy dense data (3x3)
mat = np.matrix([[1.0, 0.0, 7.0, 0.0, 0.0],
[2.0, 0.0, 3.0, 4.0, 5.0],
[4.0, 0.0, 0.0, 6.0, 7.0]],
#dtype=np.float32)
dtype=np.float64)
#pca = PCA(n_components=2, svd_solver='arpack', copy=True)
pca = PCA(n_components=2, svd_solver='arpack', whiten=True)
print("input matrix: ")
print(mat) # for numpy matrix
# #mat = FrovedisRowmajorMatrix(mat)
# #print(mat.to_numpy_matrix()) # for frovedis rowmajor matrix
pca.fit(mat)
score = pca.fit_transform(mat)
print("Mean: ")
print(pca.mean_)
print("input matrix after fit: ")
print(mat) # for numpy matrix
#print(mat.to_numpy_matrix()) # for frovedis rowmajor matrix
print("score:")
print(score)
print("n_components_: ")
print(pca.n_components_)
print("n_samples_: ")
print(pca.n_samples_)
print("n_features_: ")
print(pca.n_features_)
print("components_: ")
print(pca.components_)
print("explained variance: ")
print(pca.explained_variance_)
print("explained variance_ratio: ")
print(pca.explained_variance_ratio_)
print("singular_values: ")
print(pca.singular_values_)
print("noise variance: ")
print(pca.noise_variance_)
# a = pca.pca_res_.to_numpy_results()
# print("to_numpy_results")
# print(a)
# for debug purpose: showing results on server
#pca.pca_res_.debug_print()
# releasing results from server
#pca.pca_res_.release()
a = np.array([[1.0, 0.0, 7.0, 0.0, 0.0],
[2.0, 0.0, 3.0, 4.0, 5.0],
[4.0, 0.0, 0.0, 6.0, 7.0]],
#dtype=np.float32)
dtype=np.float64)
X1 = pca.transform(a)
#X1 = pca.transform(FrovedisRowmajorMatrix(a))
print("transformed data")
print(X1)
X2 = pca.inverse_transform(X1)
print("inverse_transform data")
print(X2)
pca.save("./out/PCAModel")
FrovedisServer.shut_down()