原油周度预测次周,隔周预测调试完成

This commit is contained in:
workpc 2025-03-05 09:08:52 +08:00
parent eb3d770920
commit e49ab6dd02
8 changed files with 1463 additions and 115 deletions

View File

@ -159,9 +159,9 @@ table_name = 'v_tbl_crude_oil_warning'
### 开关
is_train = True # 是否训练
is_train = False # 是否训练
is_debug = False # 是否调试
is_eta = True # 是否使用eta接口
is_eta = False # 是否使用eta接口
is_market = True # 是否通过市场信息平台获取特征 ,在is_eta 为true 的情况下生效
is_timefurture = True # 是否使用时间特征
is_fivemodels = False # 是否使用之前保存的最佳的5个模型
@ -182,9 +182,9 @@ print("数据库连接成功",host,dbname,dbusername)
# 数据截取日期
start_year = 2020 # 数据开始年份
start_year = 2015 # 数据开始年份
end_time = '' # 数据截取日期
freq = 'W' # 时间频率,"D": 天 "W": 周"M": 月"Q": 季度"A": 年 "H": 小时 "T": 分钟 "S": 秒 "B": 工作日
freq = 'WW' # 时间频率,"D": 天 "W": 周"M": 月"Q": 季度"A": 年 "H": 小时 "T": 分钟 "S": 秒 "B": 工作日 "WW" 自定义周
delweekenday = True if freq == 'B' else False # 是否删除周末数据
is_corr = False # 特征是否参与滞后领先提升相关系数
add_kdj = False # 是否添加kdj指标

View File

@ -42,8 +42,8 @@ plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
# from config_jingbo_pro import *
# from config_jingbo import *
# from config_jingbo_zhoudu import *
from config_jingbo_yuedu import *
from config_jingbo_zhoudu import *
# from config_jingbo_yuedu import *
# from config_yongan import *
# from config_juxiting import *
# from config_juxiting_zhoudu import *
@ -720,7 +720,17 @@ def datachuli(df_zhibiaoshuju,df_zhibiaoliebiao,datecol='date',end_time='',y='y'
logger.info(f'删除两月不更新特征后数据量:{df.shape}')
if freq == 'W':
# 衍生时间特征
if is_timefurture:
df = addtimecharacteristics(df=df,dataset=dataset)
if freq == 'WW':
# 自定义周数据
# 按weekofmothe分组取均值得到新的数据
df = df.groupby(df['yearmonthweeks']).mean()
# 时间列转换为日期格式字符串
df['ds'] = df['ds'].dt.strftime('%Y-%m-%d')
elif freq == 'W':
# 按周取样
df = df.resample('W', on='ds').mean().reset_index()
elif freq == 'M':
@ -750,9 +760,7 @@ def datachuli(df_zhibiaoshuju,df_zhibiaoliebiao,datecol='date',end_time='',y='y'
# kdj指标
if add_kdj:
df = calculate_kdj(df)
# 衍生时间特征
if is_timefurture:
df = addtimecharacteristics(df=df,dataset=dataset)
# 特征分析
featureAnalysis(df,dataset=dataset,y=y)
return df
@ -1833,6 +1841,15 @@ def addtimecharacteristics(df,dataset):
df['is_year_start'] = df['ds'].dt.is_year_start.astype(int)
# 是否年末
df['is_year_end'] = df['ds'].dt.is_year_end.astype(int)
# 添加月度第几周周一到周日为一周每月1日所在的周为第一周
# 计算当前日期所在周的周一
df['current_monday'] = df['ds'] - pd.to_timedelta(df['ds'].dt.dayofweek, unit='D')
# 计算当月1日所在周的周一
df['first_monday'] = df['ds'].dt.to_period('M').dt.start_time - pd.to_timedelta(df['ds'].dt.to_period('M').dt.start_time.dt.dayofweek, unit='D')
# 计算周数差并+1得到周数
df['weekofmonth'] = ((df['current_monday'] - df['first_monday']).dt.days // 7) + 1
df['yearmonthweeks'] = df['year'].astype(str) + df['month'].astype(str) + df['weekofmonth'].astype(str)
df.drop(columns=['current_monday', 'first_monday'], inplace=True)
# 去掉 quarter_start quarter
df.drop(columns=['quarter_start','quarter'],inplace=True)
df.to_csv(os.path.join(dataset,'指标数据添加时间特征.csv'), index=False)

View File

@ -141,6 +141,7 @@ def predict_main():
logger.info('更新accuracy表的y值')
# 找到update_y 中ds且df中的y的行
update_y = update_y[update_y['ds']<=end_time]
logger.info(f'要更新y的信息{update_y}')
# try:
for row in update_y.itertuples(index=False):

View File

@ -124,7 +124,8 @@ def predict_main():
else:
for row in first_row.itertuples(index=False):
row_dict = row._asdict()
row_dict['ds'] = row_dict['ds'].strftime('%Y-%m-%d %H:%M:%S')
# row_dict['ds'] = row_dict['ds'].strftime('%Y-%m-%d')
# row_dict['ds'] = row_dict['ds'].strftime('%Y-%m-%d %H:%M:%S')
check_query = sqlitedb.select_data('trueandpredict', where_condition=f"ds = '{row.ds}'")
if len(check_query) > 0:
set_clause = ", ".join([f"{key} = '{value}'" for key, value in row_dict.items()])

View File

@ -166,7 +166,7 @@ def ex_Model(df,horizon,input_size,train_steps,val_check_steps,early_stop_patien
models.append(model)
# 创建NeuralForecast实例并训练模型
nf = NeuralForecast(models=models, freq=freq)
nf = NeuralForecast(models=models, freq=freq[0])
from joblib import dump, load
if is_train: