-
Notifications
You must be signed in to change notification settings - Fork 1
/
svd.py
88 lines (68 loc) · 1.87 KB
/
svd.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
import csv
import numpy as np
def SVD(M,dim):
Mt=np.transpose(M)
#To Calculate U
prd=np.dot(M,Mt)
#Eigen Value Decomposition
eigenvalue,eigenvec=np.linalg.eig(prd)
#Indirect sort on eigenvalue to find out the proper indices, the same can
#be used with corresponding eigenvectors
sortindex=eigenvalue.argsort()[::-1]
#Sort Eigen values
eigenvalue=eigenvalue[sortindex]
#Sort and reduce U to nXdim
U=eigenvec[:,sortindex]
U=U[:,0:dim]
U=np.real(U)
U=np.around(U,decimals=2)
#To calculate sigma
sigma=np.sqrt(abs(eigenvalue))
sigma=sigma[0:dim]
sigma=np.around(sigma,decimals=2)
#To Calculate V
prd=np.dot(Mt,M)
eigenvalue,eigenvec=np.linalg.eig(prd)
sortindex=eigenvalue.argsort()[::-1]
V=eigenvec[:,sortindex]
V=V[:,0:dim]
V=np.real(V)
V=np.around(V,decimals=2)
return U,sigma,V
def query(q,V):
prd=np.dot(q,V)
Vt=np.transpose(V)
print(abs(prd))
other=np.dot(prd,Vt)
print(abs(other))
return
#Reading the data
dataset=list()
fp=open('dataset.txt','r')
reader = csv.reader(fp, delimiter=',')
for row in reader:
dataset.append(row)
#print(dataset)
movies=list()
users=list()
for i in dataset[0]:
movies.append(i)
for i in dataset:
users.append(i[0])
movies.remove("X")
users.remove("X")
#print(movies)
#print(users)
dataset.pop(0) #Removing movies
for i in dataset: #Removing users
i.pop(0)
m=list(dataset)
M=np.mat(m,dtype=int)
print(M)
U,sigma,V=SVD(M,2)
print("U ", U)
print("---------------------------------------")
print("sigma ", sigma)
print("---------------------------------------")
print("Vt ", V.transpose())
print("---------------------------------------")