-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_need_predict.py
178 lines (152 loc) · 5.01 KB
/
extract_need_predict.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 os
import my_dic
import numpy as np
import random
father=os.path.abspath(os.path.join(os.path.dirname("__file__"),os.path.pardir))
user_action_seq=open(father+"/JDATA用户购买时间预测_A榜/user_action_seq.txt","r",encoding="utf-8")
sku_info_dic=my_dic.sku_info_dic
user_info_dic=my_dic.user_info_dic
sku_feature=my_dic.sku_feature_dic
user_feature=my_dic.user_feature_dic
sku_len=len(sku_feature['1'])
user_len=len(user_feature['1'])
# 为了更好的保存用户对商品的行为特征,我们不能对浏览和关注取one-hot,所以这里设浏览权重为1,关注权重为3,购买权重为10
def have_purchase(subline):
if '-' not in subline:
return False
for each in subline.split(','):
if each.split('-')[1] == '3':
if sku_info_dic[each.split('-')[0]]['cate'] == '101' or sku_info_dic[each.split('-')[0]]['cate'] == '30':
return True
return False
def have_purchase_spe(subline,cate):
if '-' not in subline:
return False
for each in subline.split(','):
if each.split('-')[1] == '3':
if sku_info_dic[each.split('-')[0]]['cate'] == cate:
return True
return False
def have_overview_spe(subline,cate):
if '-' not in subline:
return False
for each in subline.split(','):
if each.split('-')[1] == '1':
if sku_info_dic[each.split('-')[0]]['cate'] == cate:
return True
return False
def have_watch_spe(subline,cate):
if '-' not in subline:
return False
for each in subline.split(','):
if each.split('-')[1] == '2':
if sku_info_dic[each.split('-')[0]]['cate'] == cate:
return True
return False
total_cate = ['71', '46', '83', '101', '1', '30']
action_weight = {}
action_weight['1'] = 1
action_weight['2'] = 5
action_weight['3'] = 15
#[1, 2, 4, 7, 12, 20, 33, 54, 88]
def Build_feature(idx, line):
idx=idx+1
datelist = [0 ,1, 2, 4, 7, 12, 20, 33, 54, 88]
out = []
for i in range(1,len(datelist)):
out.extend(build_feature(line[idx - datelist[i]:idx-datelist[i-1]]))
return out
def build_feature(seq):
outnumpy = np.zeros(sku_len)
for i in range(len(seq)):
if seq[i] == "0":
each_feature = np.zeros(sku_len)
else:
each_feature = np.zeros(sku_len)
for each_sku in seq[i].split(','):
each_sku = each_sku.split('-')
each_feature += sku_feature[each_sku[0]]*action_weight[each_sku[1]]
outnumpy += each_feature
return outnumpy.tolist()
def lastpurchase_cate(idx, line):
for i in range(idx, 0, -1):
if have_purchase(line[i]):
return (idx - i)/365
return 1
def lastpurchase_all(idx,line):
out={}
for cate in total_cate:
out[cate] = 1
for i in range(idx,0,-1):
if have_purchase_spe(line[i],cate):
out[cate]=(idx-i)/365
break
out1=[]
for key in total_cate:
out1.append(out[key])
return out1
def lastwatch_all(idx,line):
out={}
for cate in total_cate:
out[cate] = 1
for i in range(idx,0,-1):
if have_watch_spe(line[i],cate):
out[cate]=(idx-i)/365
break
out1=[]
for key in total_cate:
out1.append(out[key])
return out1
def lastoverview_all(idx,line):
out={}
for cate in total_cate:
out[cate] = 1
for i in range(idx,0,-1):
if have_overview_spe(line[i],cate):
out[cate]=(idx-i)/365
break
out1=[]
for key in total_cate:
out1.append(out[key])
return out1
def find_object_cate(line):
out = []
n = 0
for i in line:
if have_purchase(i):
out.append(n)
n += 1
return out
next(user_action_seq)
line=user_action_seq.readline()
n=0
feature_pre=0
while line!="":
line = line.replace("\n", "").rstrip('\t').split("\t")
# 建立用户购买目标品类的日期list
idx = 365
windows_feature = [random.random()]
windows_feature.append(lastpurchase_cate(idx, line))
# 所有类别的上一次购买时间
windows_feature.extend(lastpurchase_all(idx, line))
# 所有类别的上一次浏览时间
windows_feature.extend(lastoverview_all(idx, line))
# 所有类别的上一次关注时间
windows_feature.extend(lastwatch_all(idx, line))
windows_feature.extend(user_feature[line[0]].tolist())
windows_feature.extend(Build_feature(idx, line))
a_feature = [int(line[0])]
a_feature.extend(windows_feature)
try:
feature_pre[n] = np.array(a_feature)
except:
print('跳入错误')
feature_pre = np.zeros((98924, len(a_feature)))
feature_pre[n] = np.array(a_feature)
n += 1
if n%1000==0:
print(a_feature)
print(feature_pre.shape)
line = user_action_seq.readline()
np.save("../JDATA用户购买时间预测_A榜/feature_predict_5.npy",feature_pre)