-
Notifications
You must be signed in to change notification settings - Fork 0
/
userLayer.py
178 lines (140 loc) · 7.33 KB
/
userLayer.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
178
import numpy as np
import scipy.sparse as sps
import pandas as pd
import os
from datetime import datetime
from Utils.Logger import Logger
from Data_manager.HMDatasetReader import HMDatasetReader
from Data_manager.split_functions.split_train_validation_random_holdout import \
split_train_in_two_percentage_global_sample
from Evaluation.Evaluator import EvaluatorHoldout
def run_userwise():
dataset_name = "hm"
reader = HMDatasetReader(False)
DATASET_PATH = os.getenv('DATASET_PATH')
PROCESSED_PATH = os.getenv('PROCESSED_PATH')
dataset = reader.load_data(
'{}/processed_URM_train_20180920_20200916_val_20200916_20200923/{}/'.format(DATASET_PATH, dataset_name))
# URM_train_validation, URM_test = split_train_in_two_percentage_global_sample(URM_all, train_percentage=0.85)
# URM_train, URM_validation = split_train_in_two_percentage_global_sample(URM_train_validation, train_percentage=0.85)
ICM_name = "ICM_idxgrp_idx_prdtyp"
ICM_train = dataset.get_ICM_from_name(ICM_name)
UCM_name = "UCM_postal_code"
UCM_train = dataset.get_loaded_UCM_dict()[UCM_name]
URM_train = dataset.get_URM_from_name('URM_train')
URM_validation = dataset.get_URM_from_name('URM_validation')
URM_test = URM_validation
evaluator_validation = EvaluatorHoldout(URM_validation, cutoff_list=[12])
evaluator_test = EvaluatorHoldout(URM_validation, cutoff_list=[12])
profile_length = np.ediff1d(sps.csr_matrix(URM_train).indptr)
profile_length = [x for x in profile_length if x > 0]
block_size = int(len(profile_length) * 0.05)
sorted_users = np.argsort(profile_length)
print(block_size, sorted_users, max(profile_length))
for group_id in range(0, 20):
start_pos = group_id * block_size
end_pos = min((group_id + 1) * block_size, len(profile_length))
users_in_group = sorted_users[start_pos:end_pos]
users_in_group_p_len = np.array(profile_length)[users_in_group]
print("Group {}, #users in group {}, average p.len {:.2f}, median {}, min {}, max {}".format(
group_id,
users_in_group.shape[0],
users_in_group_p_len.mean(),
np.median(users_in_group_p_len),
users_in_group_p_len.min(),
users_in_group_p_len.max()))
from Recommenders.NonPersonalizedRecommender import TopPop
from Recommenders.KNN.UserKNNCFRecommender import UserKNNCFRecommender
from Recommenders.KNN.ItemKNNCFRecommender import ItemKNNCFRecommender
from Recommenders.SLIM.Cython.SLIM_BPR_Cython import SLIM_BPR_Cython
from Recommenders.SLIM.SLIMElasticNetRecommender import SLIMElasticNetRecommender, \
MultiThreadSLIM_SLIMElasticNetRecommender
from Recommenders.GraphBased.P3alphaRecommender import P3alphaRecommender
from Recommenders.GraphBased.RP3betaRecommender import RP3betaRecommender
from Recommenders.MatrixFactorization.Cython.MatrixFactorization_Cython import MatrixFactorization_BPR_Cython, \
MatrixFactorization_FunkSVD_Cython, MatrixFactorization_AsySVD_Cython
from Recommenders.MatrixFactorization.PureSVDRecommender import PureSVDRecommender
from Recommenders.KNN.ItemKNN_CFCBF_Hybrid_Recommender import ItemKNN_CFCBF_Hybrid_Recommender
from Recommenders.KNN.UserKNN_CFCBF_Hybrid_Recommender import UserKNN_CFCBF_Hybrid_Recommender
MAP_recommender_per_group = {}
collaborative_recommender_class = {
# "SLIMEN": MultiThreadSLIM_SLIMElasticNetRecommender,
"TopPop": TopPop,
# "UserKNNCF": UserKNNCFRecommender,
"ItemKNNCF": ItemKNNCFRecommender,
"P3alpha": P3alphaRecommender,
"RP3beta": RP3betaRecommender,
"PureSVD": PureSVDRecommender,
}
hybird_recommender_class = {"ItemKNNCFCBF": ItemKNN_CFCBF_Hybrid_Recommender,
# "UserKNNCFCBF": UserKNN_CFCBF_Hybrid_Recommender
}
recommender_object_dict = {}
for label, recommender_class in collaborative_recommender_class.items():
recommender_object = recommender_class(URM_train)
recommender_object.fit()
recommender_object_dict[label] = recommender_object
for label, recommender_class in hybird_recommender_class.items():
if label == "ItemKNNCFCBF":
recommender_object = recommender_class(URM_train, ICM_train)
recommender_object.fit()
recommender_object_dict[label] = recommender_object
else:
recommender_object = recommender_class(URM_train, UCM_train)
recommender_object.fit()
recommender_object_dict[label] = recommender_object
cutoff = 12
for group_id in range(0, 20):
start_pos = group_id * block_size
end_pos = min((group_id + 1) * block_size, len(profile_length))
users_in_group = sorted_users[start_pos:end_pos]
users_in_group_p_len = np.array(profile_length)[users_in_group]
print("Group {}, #users in group {}, average p.len {:.2f}, median {}, min {}, max {}".format(
group_id,
users_in_group.shape[0],
users_in_group_p_len.mean(),
np.median(users_in_group_p_len),
users_in_group_p_len.min(),
users_in_group_p_len.max()))
users_not_in_group_flag = np.isin(sorted_users, users_in_group, invert=True)
users_not_in_group = sorted_users[users_not_in_group_flag]
evaluator_test = EvaluatorHoldout(URM_test, cutoff_list=[cutoff], ignore_users=users_not_in_group)
for label, recommender in recommender_object_dict.items():
result_df, _ = evaluator_test.evaluateRecommender(recommender)
if label in MAP_recommender_per_group:
MAP_recommender_per_group[label].append(result_df.loc[cutoff]["MAP"])
else:
MAP_recommender_per_group[label] = [result_df.loc[cutoff]["MAP"]]
import matplotlib.pyplot as plt
_ = plt.figure(figsize=(16, 9))
for label, recommender in recommender_object_dict.items():
results = MAP_recommender_per_group[label]
plt.scatter(x=np.arange(0, len(results)), y=results, label=label)
plt.ylabel('MAP')
plt.xlabel('User Group')
plt.legend()
plt.show()
plt.savefig(os.path.join(DATASET_PATH, 'userwise_URM_train_20180920_20200916_val_20200916_20200923.png'))
plt.title("userwise_all_2_years")
plt.savefig(os.path.join(DATASET_PATH, 'userwise_URM_train_20180920_20200916_val_20200916_20200923.png'))
if __name__ == '__main__':
# current date and time
start = datetime.now()
log_for_telegram_group = True
logger = Logger('Userwise TEST - Start time:' + str(start))
if log_for_telegram_group:
logger.log('Started Userwise')
print('Started Userwise')
try:
run_userwise()
except Exception as e:
if log_for_telegram_group:
logger.log('We got an exception! Check log and turn off the machine.')
logger.log('Exception: \n{}'.format(str(e)))
print('We got an exception! Check log and turn off the machine.')
print('Exception: \n{}'.format(str(e)))
if log_for_telegram_group:
logger.log('Userwise finished! Check results and turn off the machine.')
end = datetime.now()
logger.log('End time:' + str(end) + ' Program duration:' + str(end - start))
print('Hyper parameter search finished! Check results and turn off the machine.')