-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.py
76 lines (63 loc) · 2.34 KB
/
analysis.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
import math
import random
import sys
from db_api import *
from recommender import average_rating, nearest_neighbour, slope_one, hybrid_algorithm
random.seed(0)
_DEBUG = False
_SAMPLE_NUMBER = 100
def rmsd(a, b):
"""calculate root-mean-square deviation"""
return math.sqrt(statistics.mean([(i - round(j)) ** 2 for i, j in zip(a, b)]))
def parse_result(a):
if a > 5:
a = 5
elif a < 0:
a = 0
return a
def main():
"""calculate RMSD for different recommender algorithm"""
users = [i.id for i in list(User.select())]
sample_users = random.sample(users, _SAMPLE_NUMBER)
actual_result = []
average_result = []
nearest_neighbour_result = []
slope_one_result = []
hybird_result = []
for user_id in sample_users:
print('Current user:', get_user_by_id(user_id))
movie_id = random.choice(get_movie_rating_by_user(user_id)).movie_id
print('Current movie:', get_movie_by_id(movie_id))
actual = get_user_movie_rating(user_id, movie_id)
print('Actual Rating:', actual)
actual_result.append(actual)
avg = average_rating(movie_id, True)
print('Average Rating:', avg)
average_result.append(avg)
nearest = nearest_neighbour(user_id, movie_id, True)
print('Nearest Neighbour Rating:', nearest)
nearest_neighbour_result.append(nearest)
slope = slope_one(user_id, movie_id, True)
print('Slope One Rating:', slope)
slope_one_result.append(parse_result(slope))
hybrid = hybrid_algorithm(avg, nearest, slope, True)
print('Hybrid Algorithm Rating:', hybrid)
hybird_result.append(parse_result(hybrid))
print()
if _DEBUG:
print(actual_result)
print(average_result)
print(nearest_neighbour_result)
print(slope_one_result)
print(hybird_result)
print('RMSD of each recommender system')
print(' Average Rating '.center(80, '#'))
print(rmsd(actual_result, average_result))
print(' Nearest Neighbour '.center(80, '#'))
print(rmsd(actual_result, nearest_neighbour_result))
print(' Slope One '.center(80, '#'))
print(rmsd(actual_result, slope_one_result))
print(' Hybrid Algorithm '.center(80, '#'))
print(rmsd(actual_result, hybird_result))
if __name__ == '__main__':
sys.exit(main())