-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
84 lines (62 loc) · 1.75 KB
/
common.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
"""
Assignment #2
Project: To implement both SVD and CUR Matrix decomposition algorithms and
compare their efficency (in terms of space, time etc.)
Instructor: Dr. Aruna Malapati
Contributors: G V Sandeep 2014A7PS106H
Kushagra Agrawal 2014AAPS334H
Snehal Wadhwani 2014A7PS430H
Course: CS F469 Information Retrieval
"""
import numpy as np
import math
import time
from numpy import linalg as LA
def handle_input(filename):
"""
TAKING INPUT AND FORMING A MATRIX OUT OF IT
Input : A text file containing lines of the format "USER_ID ITEM_ID RATING"
Output : A matrix which stores this data
"""
input_file = open(filename,"r")
rating_raw = input_file.readlines()
rating_list = []
for line in rating_raw:
ind_rating = []
line = line.split(" ")
user = int(line[0])
ind_rating.append(user)
item = int(line[1])
ind_rating.append(item)
rating = float(line[2])
ind_rating.append(rating)
rating_list.append(ind_rating)
max_item = 0
max_user = rating_list[len(rating_list)-1][0]
for rating in rating_list:
if rating[1] > max_item:
max_item = rating[1]
ratings = np.zeros((max_user, max_item))
for rating in rating_list:
ratings[rating[0]-1][rating[1]-1] = rating[2]
return ratings
def calc_error(ratings_org, ratings_svd):
"""
Caluclating the Frobenius Error
"""
error = 0
for i in range(0,len(ratings_org)):
for j in range(0,len(ratings_org[i])):
error = error + (ratings_org[i][j]-ratings_svd[i][j])**2
error = math.sqrt(error)
return error
def print_matrix(matrix):
"""
UTITLITY FUNCTION TO PRINT A MATRIX
Input : A matrix
Output: prints the matrix onto the console
"""
for i in xrange(len(matrix)):
for j in xrange(len(matrix[i])):
print matrix[i][j],
print "\n"