forked from bc3527/Netflix-Rating-Prediction-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.py
180 lines (141 loc) · 3.3 KB
/
Cache.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
### Cache maker
### RMSE
# Imports #
import sys
import time
# ------
# Cache
# ------
def cache_start_users(w) :
cache = {}
movies = [0]
input_users = {}
input_movies = [0]*17771
file_name = "movie_titles.txt"
read_file = open(file_name, 'r')
for line in read_file :
s = line.split(',')
movies.append(s[1])
file_name = "probe.txt"
read_file = open(file_name, 'r')
for line in read_file :
s = line.split(":")
if len(s) != 1 :
movie = s[0]
input_movies[int(movie)] = 1
input_users[movie] = []
else :
input_users[movie].append(s[0].strip())
for m_id in range (1, 17771) :
file_name = "/u/downing/cs/netflix/training_set/mv_"+'%0*d' % (7, m_id)+".txt"
read_file = open(file_name, 'r')
cache = user_ratings_cache(read_file, w, cache, movies, input_users, input_movies)
for a,b in cache.items() :
w.write(a + ":\n")
for c,d in b.items() :
avg = d[0]/d[1]
w.write(c + "--" + str(avg) + "\n")
def cache_start_movies(w) :
for m_id in range (1, 17771) :
file_name = "/u/downing/cs/netflix/training_set/mv_"+'%0*d' % (7, m_id)+".txt"
read_file = open(file_name, 'r')
movie_ratings_cache(read_file, w)
def movie_ratings_cache( r, w ) :
cache = [0]
cache[0] = "null"
index = 0
avg = 0
count = 0.0
s = "a"
movie_id = ""
while (s != "") :
s = r.readline()
x = s.split(",")
if len(x) == 1 :
x = s.split(":")
if cache[0] == "null" :
cache[0] = "progress"
movie_id = x[0]
else :
avg = avg/count
if cache[0] == "progress" :
cache =[[movie_id, avg]]
else :
cache.append([movie_id, avg])
count = 0
index += 1
movie_id = x[0]
else :
count += 1
avg += int(x[1])
for a in cache :
w.write(a[0] + ":" + str(a[1]) + "\n")
def user_ratings_cache(r, w, cache, movies, input_users, input_movies) :
s = "a"
mov = ""
flag = 0
ids = -1
for s in r:
x = s.split(",")
ids += 1
if len(x) == 1 :
d = s.split(":")
mov = d[0]
year = movies[int(mov)]
if input_movies[int(mov)] == 0 :
flag = 0
else :
flag = 1
else :
if flag == 1 :
user_id = x[0]
movie_users_list = input_users[mov]
if user_id in movie_users_list :
if user_id not in cache :
cache[user_id] = {year: [0.0 + int(x[1]), 1.0]}
else :
user_id_temp = cache[user_id]
count = 0
if year in user_id_temp :
temp = user_id_temp[year]
temp[1] += 1
temp[0] = temp[0] + int(x[1])
user_id_temp[year] = temp
cache[user_id] = user_id_temp
else :
user_id_temp[year] = [0.0 + int(x[1]), 1.0]
cache[user_id] = user_id_temp
return cache
# ------
# RMSE
# ------
def create_actual_database(actual) :
for m_id in range (1, 17771) :
file_name = "/u/downing/cs/netflix/training_set/mv_"+'%0*d' % (7, m_id)+".txt"
read_file = open(file_name, 'r')
for line in read_file :
s = line.split(",")
if len(s) == 1 :
s = line.split(":")
actual[s[0]] = 0
movie_id = s[0]
else :
d = actual[movie_id]
user_id = s[0]
rating = float(s[1])
if d == 0 :
d = {user_id:rating}
actual[movie_id] = d
else :
d[user_id] = rating
actual[movie_id] = d
print actual
# ------
# run
# ------
# cache_start_users(sys.stdout)
#
# or
#
# cache_start_movies
cache_start_users(sys.stdout)