forked from guoday/ICME2019-CTR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepocess.py
194 lines (170 loc) · 6.07 KB
/
prepocess.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
182
183
184
185
186
187
188
189
190
191
192
193
194
import tqdm
import pandas as pd
import numpy as np
import random
import json
import os
import pickle
import gc
from collections import Counter
import math
from sklearn.model_selection import train_test_split
def split_data(reprepocess=False):
#split data, (train,test) for inference, (train_dev,dev) for dev
if reprepocess:
train_df =pd.read_csv('data/final_track2_train.txt', sep='\t', names=['uid', 'user_city', 'item_id', 'author_id', 'item_city', 'channel', 'finish', 'like', 'music_id', 'did', 'create_time', 'video_duration'])
day=[i*7//len(train_df) for i in range(len(train_df))]
train_df['day']=day
train_dev_df=train_df[train_df['day']<=4]
dev_df=train_df[train_df['day']==6]
return (train_dev_df,dev_df)
else:
train_dev_df=pd.read_pickle('data/train_dev.pkl')
dev_df=pd.read_pickle('data/dev.pkl')
return (train_dev_df,dev_df)
def parsing_item_title_features():
if os.path.exists("data/item_title_features.pkl"):
print("Reload title features")
return pickle.load(open('data/item_title_features.pkl','rb'))
print("process title features")
item_dict={}
cont=0
with open("data/track2_title.txt",'r') as f:
for line in f:
line=json.loads(line.strip())
item_id=int(line['item_id'])
if item_id not in item_dict:
item_dict[item_id]={}
keys=list(line['title_features'].keys())
values=[line['title_features'][x] for x in keys]
if len(keys)==0:
keys=['empty']
values=[1]
item_dict[item_id]['title_keys']=' '.join(keys)
item_dict[item_id]['title_values']=values
pickle.dump(item_dict,open('data/item_title_features.pkl','wb'))
return item_dict
def parsing_item_face_features():
if os.path.exists("data/item_face_features.pkl"):
print("Reload face features")
return pickle.load(open('data/item_face_features.pkl','rb'))
print("process face features")
item_dict={}
with open("data/track2_face_attrs.txt",'r') as f:
for line in f:
line=json.loads(line.strip())
item_id=int(line['item_id'])
if item_id not in item_dict:
item_dict[item_id]={}
people=len(line["face_attrs"])
if people==0:
continue
gender=[]
beauty=[]
for item in line["face_attrs"]:
gender.append(item['gender'])
beauty.append(item["beauty"])
item_dict[item_id]['gender']=gender
item_dict[item_id]['beauty']=beauty
pickle.dump(item_dict,open('data/item_face_features.pkl','wb'))
return item_dict
def create_titile_features(df,Title_dict):
print("Title features")
if 'title_cont' not in list(df):
item_ids=df['item_id'].values
keys=[]
values=[]
hit=[]
cont=[]
for idx in item_ids:
try:
keys.append(Title_dict[idx]['title_keys'])
values.append(Title_dict[idx]['title_values'])
hit.append(True)
cont.append(sum(Title_dict[idx]['title_values']))
except:
keys.append('empty')
values.append([1])
cont.append(0)
hit.append(False)
print("Hit rate",round(np.mean(hit)*100,3))
df['title_keys']=keys
df['title_values']=values
df['title_cont']=cont
return df
def create_face_features(df,face_dict):
print("Face features")
if 'gender_cont' not in list(df):
item_ids=df['item_id'].values
gender_cont=[]
gender_0=[]
gender_1=[]
beauty_max=[]
beauty_min=[]
beauty_mean=[]
beauty_var=[]
beauty_fft_var=[]
hit=[]
for idx in item_ids:
try:
beauty_min.append(min(face_dict[idx]['beauty']))
hit.append(True)
except:
beauty_min.append(-1)
hit.append(False)
print("Hit rate",round(np.mean(hit)*100,3))
df['beauty_min']=beauty_min
return df
def audio_w2v():
print("process audio features")
w2v=[]
with open("data/track2_audio_features.txt",'r') as f:
for line in f:
line=json.loads(line.strip())
item_id=int(line['item_id'])
if len(line['audio_feature_128_dim'])==128:
a=[item_id]
a.extend(line['audio_feature_128_dim'])
w2v.append(a)
out_df=pd.DataFrame(w2v)
names=['item_id']
for i in range(128):
names.append('audio_embedding_'+str(i))
out_df.columns = names
print(out_df.head())
out_df.to_pickle('data/audio_w2v.pkl')
def video_w2v():
print("process video features")
w2v=[]
with open("data/track2_video_features.txt",'r') as f:
for line in f:
line=json.loads(line.strip())
item_id=int(line['item_id'])
if len(line['video_feature_dim_128'])==128:
a=[item_id]
a.extend(line['video_feature_dim_128'])
w2v.append(a)
out_df=pd.DataFrame(w2v)
names=['item_id']
for i in range(128):
names.append('video_embedding_'+str(i))
out_df.columns = names
print(out_df.head())
out_df.to_pickle('data/video_w2v.pkl')
if __name__ == "__main__":
dev_group=split_data(True)
Title_dict=parsing_item_title_features()
Face_dict=parsing_item_face_features()
video_w2v()
audio_w2v()
for train_df,test_df,path1,path2 in [dev_group+('data/train_dev.pkl','data/dev.pkl')]:
print(train_df.shape,test_df.shape)
for df in [train_df,test_df]:
create_titile_features(df,Title_dict)
create_face_features(df,Face_dict)
print(train_df.shape,test_df.shape)
train_df.to_pickle(path1)
test_df.to_pickle(path2)
del train_df
del test_df
gc.collect()