-
Notifications
You must be signed in to change notification settings - Fork 1
/
商品商户交互特征.py
161 lines (140 loc) · 8.88 KB
/
商品商户交互特征.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
import pandas as pd
import xgboost as xgb
pd.options.mode.chained_assignment = None
import numpy as np
import seaborn as sns
import time
from datetime import date
from matplotlib import pyplot as plt
from sklearn.preprocessing import MinMaxScaler
# 从训练集一中获取商品店铺相关的特征
train1_f = pd.read_csv('data/train1_f.csv')
item = pd.read_csv('data/item_feature1.csv')
shop = pd.read_csv('data/shop_feature1.csv')
item = item.drop_duplicates()
shop = shop.drop_duplicates()
train1_f = pd.merge(train1_f,item[['item_buy','item_id']],on='item_id',how='left')
train1_f = pd.merge(train1_f,shop[['shop_click_buy_total','shop_id']],on='shop_id',how='left')
commodity_shop = train1_f
# 该商品在该店铺点击次数
t = commodity_shop[['item_id','shop_id']]
t['item_shop_click_total'] = 1
t = t.groupby(['item_id','shop_id']).agg(sum).reset_index()
commodity_shop = pd.merge(commodity_shop,t,on=['item_id','shop_id'],how='left')
# 该商品在该店铺购买次数
t = commodity_shop[['item_id','shop_id','is_trade']]
t = t.groupby(['item_id','shop_id']).agg(sum).reset_index()
t.rename(columns={'is_trade':'item_shop_click_buy_total'},inplace=True)
commodity_shop = pd.merge(commodity_shop,t,on=['item_id','shop_id'],how='left')
# 该商品在该店铺购买率
commodity_shop['item_shop_click_buy_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['item_shop_click_total']
# 该商品在该店铺售卖数量/该商品总售卖量
commodity_shop['item_shop_sale_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['item_buy']
# 该商品在该店铺售卖数量/该商户总售卖量
commodity_shop['shop_item_sale_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['shop_click_buy_total']
commodity_shop[['item_shop_click_buy_rate','item_shop_click_buy_total','item_shop_click_total','item_shop_sale_rate','shop_item_sale_rate','shop_id','item_id']].to_csv('data/shop_item_feature1.csv',index=None)
# 从训练集二中获取商品店铺相关的特征
train1_f = pd.read_csv('data/train2_f.csv')
item = pd.read_csv('data/item_feature2.csv')
shop = pd.read_csv('data/shop_feature2.csv')
item = item.drop_duplicates()
shop = shop.drop_duplicates()
train1_f = pd.merge(train1_f,item[['item_buy','item_id']],on='item_id',how='left')
train1_f = pd.merge(train1_f,shop[['shop_click_buy_total','shop_id']],on='shop_id',how='left')
commodity_shop = train1_f
# 该商品在该店铺点击次数
t = commodity_shop[['item_id','shop_id']]
t['item_shop_click_total'] = 1
t = t.groupby(['item_id','shop_id']).agg(sum).reset_index()
commodity_shop = pd.merge(commodity_shop,t,on=['item_id','shop_id'],how='left')
# 该商品在该店铺购买次数
t = commodity_shop[['item_id','shop_id','is_trade']]
t = t.groupby(['item_id','shop_id']).agg(sum).reset_index()
t.rename(columns={'is_trade':'item_shop_click_buy_total'},inplace=True)
commodity_shop = pd.merge(commodity_shop,t,on=['item_id','shop_id'],how='left')
# 该商品在该店铺购买率
commodity_shop['item_shop_click_buy_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['item_shop_click_total']
# 该商品在该店铺售卖数量/该商品总售卖量
commodity_shop['item_shop_sale_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['item_buy']
# 该商品在该店铺售卖数量/该商户总售卖量
commodity_shop['shop_item_sale_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['shop_click_buy_total']
commodity_shop[['item_shop_click_buy_rate','item_shop_click_buy_total','item_shop_click_total','item_shop_sale_rate','shop_item_sale_rate','shop_id','item_id']].to_csv('data/shop_item_feature2.csv',index=None)
# 从训练集三中获取商品店铺相关的特征
train1_f = pd.read_csv('data/train3_f.csv')
item = pd.read_csv('data/item_feature3.csv')
shop = pd.read_csv('data/shop_feature3.csv')
item = item.drop_duplicates()
shop = shop.drop_duplicates()
train1_f = pd.merge(train1_f,item[['item_buy','item_id']],on='item_id',how='left')
train1_f = pd.merge(train1_f,shop[['shop_click_buy_total','shop_id']],on='shop_id',how='left')
commodity_shop = train1_f
# 该商品在该店铺点击次数
t = commodity_shop[['item_id','shop_id']]
t['item_shop_click_total'] = 1
t = t.groupby(['item_id','shop_id']).agg(sum).reset_index()
commodity_shop = pd.merge(commodity_shop,t,on=['item_id','shop_id'],how='left')
# 该商品在该店铺购买次数
t = commodity_shop[['item_id','shop_id','is_trade']]
t = t.groupby(['item_id','shop_id']).agg(sum).reset_index()
t.rename(columns={'is_trade':'item_shop_click_buy_total'},inplace=True)
commodity_shop = pd.merge(commodity_shop,t,on=['item_id','shop_id'],how='left')
# 该商品在该店铺购买率
commodity_shop['item_shop_click_buy_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['item_shop_click_total']
# 该商品在该店铺售卖数量/该商品总售卖量
commodity_shop['item_shop_sale_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['item_buy']
# 该商品在该店铺售卖数量/该商户总售卖量
commodity_shop['shop_item_sale_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['shop_click_buy_total']
commodity_shop[['item_shop_click_buy_rate','item_shop_click_buy_total','item_shop_click_total','item_shop_sale_rate','shop_item_sale_rate','shop_id','item_id']].to_csv('data/shop_item_feature3.csv',index=None)
# 从训练集4中获取商品店铺相关的特征
train1_f = pd.read_csv('data/train4_f.csv')
item = pd.read_csv('data/item_feature4.csv')
shop = pd.read_csv('data/shop_feature4.csv')
item = item.drop_duplicates()
shop = shop.drop_duplicates()
train1_f = pd.merge(train1_f,item[['item_buy','item_id']],on='item_id',how='left')
train1_f = pd.merge(train1_f,shop[['shop_click_buy_total','shop_id']],on='shop_id',how='left')
commodity_shop = train1_f
# 该商品在该店铺点击次数
t = commodity_shop[['item_id','shop_id']]
t['item_shop_click_total'] = 1
t = t.groupby(['item_id','shop_id']).agg(sum).reset_index()
commodity_shop = pd.merge(commodity_shop,t,on=['item_id','shop_id'],how='left')
# 该商品在该店铺购买次数
t = commodity_shop[['item_id','shop_id','is_trade']]
t = t.groupby(['item_id','shop_id']).agg(sum).reset_index()
t.rename(columns={'is_trade':'item_shop_click_buy_total'},inplace=True)
commodity_shop = pd.merge(commodity_shop,t,on=['item_id','shop_id'],how='left')
# 该商品在该店铺购买率
commodity_shop['item_shop_click_buy_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['item_shop_click_total']
# 该商品在该店铺售卖数量/该商品总售卖量
commodity_shop['item_shop_sale_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['item_buy']
# 该商品在该店铺售卖数量/该商户总售卖量
commodity_shop['shop_item_sale_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['shop_click_buy_total']
commodity_shop[['item_shop_click_buy_rate','item_shop_click_buy_total','item_shop_click_total','item_shop_sale_rate','shop_item_sale_rate','shop_id','item_id']].to_csv('data/shop_item_feature4.csv',index=None)
# # 从训练集5中获取商品店铺相关的特征
# train1_f = pd.read_csv('data/train5_f.csv')
# item = pd.read_csv('data/item_feature5.csv')
# shop = pd.read_csv('data/shop_feature5.csv')
# item = item.drop_duplicates()
# shop = shop.drop_duplicates()
# train1_f = pd.merge(train1_f,item[['item_buy','item_id']],on='item_id',how='left')
# train1_f = pd.merge(train1_f,shop[['shop_click_buy_total','shop_id']],on='shop_id',how='left')
# commodity_shop = train1_f
# # 该商品在该店铺点击次数
# t = commodity_shop[['item_id','shop_id']]
# t['item_shop_click_total'] = 1
# t = t.groupby(['item_id','shop_id']).agg(sum).reset_index()
# commodity_shop = pd.merge(commodity_shop,t,on=['item_id','shop_id'],how='left')
# # 该商品在该店铺购买次数
# t = commodity_shop[['item_id','shop_id','is_trade']]
# t = t.groupby(['item_id','shop_id']).agg(sum).reset_index()
# t.rename(columns={'is_trade':'item_shop_click_buy_total'},inplace=True)
# commodity_shop = pd.merge(commodity_shop,t,on=['item_id','shop_id'],how='left')
# # 该商品在该店铺购买率
# commodity_shop['item_shop_click_buy_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['item_shop_click_total']
# # 该商品在该店铺售卖数量/该商品总售卖量
# commodity_shop['item_shop_sale_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['item_buy']
# # 该商品在该店铺售卖数量/该商户总售卖量
# commodity_shop['shop_item_sale_rate'] = commodity_shop['item_shop_click_buy_total']/commodity_shop['shop_click_buy_total']
#
# commodity_shop[['item_shop_click_buy_rate','item_shop_click_buy_total','item_shop_click_total','item_shop_sale_rate','shop_item_sale_rate','shop_id','item_id']].to_csv('data/shop_item_feature5.csv',index=None)