/
copy_jsmp.py
281 lines (240 loc) · 7.58 KB
/
copy_jsmp.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# coding:utf-8
# kaggle Jane Street Market Prediction代码
# copy别人的代码:https://www.kaggle.com/c/jane-street-market-prediction/submissions
import numpy as np
import pandas as pd
import janestreet
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objs as go
import plotly.io as pio
import matplotlib.pyplot as plt
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.metrics import accuracy_score
import optuna
from optuna.samplers import TPESampler
import os
import time
# 数据探索
def data_explore():
# 读取数据
train = pd.read_csv("./train.csv", nrows = 10000)
print(train.head())
# 先画图看目标特征的分布
# .plt.figure()
plot_list = ['weight', 'resp_1', 'resp_2', 'resp_3', 'resp_4', 'resp']
fig = make_subplots(rows=3, cols=2)
traces = [
go.Histogram(
x = train[col],
nbinsx = 100,
name = col
) for col in plot_list
]
for i in range(len(traces)):
fig.append_trace(
traces[i],
(i // 2) + 1,
(i % 2) + 1
)
fig.update_layout(
title_text='Target features distributions',
height = 900,
width = 800
)
pio.write_image(fig, "./output/target_distribute.png")
# 看特征值的分布
features = train.columns
features = features[7:]
features = features[:130]
fig = make_subplots(
rows = 44,
cols = 3
)
traces = [
go.Histogram(
x = train[col],
nbinsx = 100,
name = col
) for col in features
]
for i in range(len(traces)):
fig.append_trace(
traces[i],
(i // 3) + 1,
(i % 3) + 1
)
fig.update_layout(
title_text='Train features distributions',
height = 5000
)
pio.write_image(fig, "./output/features_distribute.png")
cols = features
# 读取其它数据文件看看
features = pd.read_csv("./features.csv")
print(features)
example_test = pd.read_csv("./example_test.csv")
print(example_test)
submission = pd.read_csv("./example_sample_submission.csv")
print(submission)
# 开始建模
train = pd.read_csv("./small_train.csv")
# 先找到高度相关的特征
all_columns = []
for i in range(0, len(cols)):
for j in range(i+1, len(cols)):
if abs(train[cols[i]].corr(train[cols[j]])) > 0.95:
all_columns = all_columns + [cols[i], cols[j]]
all_columns = list(set(all_columns))
print('Number of columns:', len(all_columns))
# 画图
data = train[all_columns]
f = plt.figure(
figsize = (22, 22)
)
plt.matshow(
data.corr(),
fignum = f.number
)
plt.xticks(
range(data.shape[1]),
data.columns,
fontsize = 14,
rotation = 90
)
plt.yticks(
range(data.shape[1]),
data.columns,
fontsize = 14
)
cb = plt.colorbar()
cb.ax.tick_params(
labelsize = 14
)
plt.savefig("./output/features_corr.png")
# 目标值的相关度
data = train[['weight', 'resp_1', 'resp_2', 'resp_3', 'resp_4', 'resp']]
f = plt.figure(
figsize = (12, 12)
)
plt.matshow(
data.corr(),
fignum = f.number
)
plt.xticks(
range(data.shape[1]),
data.columns,
fontsize = 14,
rotation = 90
)
plt.yticks(
range(data.shape[1]),
data.columns,
fontsize = 14
)
cb = plt.colorbar()
cb.ax.tick_params(
labelsize = 14
)
plt.savefig("./output/targets_corr.png")
# 建模过程
def modeling():
print("开始建模")
# train = pd.read_csv("./small_train.csv")
train = pd.read_csv("./train.csv", nrows = 10000)
train = train[train['weight'] != 0]
train['action'] = ((train['weight'].values * train['resp'].values) > 0).astype('int')
X_train = train.loc[:, train.columns.str.contains('feature')]
y_train = train.loc[:, 'action']
X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, random_state=666, test_size=0.2)
del train
X_train = X_train.fillna(-999)
sampler = TPESampler(seed=666)
tm = "auto"
def create_model(trial):
max_depth = trial.suggest_int("max_depth", 2, 12)
n_estimators = trial.suggest_int("n_estimators", 2, 600)
learning_rate = trial.suggest_uniform('learning_rate', 0.0001, 0.99)
subsample = trial.suggest_uniform('subsample', 0.0001, 1.0)
colsample_bytree = trial.suggest_uniform('colsample_bytree', 0.0000001, 1)
model = XGBClassifier(
n_estimators=n_estimators,
max_depth=max_depth,
learning_rate=learning_rate,
subsample=subsample,
colsample_bytree=colsample_bytree,
random_state=666,
tree_method=tm,
silent = 1
)
return model
def objective(trial):
model = create_model(trial)
model.fit(X_train, y_train)
score = accuracy_score(
y_train,
model.predict(X_train)
)
return score
params1 = {
'max_depth': 8,
'n_estimators': 500,
'learning_rate': 0.01,
'subsample': 0.9,
'tree_method': tm,
'random_state': 666
}
params3 = {
'max_depth': 10,
'n_estimators': 500,
'learning_rate': 0.03,
'subsample': 0.9,
'colsample_bytree': 0.7,
'tree_method': tm,
'random_state': 666
}
start_time = time.time()
model1 = XGBClassifier(**params1)
model1.fit(X_train, y_train, eval_metric='auc')
model1.fit(X_train, y_train, eval_set=[(X_train, y_train), (X_test, y_test)], eval_metric='auc',verbose=False)
evals_result = model1.evals_result()
print("模型1评分")
y_true, y_pred = y_test, model1.predict(X_test)
print("Accuracy : %.4g" % metrics.accuracy_score(y_true, y_pred))
model3 = XGBClassifier(**params3)
model3.fit(X_train, y_train, eval_metric='auc')
model3.fit(X_train, y_train, eval_set=[(X_train, y_train), (X_test, y_test)], eval_metric='auc',verbose=False)
evals_result = model3.evals_result()
print("模型3评分")
y_true, y_pred = y_test, model3.predict(X_test)
print("Accuracy : %.4g" % metrics.accuracy_score(y_true, y_pred))
end_time = time.time()
print("建模时间:%.2f秒" % (end_time - start_time))
return (model1, model3)
if __name__ == "__main__":
newpath = "/home/code"
os.chdir(newpath)
# pio.orca.config.use_xvfb = True
# pio.orca.config.executable = "/opt/conda/envs/tensorflow/bin/orca"
pd.set_option('display.max_columns', None)
# data_explore()
# 真正开始干活
model1, model3 = modeling()
# 进行预测
env = janestreet.make_env()
iter_test = env.iter_test()
for (test_df, sample_prediction_df) in iter_test:
if test_df['weight'].item() > 0:
X_test = test_df.loc[:, test_df.columns.str.contains('feature')]
X_test = X_test.fillna(-999)
y_preds = model1.predict(X_test) + model3.predict(X_test)
if y_preds == 2:
y_preds = np.array([1])
else:
y_preds = np.array([0])
else:
y_preds = np.array([0])
sample_prediction_df.action = y_preds
env.predict(sample_prediction_df)