-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCA.py
62 lines (34 loc) · 1.24 KB
/
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
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
from mpl_toolkits import mplot3d
from scipy.sparse.linalg import svds
from scipy.sparse import rand, coo_matrix
import numpy as np
from matplotlib import pyplot
# Create sparse 3 dimensional matrix with random values
def data(m,n):
X = rand(m, n, density=0.1)
k = min(X.shape) - 1
return X, k
# Preform truncated SVD
def svd(X,k):
U, S, V = svds(X,k=k)
singular_values = np.dot(U,np.diag(S))
return singular_values
#Plot singular value vectors
def graph(X,singular_values):
fig = pyplot.figure()
ax = pyplot.axes(projection='3d')
ax.scatter(singular_values[:,0],singular_values[:,1])
fig2 = pyplot.figure()
ax1 =pyplot.axes(projection='3d')
ax1.plot3D(singular_values[:,0],singular_values[:,1])
print(singular_values)
pyplot.show()
def main():
m = int(input('Choose the number of rows in your sparse random matrix:\n'))
print('For now there is only 3 dimensional capability, in the future there will be added functionality for higher dimensions.')
X,k = data(m,3)
singular_values = svd(X,k)
print('Now a graph will be displayed with your singular vectors plotted along with the singular value matrix')
graph(X,singular_values)
if __name__ == '__main__':
main()