diff --git a/lib/dataread.py b/lib/dataread.py index a76247a..2a549c1 100644 --- a/lib/dataread.py +++ b/lib/dataread.py @@ -756,7 +756,7 @@ def calculate_correlation(df): # 去掉ds y df = df.drop(columns=['ds', 'y']) # 计算相关系数 - corr = df.corrwith(yy) + corr = df.corrwith(yy).round(4) # 输出相关系数 print(corr) # 保存结果 diff --git a/lib/tools.py b/lib/tools.py index 0188f49..7f043cb 100644 --- a/lib/tools.py +++ b/lib/tools.py @@ -1074,7 +1074,7 @@ def find_best_models(date='', global_config=None): return best_models -def plot_pp_predict_result(y_hat, global_config): +def plot_pp_predict_result(y_hat, global_config,wd='yuedu'): """ 绘制PP期货预测结果的图表 """ @@ -1082,11 +1082,18 @@ def plot_pp_predict_result(y_hat, global_config): import seaborn as sns # 获取y的真实值 - # y = pd.read_csv(os.path.join( - # global_config['dataset'], '指标数据.csv'))[['ds', 'y']] - y = pd.read_csv('juxitingdataset/指标数据.csv')[['ds', 'y']] + if wd == 'yuedu': + y = pd.read_csv(os.path.join( + global_config['dataset'], '指标数据.csv'))[['ds', 'y']][-12:] + xgx_df = pd.read_csv(os.path.join( + global_config['dataset'], '相关系数.csv')) + else: + y = pd.read_csv('juxitingdataset/指标数据.csv')[['ds', 'y']][-30:] + xgx_df = pd.read_csv('juxitingdataset/相关系数.csv') + xgx_df = xgx_df.rename(columns={xgx_df.columns[0]: '指标', xgx_df.columns[1]: '系数'}) + top_10_correlations = xgx_df.sort_values(by='系数',ascending=False)[1:11] y['ds'] = pd.to_datetime(y['ds']) - y = y[y['ds'] < y_hat['ds'].iloc[0]][-30:] + y = y[y['ds'] < y_hat['ds'].iloc[0]] # 取y的最后一行数据追加到y_hat(将真实值最后一行作为预测值起点) if not y.empty: @@ -1096,7 +1103,8 @@ def plot_pp_predict_result(y_hat, global_config): y_y_hat = pd.concat([y_last_row, y_hat], ignore_index=True) # 创建图表和子图布局,为表格预留空间 - fig, ax = plt.subplots(figsize=(16, 9)) + fig = plt.figure(figsize=(16, 22)) + ax = fig.add_axes([0.05, 0.55, 0.9, 0.25]) # 16:9核心参数 # 对日期列进行排序,确保日期大的在右边 y_y_hat = y_y_hat.sort_values(by='ds') @@ -1132,13 +1140,33 @@ def plot_pp_predict_result(y_hat, global_config): # 在图表下方添加表格 table = ax.table(cellText=data, colLabels=columns, - loc='bottom', bbox=[0, -0.6, 1, 0.2]) + loc='bottom', bbox=[0, -0.4, 1, 0.2],cellLoc='center') table.auto_set_font_size(False) - table.set_fontsize(14) - - plt.tight_layout(rect=[0, 0.1, 1, 1]) # 调整布局,为表格留出空间 + table.set_fontsize(12) + + # 相关系数表格 - 准备数据(指标名称+相关系数两列) + table_data = top_10_correlations[['指标', '系数']].values.tolist() # 提取表格数据 + table_data.insert(0, ['指标名称', '相关系数']) # 添加表头 + + # 在当前图表下方绘制表格(调整bbox参数控制位置和大小) + table = ax.table( + cellText=table_data, # 表格数据 + loc='bottom', # 表格位置(底部) + bbox=[0, -0.9, 1, 0.4], # [左, 下, 宽, 高],调整下边界(-0.7)和高度(0.5)控制表格位置 + cellLoc='left' + ) + table.auto_set_font_size(False) # 关闭自动字体大小 + table.set_fontsize(10) # 设置表格字体大小 + table.scale(1.1, 1.5) # 调整表格缩放比例(宽度, 高度) + + plt.tight_layout() # 自动调整整体布局 plt.savefig(os.path.join( - global_config['dataset'], 'pp_predict_result.png')) + global_config['dataset'], f'pp_{wd}correlation.png'), + bbox_inches='tight', + pad_inches=1.0 # 增加边距 + ) + + if __name__ == '__main__': diff --git a/main_juxiting_yuedu.py b/main_juxiting_yuedu.py index bcad998..3562524 100644 --- a/main_juxiting_yuedu.py +++ b/main_juxiting_yuedu.py @@ -528,13 +528,19 @@ def predict_main(): if __name__ == '__main__': # global end_time # 遍历2024-11-25 到 2024-12-3 之间的工作日日期 - for i_time in pd.date_range('2025-7-28', '2025-7-29', freq='B'): - try: - global_config['end_time'] = i_time.strftime('%Y-%m-%d') - global_config['db_mysql'].connect() - predict_main() - except Exception as e: - logger.info(f'预测失败:{e}') - continue + # for i_time in pd.date_range('2025-7-28', '2025-7-29', freq='B'): + # try: + # global_config['end_time'] = i_time.strftime('%Y-%m-%d') + # global_config['db_mysql'].connect() + # predict_main() + # except Exception as e: + # logger.info(f'预测失败:{e}') + # continue - # predict_main() + predict_main() + + # 图片报告 + # global_config['end_time'] = '2025-07-31' + # logger.info('图片报告ing') + # pp_bdwd_png(global_config=global_config) + # logger.info('图片报告end') diff --git a/models/nerulforcastmodels.py b/models/nerulforcastmodels.py index f613641..3df6caf 100644 --- a/models/nerulforcastmodels.py +++ b/models/nerulforcastmodels.py @@ -3539,11 +3539,34 @@ def pp_export_pdf(num_indicators=475, num_models=21, num_dayindicator=202, input def pp_bdwd_png(global_config): best_bdwd_price = find_best_models( date=global_config['end_time'], global_config=global_config) - # y_hat = pd.DataFrame(best_bdwd_price).T[['date', 'predictresult']][-4:] - y_hat = pd.DataFrame(best_bdwd_price).T[['date', 'predictresult']] - y_hat['ds'] = pd.to_datetime(y_hat['date']) - # 绘制PP期货预测结果的图表 - plot_pp_predict_result(y_hat, global_config) + y_hat_yuedu = pd.DataFrame(best_bdwd_price).T[['date', 'predictresult']][-4:] + y_hat_yuedu['ds'] = pd.to_datetime(y_hat_yuedu['date']) + # 绘制PP期货月度预测结果的图表 + plot_pp_predict_result(y_hat_yuedu, global_config) + + y_hat_zhoudu = pd.DataFrame(best_bdwd_price).T[['date', 'predictresult']][2:4] + y_hat_zhoudu['ds'] = pd.to_datetime(y_hat_zhoudu['date']) + y_hat_zhoudu.drop(columns=['date'],inplace=True) + print(y_hat_zhoudu) + # 获取本周最佳模型的五日预测价格 + five_days_predict_price = pd.read_csv('juxitingdataset/predict.csv') + week_price_modelname = best_bdwd_price['week_price']['model_name'] + five_days_predict_price = five_days_predict_price[['ds',week_price_modelname]] + five_days_predict_price['ds'] = pd.to_datetime(five_days_predict_price['ds']) + five_days_predict_price.rename(columns={week_price_modelname:'predictresult'},inplace=True) + # 设置索引 次日 次二日 次三日 次四日 次五日 + index_labels = ["次日", "次二日", "次三日", "次四日", "次五日"] + five_days_predict_price.index = index_labels + y_hat_riduzhoudu = pd.concat([y_hat_zhoudu, five_days_predict_price], axis=0) + y_hat_riduzhoudu = y_hat_riduzhoudu.sort_values(by='ds') + print(y_hat_riduzhoudu) + # 绘制PP期货日度周度预测结果的图表 + plot_pp_predict_result(y_hat_riduzhoudu, global_config,'zhoudu') + + + + + def pp_export_pdf_v1(num_indicators=475, num_models=21, num_dayindicator=202, inputsize=5, dataset='dataset', time='2024-07-30', reportname='report.pdf'): diff --git a/聚烯烃八大维度预测任务.py b/聚烯烃八大维度预测任务.py index e570298..4237d57 100644 --- a/聚烯烃八大维度预测任务.py +++ b/聚烯烃八大维度预测任务.py @@ -17,8 +17,8 @@ def run_predictions(target_date): # 依次执行每个脚本 for script in scripts: # command = [r"C:\Users\Hello\.conda\envs\predict\python", script] # liuruipc - # command = [r"C:/Users/EDY/.conda/envs/priceforecast/python.exe", script] #168pc - command = [r"C:/Users/Hello/.conda/envs/jaigeyuce/python.exe", script] #yitijipc + command = [r"C:/Users/EDY/.conda/envs/priceforecast/python.exe", script] #168pc + # command = [r"C:/Users/Hello/.conda/envs/jaigeyuce/python.exe", script] #yitijipc subprocess.run(command, check=True)