forked from diksha16017/CF_RecommendationSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlternatingMinimization.py
50 lines (43 loc) · 1.18 KB
/
AlternatingMinimization.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
import numpy as np
import os
k = 3
count = 30
totalMovies = 5
totalUsers = 5
R = np.zeros((totalUsers, totalMovies))
U = np.random.rand(totalUsers, k)
os.chdir("C:\Users\AJAY\PycharmProjects\CF_Test_UserUser")
def readTrainData(R):
with open("u2.txt") as myFile:
for line in myFile:
line = line.split("\t")
user = int(line[0])-1
movie = int(line[1])
movie = movie-1
rating = int(line[2])
R[user, movie] = rating
print 'R'
print R
def matrix_factorisation(R, U):
global count
while count > 0:
count = count - 1
VK = np.linalg.lstsq(U, R)[0]
VK[VK < 0] = 0
UK_temp = np.linalg.lstsq(np.transpose(VK), np.transpose(R))[0]
UK = np.transpose(UK_temp)
UK[UK < 0] = 0
U = UK
if np.array_equal(np.dot(UK, VK), R) == True:
break
print 'printing final UK and VK'
print UK
print '***********'
print VK
print '***********'
print 'printing final R'
R_final = np.dot(UK, VK)
R_final[R_final < 0] = 0
print R_final
readTrainData(R)
matrix_factorisation(R, U)