-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsub2.py
88 lines (65 loc) · 2.56 KB
/
sub2.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
sushkov = True
if sushkov:
read_movie_file = open('/home/msushkov/Dropbox/Caltech/Caltech Classes/CS/CS 156b/data/mu/data1.txt', 'r')
read_user_file = open('/home/msushkov/Dropbox/Caltech/Caltech Classes/CS/CS 156b/data/um/data1.txt', 'r')
qual = open('/home/msushkov/Dropbox/Caltech/Caltech Classes/CS/CS 156b/data/mu/qual.dta', 'r')
write_file = open('/home/msushkov/Dropbox/Caltech/Caltech Classes/CS/CS 156b/submissions/mu_average_of_averages.txt', 'w')
else:
read_movie_file = open('C:\Users\Mike\Documents\Mike\Caltech\\aJunior Fall\CS 156b\mu\data2.txt', 'r')
read_user_file = open('C:\Users\Mike\Documents\Mike\Caltech\\aJunior Fall\CS 156b\um\data2.txt', 'r')
qual = open('C:\Users\Mike\Documents\Mike\Caltech\\aJunior Fall\CS 156b\mu\qual.dta', 'r')
write_file = open('C:\Users\Mike\Documents\Mike\Caltech\\aJunior Fall\CS 156b\sub2\average_of_averages.txt', 'w')
previous_movie = '1'
index = 0
rating = 0.0
movie_ratings = {}
for line in read_movie_file:
vals = line.split()
current_movie = vals[1]
current_rating = int(vals[3])
if previous_movie != current_movie:
average_rating = float(rating) / float(index)
movie_ratings[previous_movie] = average_rating
index = 1
rating = current_rating
else:
if current_rating != 0:
index += 1
rating += current_rating
previous_movie = current_movie
# deals with the last line of the file
average_rating = rating / index
movie_ratings[previous_movie] = average_rating
read_movie_file.close()
# now for the user
previous_user = '1'
index = 0
rating = 0.0
user_ratings = {}
for line in read_user_file:
vals = line.split()
current_user = vals[0]
current_rating = int(vals[3])
if previous_user != current_user:
average_rating = float(rating) / float(index)
user_ratings[previous_user] = average_rating
index = 1
rating = current_rating
else:
if current_rating != 0:
index += 1
rating += current_rating
previous_user = current_user
# deals with the last line of the file
average_rating = rating / index
user_ratings[previous_user] = average_rating
read_user_file.close()
# creating the output
for line in qual:
vals = line.split()
current_user_rating = user_ratings[vals[0]]
current_movie_rating = movie_ratings[vals[1]]
predicted_score = (float(current_user_rating) + float(current_movie_rating)) / 2.0
write_file.write(str(predicted_score) + '\n')
write_file.close()
qual.close()