-
Notifications
You must be signed in to change notification settings - Fork 1
/
autox_recommend.py
181 lines (143 loc) · 9.12 KB
/
autox_recommend.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
179
180
181
from AutoX import user_feature_engineer, item_feature_engineer
from AutoX import ranker
from AutoX import process_recall
class AutoXRecommend():
def __init__(self):
pass
def recall(self, uids, method):
if method == 'popular':
return []
elif method == 'itemcf':
return []
def fit(self, interaction, user_info, item_info, uid, iid, mode='recall_and_rank', recall_method=None):
assert mode in ['recall', 'recall_and_rank']
if mode == 'recall':
assert recall_method in ['popular', 'itemcf']
recall = self.recall(interaction[uid].unique, method=recall_method, dtype='test')
result = process_recall(recall)
return result
# 召回
# popular_recall
print('popular_recall, train')
popular_recall_train = popular_recall(None, transactions_train, date='2020-09-15',
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='train')
print('popular_recall, valid')
popular_recall_valid = popular_recall(None, transactions_train, date='2020-09-22',
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='train')
print('history_recall, train')
history_recall_train = history_recall(None, transactions_train, date='2020-09-15',
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='train')
print('history_recall, valid')
history_recall_valid = history_recall(None, transactions_train, date='2020-09-22',
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='train')
print('itemcf_recall, train')
itemcf_recall_train = itemcf_recall(None, transactions_train, date='2020-09-15',
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='train',
topk=1000, use_iif=False, sim_last_days=14)
print('itemcf_recall, valid')
itemcf_recall_valid = itemcf_recall(None, transactions_train, date='2020-09-22',
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='train',
topk=1000, use_iif=False, sim_last_days=14)
print('binary_recall, train')
binary_recall_train = binary_recall(None, transactions_train, date='2020-09-15',
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='train', topk=1000)
print('binary_recall, valid')
binary_recall_valid = binary_recall(None, transactions_train, date='2020-09-22',
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='train', topk=1000)
# 合并数据
train = popular_recall_train.append(history_recall_train)
train.drop_duplicates(subset=[uid, iid], keep='first', inplace=True)
train = train.merge(itemcf_recall_train, on=['customer_id', 'article_id', 'label'], how='outer')
train = train.merge(binary_recall_train, on=['customer_id', 'article_id', 'label'], how='outer')
valid = popular_recall_valid.append(history_recall_valid)
valid.drop_duplicates(subset=[uid, iid], keep='first', inplace=True)
valid = valid.merge(itemcf_recall_valid, on=['customer_id', 'article_id', 'label'], how='outer')
valid = valid.merge(binary_recall_valid, on=['customer_id', 'article_id', 'label'], how='outer')
# 特征构造
train = feature_engineer(train, transactions_train,
date='2020-09-15',
customers=customers, articles=articles,
uid=uid, iid=iid, time_col=time_col,
last_days=7, dtype='train')
valid = feature_engineer(valid, transactions_train,
date='2020-09-22',
customers=customers, articles=articles,
uid=uid, iid=iid, time_col=time_col,
last_days=7, dtype='train')
# 排序
lgb_ranker, valid = ranker(train, valid,
uid=uid, iid=iid, time_col=time_col)
# 重新运行
# 召回
train_date = '2020-09-22'
test_date = '2020-09-22'
all_user = transactions_train[uid].unique()
popular_recall_train = popular_recall(None, transactions_train, date=train_date,
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='train')
popular_recall_test = popular_recall(all_user, transactions_train, date=test_date,
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='test')
history_recall_train = history_recall(None, transactions_train, date=train_date,
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='train')
history_recall_test = history_recall(all_user, transactions_train, date=test_date,
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='test')
itemcf_recall_train = itemcf_recall(None, transactions_train, date=train_date,
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='train',
topk=1000, use_iif=False, sim_last_days=14)
itemcf_recall_test = itemcf_recall(all_user, transactions_train, date=test_date,
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='test',
topk=1000, use_iif=False, sim_last_days=14)
binary_recall_train = binary_recall(None, transactions_train, date=train_date,
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='train', topk=1000)
binary_recall_test = binary_recall(all_user, transactions_train, date=test_date,
uid=uid, iid=iid, time_col=time_col,
last_days=7, recall_num=100, dtype='test', topk=1000)
# 合并数据
train = popular_recall_train.append(history_recall_train)
train.drop_duplicates(subset=[uid, iid], keep='first', inplace=True)
train = train.merge(itemcf_recall_train, on=['customer_id', 'article_id', 'label'], how='outer')
train = train.merge(binary_recall_train, on=['customer_id', 'article_id', 'label'], how='outer')
test = popular_recall_test.append(history_recall_test)
test.drop_duplicates(subset=[uid, iid], keep='first', inplace=True)
test = test.merge(itemcf_recall_test, on=['customer_id', 'article_id'], how='outer')
test = test.merge(binary_recall_test, on=['customer_id', 'article_id'], how='outer')
# 特征构造
train = feature_engineer(train, transactions_train,
date='2020-09-22',
customers=customers, articles=articles,
uid=uid, iid=iid, time_col=time_col,
last_days=7, dtype='train')
test = feature_engineer(test, transactions_train,
date='2020-09-22',
customers=customers, articles=articles,
uid=uid, iid=iid, time_col=time_col,
last_days=7, dtype='test')
# 排序
ranker(train, test)
def transform(self, uids):
# 召回
popular_recall_test = self.recall_test(uids, method='popular')
itemcf_recall_test = self.recall_test(uids, method='itemcf')
test = popular_recall_test.append(itemcf_recall_test)
test.drop_duplicates(subset=[self.uid, self.iid], keep='first', inplace=True)
# 特征构造
test = test.merge(self.user_fe, on=self.uid, how='left')
test = test.merge(self.item_fe, on=self.iid, how='left')
test = test.merge(self.inter_fe, on=[self.iid, self.iid], how='left')
# 排序
result = ranker.precict(test)
return result