forked from wrzto/JData-algorithm-competition
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcv_params.py
More file actions
184 lines (170 loc) · 6.48 KB
/
cv_params.py
File metadata and controls
184 lines (170 loc) · 6.48 KB
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
#coding=utf-8
from xgboost import XGBClassifier
from sklearn.model_selection import GridSearchCV
import xgboost as xgb
def xgbpa(trainX, trainY):
# init
xgb1 = XGBClassifier(
learning_rate=0.3,
n_estimators=200,
max_depth=5,
min_child_weight=1,
gamma=0,
subsample=0.8,
colsample_bytree=0.8,
objective='binary:logistic',
nthread=4,
scale_pos_weight=1,
seed=6
)
# max_depth 和 min_weight 参数调优
param1 = {'max_depth': list(range(3, 7)), 'min_child_weight': list(range(1, 5, 2))}
from sklearn import svm, datasets
gsearch1 = GridSearchCV(
estimator=XGBClassifier(
learning_rate=0.3,
n_estimators=150,
max_depth=5,
min_child_weight=1,
gamma=0,
subsample=0.8,
colsample_bytree=0.8,
objective='binary:logistic',
nthread=4,
scale_pos_weight=1,
seed=6
),
param_grid=param1, scoring='roc_auc', n_jobs=4, iid=False, cv=5)
gsearch1.fit(trainX, trainY)
print(gsearch1.scorer_)
print(gsearch1.best_params_, gsearch1.best_score_)
best_max_depth = gsearch1.best_params_['max_depth']
best_min_child_weight = gsearch1.best_params_['min_child_weight']
# gamma参数调优
param2 = {'gamma': [i / 10.0 for i in range(0, 5, 2)]}
gsearch2 = GridSearchCV(
estimator=XGBClassifier(
learning_rate=0.3, # 如同学习率
n_estimators=150, # 树的个数
max_depth=best_max_depth,
min_child_weight=best_min_child_weight,
gamma=0,
subsample=0.8,
colsample_bytree=0.8,
objective='binary:logistic',
nthread=4,
scale_pos_weight=1,
seed=6
),
param_grid=param2, scoring='roc_auc', n_jobs=4, iid=False, cv=5)
gsearch2.fit(trainX, trainY)
print(gsearch2.scorer_)
print(gsearch2.best_params_, gsearch2.best_score_)
best_gamma = gsearch2.best_params_['gamma']
# 调整subsample 和 colsample_bytree参数
param3 = {'subsample': [i / 10.0 for i in range(6, 9)], 'colsample_bytree': [i / 10.0 for i in range(6, 9)]}
gsearch3 = GridSearchCV(
estimator=XGBClassifier(
learning_rate=0.3,
n_estimators=150,
max_depth=best_max_depth,
min_child_weight=best_min_child_weight,
gamma=best_gamma,
subsample=0.8,
colsample_bytree=0.8,
objective='binary:logistic',
nthread=4,
scale_pos_weight=1,
seed=6
),
param_grid=param3, scoring='roc_auc', n_jobs=4, iid=False, cv=5)
gsearch3.fit(trainX, trainY)
print(gsearch3.scorer_)
print(gsearch3.best_params_, gsearch3.best_score_)
best_subsample = gsearch3.best_params_['subsample']
best_colsample_bytree = gsearch3.best_params_['colsample_bytree']
# 正则化参数调优
param4 = {'reg_alpha': [i / 10.0 for i in range(2, 10, 2)], 'reg_lambda': [i / 10.0 for i in range(2, 10, 2)]}
gsearch4 = GridSearchCV(
estimator=XGBClassifier(
learning_rate=0.3,
n_estimators=150,
max_depth=best_max_depth,
min_child_weight=best_min_child_weight,
gamma=best_gamma,
subsample=best_subsample,
colsample_bytree=best_colsample_bytree,
objective='binary:logistic',
nthread=4,
scale_pos_weight=1,
seed=6
),
param_grid=param4, scoring='roc_auc', n_jobs=4, iid=False, cv=5)
gsearch4.fit(trainX, trainY)
print(gsearch4.scorer_)
print(gsearch4.best_params_, gsearch4.best_score_)
best_reg_alpha = gsearch4.best_params_['reg_alpha']
best_reg_lambda = gsearch4.best_params_['reg_lambda']
param5= {'scale_pos_weight': [i for i in [0.5, 1, 2]]}
gsearch5 = GridSearchCV(
estimator = XGBClassifier(
learning_rate = 0.3,
n_estimators = 150,
max_depth = best_max_depth,
min_child_weight = best_min_child_weight,
gamma = best_gamma,
subsample = best_subsample,
colsample_bytree = best_colsample_bytree,
reg_alpha = best_reg_alpha,
reg_lambda = best_reg_lambda,
objective = 'binary:logistic',
nthread = 4,
scale_pos_weight = 1,
seed = 6
),
param_grid = param5, scoring = 'roc_auc', n_jobs = 4, iid = False, cv = 5)
gsearch5.fit(trainX, trainY)
print(gsearch5.best_params_, gsearch5.best_score_)
best_scale_pos_weight = gsearch5.best_params_['scale_pos_weight']
# 降低学习速率,数的数量
param6 = [{'learning_rate': [0.01, 0.05, 0.1, 0.2], 'n_estimators': [800, 1000, 1200]}]
gsearch6 = GridSearchCV(
estimator=XGBClassifier(
learning_rate=0.3,
n_estimators=150,
max_depth=best_max_depth,
min_child_weight=best_min_child_weight,
gamma=best_gamma,
subsample=best_subsample,
colsample_bytree=best_colsample_bytree,
reg_alpha=best_reg_alpha,
reg_lambda = best_reg_lambda,
objective = 'binary:logistic',
nthread = 4,
scale_pos_weight = best_scale_pos_weight,
seed = 6
),
param_grid = param6, scoring = 'roc_auc', n_jobs = 4, iid = False, cv = 5)
gsearch6.fit(trainX, trainY)
print(gsearch6.scorer_)
print(gsearch6.best_params_, gsearch6.best_score_)
best_learning_rate = gsearch6.best_params_['learning_rate']
best_n_estimators = gsearch6.best_params_['n_estimators']
print(gsearch1.best_params_, gsearch1.best_score_)
print(gsearch2.best_params_, gsearch2.best_score_)
print(gsearch3.best_params_, gsearch3.best_score_)
print(gsearch4.best_params_, gsearch4.best_score_)
print(gsearch5.best_params_, gsearch5.best_score_)
print(gsearch6.best_params_, gsearch6.best_score_)
if __name__ == '__main__':
# user_model cv
print('--------------user model---------------')
Train = pd.read_csv('./train_user_model_feat.csv')
xgbpa(Train.drop(['user_id', 'label'], axis=1), Train.label)
del Train
#sku_model cv
print('--------------sku model---------------')
Drop_cols = ['user_id', 'sku_id', 'cate', 'brand', 'label']
Train = pd.read_csv("./train_sku_feat.csv")
xgbpa(Train.drop(Drop_cols, axis=1), Train.label)
del Train