forked from lkjie/txad20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
149 lines (125 loc) · 5.53 KB
/
preprocess.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
#!/usr/bin/env python
# coding=utf-8
import pandas as pd
import numpy as np
import json
import csv
import os
import pickle
import re
import sys
import logging
import lightgbm as lgb
from collections import Counter, defaultdict
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_score, recall_score, roc_auc_score
from .utils import base_train
np.set_printoptions(formatter={'float': lambda x: "{0:0.3f}".format(x)})
logging.getLogger().setLevel(logging.INFO)
'''
统计特征:
user纬度:点击广告总次数,creative_id个数,
'''
def agg_features(df_click_log, groupby_cols, stat_col, aggfunc):
if type(groupby_cols) == str:
groupby_cols = [groupby_cols]
data = df_click_log[groupby_cols + [stat_col]]
if aggfunc == "size":
tmp = pd.DataFrame(data.groupby(groupby_cols).size()).reset_index()
elif aggfunc == "count":
tmp = pd.DataFrame(data.groupby(groupby_cols)[stat_col].count()).reset_index()
elif aggfunc == "mean":
tmp = pd.DataFrame(data.groupby(groupby_cols)[stat_col].mean()).reset_index()
elif aggfunc == "unique":
tmp = pd.DataFrame(data.groupby(groupby_cols)[stat_col].nunique()).reset_index()
elif aggfunc == "max":
tmp = pd.DataFrame(data.groupby(groupby_cols)[stat_col].max()).reset_index()
elif aggfunc == "min":
tmp = pd.DataFrame(data.groupby(groupby_cols)[stat_col].min()).reset_index()
elif aggfunc == "sum":
tmp = pd.DataFrame(data.groupby(groupby_cols)[stat_col].sum()).reset_index()
elif aggfunc == "std":
tmp = pd.DataFrame(data.groupby(groupby_cols)[stat_col].std()).reset_index()
elif aggfunc == "median":
tmp = pd.DataFrame(data.groupby(groupby_cols)[stat_col].median()).reset_index()
elif aggfunc == "skew":
tmp = pd.DataFrame(data.groupby(groupby_cols)[stat_col].skew()).reset_index()
elif aggfunc == "unique_mean":
group = data.groupby(groupby_cols)
group = group.apply(lambda x: np.mean(list(Counter(list(x[stat_col])).values())))
tmp = pd.DataFrame(group.reset_index())
elif aggfunc == "unique_var":
group = data.groupby(groupby_cols)
group = group.apply(lambda x: np.var(list(Counter(list(x[stat_col])).values())))
tmp = pd.DataFrame(group.reset_index())
else:
raise Exception("aggfunc error")
feat_name = '_'.join(groupby_cols) + "_" + stat_col + "_" + aggfunc
tmp.columns = groupby_cols + [feat_name]
print(feat_name)
return tmp
# try:
# del df_click_log[feat_name]
# except:
# pass
# df_click_log = df_click_log.merge(tmp, how='left', on=groupby_cols)
# return df_click_log
def get_features(df_click_log):
paras = [
(['user_id'], 'creative_id', 'unique'),
(['user_id'], 'creative_id', 'count'),
(['user_id'], 'ad_id', 'unique'),
(['user_id'], 'ad_id', 'count'),
(['user_id'], 'product_id', 'unique'),
(['user_id'], 'product_id', 'count'),
(['user_id'], 'product_category', 'unique'),
(['user_id'], 'product_category', 'count'),
(['user_id'], 'advertiser_id', 'unique'),
(['user_id'], 'advertiser_id', 'count'),
(['user_id'], 'industry', 'unique'),
(['user_id'], 'industry', 'count')
]
df_tmp = pd.DataFrame()
for groupby_cols, stat_col, aggfunc in paras:
tmp = agg_features(df_click_log, groupby_cols, stat_col, aggfunc)
df_tmp = df_tmp.merge(tmp, how='left', on='user_id') if not df_tmp.empty else tmp
return df_tmp
def main(args):
txdir = "/Users/l/txad20/"
df_ad = pd.read_csv(txdir + "train_preliminary/ad.csv")
df_click_log = pd.read_csv(txdir + "train_preliminary/click_log.csv")
df_user = pd.read_csv(txdir + "train_preliminary/user.csv")
df_ad.loc[df_ad['product_id'] == '\\N', 'product_id'] = 0
df_ad.loc[df_ad['industry'] == '\\N', 'industry'] = 0
df_user['gender'] = df_user['gender'] - 1
df_train, df_dev = train_test_split(df_user, test_size=0.2, random_state=2020)
df_feat = get_features(df_click_log)
df_train = df_train.merge(df_feat, how='left', on='user_id')
df_dev = df_dev.merge(df_feat, how='left', on='user_id')
y_train = df_train['gender']
x_train = df_train.drop(['gender', 'age', 'user_id'], axis=1)
y_dev = df_dev['gender']
x_dev = df_dev.drop(['gender', 'age', 'user_id'], axis=1)
gbm_gender = base_train(x_train, y_train, x_dev, y_dev, job='classification')
y_train = df_train['age']
x_train = df_train.drop(['gender', 'age', 'user_id'], axis=1)
y_dev = df_dev['age']
x_dev = df_dev.drop(['gender', 'age', 'user_id'], axis=1)
gbm_age = base_train(x_train, y_train, x_dev, y_dev, job='regression')
# 预测
df_ad_test = pd.read_csv(txdir + "test/ad.csv")
df_click_log_test = pd.read_csv(txdir + "test/click_log.csv")
df_ad_test.loc[df_ad_test['product_id'] == '\\N', 'product_id'] = 0
df_ad_test.loc[df_ad_test['industry'] == '\\N', 'industry'] = 0
df_click_log_test = df_click_log_test.merge(df_ad_test, how='left')
df_feat_test = get_features(df_click_log_test)
df_res = df_feat_test[['user_id']]
df_test = df_feat_test.drop(['user_id'], axis=1)
df_res['predicted_age'] = gbm_age.predict(df_test)
df_res['predicted_gender'] = gbm_gender.predict(df_test)
df_res.loc[df_res['predicted_gender'] >= 0.5, 'predicted_gender'] = 2
df_res.loc[df_res['predicted_gender'] < 0.5, 'predicted_gender'] = 1
df_res.to_csv("submission.csv", index=False)
if __name__ == '__main__':
args = None
main(args)