PriceForecast/maincanshu.py

123 lines
5.6 KiB
Python
Raw Permalink Normal View History

2024-11-01 16:38:21 +08:00
# 读取配置
from config_jingbo import *
from lib.tools import *
from lib.dataread import *
from models.nerulforcastmodels import ex_Model,model_losss,brent_export_pdf
from models.lstmmodels import ex_Lstm_M,ex_Lstm
from models.grumodels import ex_GRU
import glob
import torch
torch.set_float32_matmul_precision("high")
if __name__ == '__main__':
signature = BinanceAPI(APPID, SECRET)
# 遍历参数训练模型
input_size_list = [14]
horizon_list = [7]
train_steps_list = [500,1000,1500,2000]
k_list = [10,18,25,50,100]
end_time_list = ['2024-07-03']
is_debug = False
is_fivemodels = False # 是否使用之前保存的最佳的5个模型
delweekenday = True
# 组合上面三个参数
for i in range(len(input_size_list)):
for h in range(len(horizon_list)):
for j in range(len(train_steps_list)):
for k in range(len(k_list)):
for end_time in end_time_list:
input_size = input_size_list[i]
horizons = horizon_list[h]
train_steps = train_steps_list[j]
K = k_list[k]
settings = f'{input_size}-{horizon_list[h]}-{train_steps}-{K}-{data_set}-{end_time}-{y}'
logger.info(f'当前配置:{settings}')
# 获取数据
if is_eta:
etadata = EtaReader(signature=signature,
classifylisturl = classifylisturl,
classifyidlisturl=classifyidlisturl,
edbcodedataurl=edbcodedataurl,
edbcodelist=edbcodelist
)
df = etadata.get_eta_api_data(data_set=data_set,dataset=dataset) # 原始数据,未处理
else:
filename = os.path.join(dataset,data_set)
logger.info(f'未启用Eta数据,将读取本地数据{filename}')
df = pd.read_excel(filename,sheet_name='指标数据')
# 数据处理
df = datachuli(df=df,dataset=dataset,end_time=end_time,y=y,delweekenday=delweekenday)
if is_timefurture:
df = addtimecharacteristics(df=df,dataset=dataset)
# 更改预测列名称
df.rename(columns={y:'y'},inplace=True)
logger.info(f"开始训练模型...")
row,col = df.shape
logger.info(f'当前配置:{settings}')
# 获取日期时间 计算今天日期 %Y-%m-%d-%H-%M-%S
from datetime import datetime
now = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
ex_Model(df,
horizon=horizon,
input_size=input_size,
train_steps=train_steps,
val_check_steps=val_check_steps,
early_stop_patience_steps=early_stop_patience_steps,
is_debug=is_debug,
dataset=dataset,
is_train=is_train,
is_fivemodels=is_fivemodels,
val_size=val_size,
test_size=test_size,
settings=settings,
now=now
)
# 模型评估
model_results3 = model_losss(df,dataset=dataset,horizon=horizon)
# 模型报告
reportname = f'{settings}--{now}-预测报告.pdf' # 报告文件名
reportname = reportname.replace(':', '-') # 替换冒号
title = f'{settings}--{now}-预测报告' # 报告标题
brent_export_pdf(dataset=dataset,num_models = 5 if is_fivemodels else 22,time=end_time,
reportname=reportname),
# 发送邮件
m = SendMail(
username=username,
passwd=passwd,
recv=recv,
title=title,
content=content,
file=max(glob.glob(os.path.join(dataset,reportname)), key=os.path.getctime),
ssl=ssl,
)
# m.send_mail()
# # LSTM 单变量模型
# ex_Lstm(df,input_seq_len=input_size,output_seq_len=horizon,is_debug=is_debug,dataset=dataset)
# # lstm 多变量模型
# ex_Lstm_M(df,n_days=input_size,out_days=horizon,is_debug=is_debug,datasetpath=dataset)
# # GRU 模型
# # ex_GRU(df)
# 发送邮件
# m = SendMail(
# username=username,
# passwd=passwd,
# recv=recv,
# title=title,
# content=content,
# file=max(glob.glob(os.path.join(dataset,'*.pdf')), key=os.path.getctime),
# ssl=ssl,
# )
# m.send_mail()