聚烯烃周度调试

This commit is contained in:
workpc 2025-05-27 10:36:37 +08:00
parent 46adadb042
commit e165f556e7

View File

@ -301,42 +301,42 @@ def predict_main():
# 判断当前日期是不是周一 # 判断当前日期是不是周一
is_weekday = datetime.datetime.now().weekday() == 0 is_weekday = datetime.datetime.now().weekday() == 0
if is_weekday: # if is_weekday:
logger.info('今天是周一,更新预测模型') # logger.info('今天是周一,更新预测模型')
# 计算最近60天预测残差最低的模型名称 # # 计算最近60天预测残差最低的模型名称
model_results = sqlitedb.select_data( # model_results = sqlitedb.select_data(
'trueandpredict', order_by="ds DESC", limit="60") # 'trueandpredict', order_by="ds DESC", limit="60")
# 删除空值率为90%以上的列 # # 删除空值率为90%以上的列
if len(model_results) > 10: # if len(model_results) > 10:
model_results = model_results.dropna( # model_results = model_results.dropna(
thresh=len(model_results)*0.1, axis=1) # thresh=len(model_results)*0.1, axis=1)
# 删除空行 # # 删除空行
model_results = model_results.dropna() # model_results = model_results.dropna()
modelnames = model_results.columns.to_list()[2:-2] # modelnames = model_results.columns.to_list()[2:-2]
for col in model_results[modelnames].select_dtypes(include=['object']).columns: # for col in model_results[modelnames].select_dtypes(include=['object']).columns:
model_results[col] = model_results[col].astype(np.float32) # model_results[col] = model_results[col].astype(np.float32)
# 计算每个预测值与真实值之间的偏差率 # # 计算每个预测值与真实值之间的偏差率
for model in modelnames: # for model in modelnames:
model_results[f'{model}_abs_error_rate'] = abs( # model_results[f'{model}_abs_error_rate'] = abs(
model_results['y'] - model_results[model]) / model_results['y'] # model_results['y'] - model_results[model]) / model_results['y']
# 获取每行对应的最小偏差率值 # # 获取每行对应的最小偏差率值
min_abs_error_rate_values = model_results.apply( # min_abs_error_rate_values = model_results.apply(
lambda row: row[[f'{model}_abs_error_rate' for model in modelnames]].min(), axis=1) # lambda row: row[[f'{model}_abs_error_rate' for model in modelnames]].min(), axis=1)
# 获取每行对应的最小偏差率值对应的列名 # # 获取每行对应的最小偏差率值对应的列名
min_abs_error_rate_column_name = model_results.apply( # min_abs_error_rate_column_name = model_results.apply(
lambda row: row[[f'{model}_abs_error_rate' for model in modelnames]].idxmin(), axis=1) # lambda row: row[[f'{model}_abs_error_rate' for model in modelnames]].idxmin(), axis=1)
# 将列名索引转换为列名 # # 将列名索引转换为列名
min_abs_error_rate_column_name = min_abs_error_rate_column_name.map( # min_abs_error_rate_column_name = min_abs_error_rate_column_name.map(
lambda x: x.split('_')[0]) # lambda x: x.split('_')[0])
# 取出现次数最多的模型名称 # # 取出现次数最多的模型名称
most_common_model = min_abs_error_rate_column_name.value_counts().idxmax() # most_common_model = min_abs_error_rate_column_name.value_counts().idxmax()
logger.info(f"最近60天预测残差最低的模型名称{most_common_model}") # logger.info(f"最近60天预测残差最低的模型名称{most_common_model}")
# 保存结果到数据库 # # 保存结果到数据库
if not sqlitedb.check_table_exists('most_model'): # if not sqlitedb.check_table_exists('most_model'):
sqlitedb.create_table( # sqlitedb.create_table(
'most_model', columns="ds datetime, most_common_model TEXT") # 'most_model', columns="ds datetime, most_common_model TEXT")
sqlitedb.insert_data('most_model', (datetime.datetime.now().strftime( # sqlitedb.insert_data('most_model', (datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S'), most_common_model,), columns=('ds', 'most_common_model',)) # '%Y-%m-%d %H:%M:%S'), most_common_model,), columns=('ds', 'most_common_model',))
if is_corr: if is_corr:
df = corr_feature(df=df) df = corr_feature(df=df)
@ -375,17 +375,17 @@ def predict_main():
logger.info('训练数据绘图end') logger.info('训练数据绘图end')
# # 模型报告 # # 模型报告
logger.info('制作报告ing') # logger.info('制作报告ing')
title = f'{settings}--{end_time}-预测报告' # 报告标题 # title = f'{settings}--{end_time}-预测报告' # 报告标题
reportname = f'聚烯烃PP大模型周度预测--{end_time}.pdf' # 报告文件名 # reportname = f'聚烯烃PP大模型周度预测--{end_time}.pdf' # 报告文件名
reportname = reportname.replace(':', '-') # 替换冒号 # reportname = reportname.replace(':', '-') # 替换冒号
pp_export_pdf(dataset=dataset, num_models=5 if is_fivemodels else 22, time=end_time, # pp_export_pdf(dataset=dataset, num_models=5 if is_fivemodels else 22, time=end_time,
reportname=reportname, sqlitedb=sqlitedb), # reportname=reportname, sqlitedb=sqlitedb),
logger.info('制作报告end') # logger.info('制作报告end')
logger.info('模型训练完成') # logger.info('模型训练完成')
push_market_value() # push_market_value()
# # LSTM 单变量模型 # # LSTM 单变量模型
# ex_Lstm(df,input_seq_len=input_size,output_seq_len=horizon,is_debug=is_debug,dataset=dataset) # ex_Lstm(df,input_seq_len=input_size,output_seq_len=horizon,is_debug=is_debug,dataset=dataset)
@ -412,12 +412,11 @@ def predict_main():
if __name__ == '__main__': if __name__ == '__main__':
# global end_time # global end_time
# 遍历2024-11-25 到 2024-12-3 之间的工作日日期 # 遍历2024-11-25 到 2024-12-3 之间的工作日日期
# for i_time in pd.date_range('2022-1-1', '2025-3-26', freq='M'): for i_time in pd.date_range('2025-3-1', '2025-5-26', freq='W'):
# try: try:
# global_config['end_time'] = i_time.strftime('%Y-%m-%d') global_config['end_time'] = i_time.strftime('%Y-%m-%d')
# predict_main() predict_main()
# except Exception as e: except Exception as e:
# logger.info(f'预测失败:{e}') logger.info(f'预测失败:{e}')
# continue continue
# predict_main()
predict_main()