-
Notifications
You must be signed in to change notification settings - Fork 2
/
add_feature_save.py
140 lines (94 loc) · 3.83 KB
/
add_feature_save.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
# -*- coding: utf-8 -*-
"""
@author: Gene Baratheon
@Email : [email protected]
@Main : XGBOOST单模型:全局统计
"""
import pandas as pd
import numpy as np
import time
from sklearn.metrics import log_loss
from collections import Counter
import extract_feature_whole as ext_feat_wh
import lightgbm as lgb
import warnings
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
from scipy import sparse
from sklearn.linear_model import LogisticRegression
import xgboost as xgb
warnings.filterwarnings("ignore")
import gc
def change_dtypes(data):
all_data_int = data.select_dtypes(include = ['int64'])
all_data_convert_int = all_data_int.apply(pd.to_numeric, downcast = 'unsigned')
data[all_data_convert_int.columns] = all_data_convert_int
del all_data_int ; del all_data_convert_int
gc.collect()
return data
#保存添加属性
def save_add_feature(data,cols,path):
print('save add feature: ',cols)
t0 = data[cols]
print('save add shape: ',t0.shape)
t0.to_csv(path,index=False)
del t0 ; gc.collect()
if __name__ == "__main__":
start_time=time.time()
print('----------------------------读取基本特征-----------------------------------------------------------')
#1. 读取特征
cols = ['instance_id','user_id','item_id','shop_id','context_timestamp','day','item_category_list','category_1']
data = pd.read_csv('../feature/gene/basic_all.csv' , usecols = cols)
print('data shape: ',data.shape) # 10951924
#2. 暂无
print('----------------------------增加一些特征------------------------------------------------------------')
#3. 添加特征 add_0506
add_cols=['instance_id']
data,add_cols = ext_feat_wh.add_0506(data,add_cols)
path='../feature/gene/add_0506.csv'
save_add_feature(data,add_cols,path)
#4. 添加特征 add_0507
add_cols=['instance_id']
data,add_cols = ext_feat_wh.add_0507(data,add_cols)
path='../feature/gene/add_0507.csv'
save_add_feature(data,add_cols,path)
#5. 添加特征 add_0508
add_cols=['instance_id']
data,add_cols = ext_feat_wh.add_0508(data,add_cols)
path='../feature/gene/add_0508.csv'
save_add_feature(data,add_cols,path)
#6. 添加特征 add_0509
add_cols=['instance_id']
data,add_cols = ext_feat_wh.add_0509(data,add_cols)
path='../feature/gene/add_0509.csv'
save_add_feature(data,add_cols,path)
#7. 添加特征 add_0511
add_cols=['instance_id']
data,add_cols = ext_feat_wh.add_0511(data,add_cols)
path='../feature/gene/add_0511.csv'
save_add_feature(data,add_cols,path)
#8. 添加特征 add_0512
add_cols=['instance_id']
data,add_cols = ext_feat_wh.add_0512(data,add_cols)
path='../feature/gene/add_0512.csv'
save_add_feature(data,add_cols,path)
#9. 添加特征 add_0513
add_cols=['instance_id']
data,add_cols = ext_feat_wh.add_0513(data,add_cols)
path='../feature/gene/add_0513.csv'
save_add_feature(data,add_cols,path)
#10. 添加特征 add_0514
add_cols=['instance_id']
data,add_cols = ext_feat_wh.add_0514(data,add_cols)
path='../feature/gene/add_0514.csv'
save_add_feature(data,add_cols,path)
#11. 添加特征 add_0515
add_cols=['instance_id']
data = data[cols]
data,add_cols = ext_feat_wh.add_0515(data,add_cols)
path='../feature/gene/add_0515.csv'
save_add_feature(data,add_cols,path)
print('data shape after add: ',data.shape)
del data ; gc.collect()
#7. 计算算法时间
end_time=time.time()
print('all time is: %.1f'%(end_time-start_time),' 秒')