-
Notifications
You must be signed in to change notification settings - Fork 0
/
recommendation.py
73 lines (60 loc) · 1.88 KB
/
recommendation.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
from project.model import Facilities
from user_list import L as user_list
import numpy as np
def classify(x):
if x == 1 :
return 0
if x == 2 :
return 0.4
if x == 3 :
return 0.8
if x == 4 :
return 1
def score_matrix_func():
x = Facilities.query.all()
f_list = [] # facility list
for x in x :
facilities = []
if x.gym is True:
facilities.append(1)
else:
facilities.append(0)
if x.FoodBeverages is True:
facilities.append(1)
else:
facilities.append(0)
if x.Parking is True:
facilities.append(1)
else:
facilities.append(0)
if x.Tv is True:
facilities.append(1)
else:
facilities.append(0)
if x.wifi is True:
facilities.append(1)
else:
facilities.append(0)
f_list.append(facilities)
# print(f_list)
score_matrix = np.zeros((len(user_list),len(f_list)))
# print(score_matrix.shape)
for i in range(len(user_list)):
a = classify(user_list[i][0])
b = classify(user_list[i][1])
c = classify(user_list[i][2])
d = classify(user_list[i][3])
for j in range(len(f_list)):
score_matrix[i][j] = (a* f_list[j][0] + b*f_list[j][1] + c*f_list[j][2] + d*f_list[j][3]) / (a + b + c + d + 1e-9)
# print(score_matrix.shape)
similarity_hotel = score_matrix.T.dot(score_matrix) + 1e-9
# print(similarity_hotel.shape)
norms = np.array([np.sqrt(np.diagonal(similarity_hotel))])
similarity_hotel = similarity_hotel/(norms*norms.T)
return similarity_hotel
similarity_hotel = score_matrix_func()
# a = np.zeros(similarity_hotel.shape)[0]
# print(a.shape)
# similarity_hotel[1][1]
# print(similarity_hotel.shape)
# score_matrix_func()