-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixture_model.py
68 lines (58 loc) · 2.62 KB
/
mixture_model.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
from mdp import MDP
from tabulate import tabulate
class MixtureModel:
"""
Class to implement mixture models of multiple MDPs.
"""
def __init__(self, path='data-mini', alpha=1, k=3, beta_weight=1, discount_factor=0.999, verbose=True, save_path="mixture-models"):
"""
The constructor for the MixtureModel class.
:param path: path to data
:param alpha: the proportionality constant when considering transitions
:param k: the number of models
:param discount_factor: the discount factor for each MDP
:param verbose:flag to show steps
:param save_path: the path to which models should be saved and loaded from
"""
self.k = k
self.df = discount_factor
self.alpha = alpha
self.beta_weight = beta_weight
self.path = path
self.verbose = verbose
self.save_path = save_path
def generate_model(self, max_iteration):
"""
Method to generate and save the various models.
:return: None
"""
# Generate models whose n-gram values change from 1...k
for i in range(1, self.k+1):
# Initialise the MDP
print('Creating MDP with k = ', str(i),' and running Policy iteration with max iterations = ', str(max_iteration))
mm = MDP(path=self.path, alpha=self.alpha, beta_weight=self.beta_weight, k=i,
discount_factor=self.df, verbose=self.verbose, save_path=self.save_path)
mm.initialise_mdp()
# Run the policy iteration and save the model
mm.policy_iteration(max_iteration=max_iteration)
def predict(self, user_id):
"""
Method to provide recommendations.
:param user_id: the id of the user
:return: a list of tuples with the recommendations and their corresponding score
"""
recommendations = {}
for i in range(1, self.k+1):
# Initialise each MDP
mm = MDP(path=self.path, alpha=self.alpha, k=i,
discount_factor=self.df, verbose=self.verbose, save_path=self.save_path)
# Load its corresponding policy
mm.load_policy("mdp-model_k=" + str(i) + ".pkl")
# Append the recommendation into the list
rec_list = mm.recommend(user_id)
for rec in rec_list:
if rec[0] not in recommendations:
recommendations[rec[0]] = 0
recommendations[rec[0]] += (1/self.k) * rec[1]
# Sort according to value for each recommendation
return sorted(recommendations.items(), key=lambda x: x[1], reverse=True)