forked from FinHackCN/xlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
executable file
·148 lines (111 loc) · 4.63 KB
/
model.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
import lightgbm as lgb
#from datetime import datetime
def datasplit(df,train_end='2019-01-01',valid_end='2020-01-01'):
#df=df.dropna()
df_train=df[df['date']<train_end]
df_valid=df[df['date']>train_end]
df_valid=df_valid[df_valid['date']<valid_end]
df_pred=df[df['date']>=valid_end]
df_train.drop_duplicates(subset=['symbol','date'],keep='first',inplace=True)
df_valid.drop_duplicates(subset=['symbol','date'],keep='first',inplace=True)
df_pred.drop_duplicates(subset=['symbol','date'],keep='first',inplace=True)
return df_train,df_valid,df_pred
def data_wash(df_train,df_valid,label='label'):
df_train=df_train.drop('date', axis=1)
df_valid=df_valid.drop('date', axis=1)
df_train=df_train.drop('symbol', axis=1)
df_valid=df_valid.drop('symbol', axis=1)
df_valid= df_valid.drop('open', axis=1)
df_valid= df_valid.drop('close', axis=1)
df_valid= df_valid.drop('high', axis=1)
df_valid= df_valid.drop('low', axis=1)
df_valid= df_valid.drop('volume', axis=1)
df_valid= df_valid.drop('amount', axis=1)
df_train= df_train.drop('open', axis=1)
df_train= df_train.drop('close', axis=1)
df_train= df_train.drop('high', axis=1)
df_train= df_train.drop('low', axis=1)
df_train= df_train.drop('volume', axis=1)
df_train= df_train.drop('amount', axis=1)
y_train=df_train[label]
x_train=df_train.drop(label, axis=1)
y_valid=df_valid[label]
x_valid=df_valid.drop(label, axis=1)
lgb_train = lgb.Dataset(x_train, y_train)
lgb_valid = lgb.Dataset(x_valid, y_valid)
return lgb_train,lgb_valid
def lgbtrain(df_train,df_valid,label='label'):
lgb_train,lgb_valid=data_wash(df_train,df_valid,'label')
# 参数设置
params = {
'boosting_type': 'gbdt',
'max_depth': 5,
'num_leaves': 32, # 叶子节点数
'learning_rate': 0.2, # 学习速率
'feature_fraction': 0.7, # 建树的特征选择比例colsample_bytree
'bagging_fraction': 0.7, # 建树的样本采样比例subsample
'bagging_freq': 5, # k 意味着每 k 次迭代执行bagging
'verbose': -1, # <0 显示致命的, =0 显示错误 (警告), >0 显示信息
'lambda_l1':300,
'lambda_l2':300,
}
print('Starting training...')
# 模型训练
gbm = lgb.train(params,
lgb_train,
num_boost_round=1000,
valid_sets=lgb_valid,
early_stopping_rounds=5,
# fobj=custom_obj,
# feval=custom_eval,
)
print('Saving model...')
# 模型保存
gbm.save_model('model.txt')
# 模型加载
def lgbtrain2(df_train,df_valid,label='label'):
lgb_train,lgb_valid=data_wash(df_train,df_valid,'label')
# 参数设置
params = {
'objective':'huber',
'boosting_type': 'gbdt',
'max_depth': 5,
'num_leaves': 32, # 叶子节点数
'learning_rate': 0.2, # 学习速率
'feature_fraction': 0.7, # 建树的特征选择比例colsample_bytree
'bagging_fraction': 0.7, # 建树的样本采样比例subsample
'bagging_freq': 5, # k 意味着每 k 次迭代执行bagging
'verbose': -1, # <0 显示致命的, =0 显示错误 (警告), >0 显示信息
'lambda_l1':300,
'lambda_l2':300,
}
print('Starting training...')
# 模型训练
gbm = lgb.train(params,
lgb_train,
num_boost_round=1000,
valid_sets=lgb_valid,
early_stopping_rounds=5,
# fobj=custom_obj,
# feval=custom_eval,
)
print('Saving model...')
# 模型保存
gbm.save_model('model.txt')
# 模型加载
def lgbpred(df_pred,label='label'):
# df_pred=df_pred.drop('symbol', axis=1)
gbm = lgb.Booster(model_file='model.txt')
df_pred=df_pred.drop(label, axis=1)
x_pred= df_pred.drop('date', axis=1)
x_pred= x_pred.drop('symbol', axis=1)
x_pred= x_pred.drop('open', axis=1)
x_pred= x_pred.drop('close', axis=1)
x_pred= x_pred.drop('high', axis=1)
x_pred= x_pred.drop('low', axis=1)
x_pred= x_pred.drop('volume', axis=1)
x_pred= x_pred.drop('amount', axis=1)
# 模型预测
y_pred = gbm.predict(x_pred, num_iteration=gbm.best_iteration)
df_pred['pred']=y_pred
return df_pred