保存预测结果到数据库

This commit is contained in:
workpc 2024-12-24 10:39:57 +08:00
parent 81e255747c
commit 21a7f130bb
2 changed files with 45 additions and 24 deletions

View File

@ -225,7 +225,7 @@ table_name = 'v_tbl_crude_oil_warning'
### 开关 ### 开关
is_train = True # 是否训练 is_train = True # 是否训练
is_debug = False # 是否调试 is_debug = False # 是否调试
is_eta = False # 是否使用eta接口 is_eta = True # 是否使用eta接口
is_timefurture = True # 是否使用时间特征 is_timefurture = True # 是否使用时间特征
is_fivemodels = False # 是否使用之前保存的最佳的5个模型 is_fivemodels = False # 是否使用之前保存的最佳的5个模型
is_edbcode = False # 特征使用edbcoding列表中的 is_edbcode = False # 特征使用edbcoding列表中的

View File

@ -197,10 +197,29 @@ def ex_Model(df,horizon,input_size,train_steps,val_check_steps,early_stop_patien
df_predict=nf.predict(df_test).reset_index() df_predict=nf.predict(df_test).reset_index()
df_predict.astype({col: 'float32' for col in df_predict.columns if col not in ['ds'] }) df_predict.astype({col: 'float32' for col in df_predict.columns if col not in ['ds'] })
# 添加预测时间
df_predict['created_dt'] = end_time
# 保存预测值 # 保存预测值
df_predict.to_csv(os.path.join(dataset,"predict.csv"),index=False) df_predict.to_csv(os.path.join(dataset,"predict.csv"),index=False)
df_predict2 = df_predict.copy() # 将预测结果保存到数据库
def save_to_database(df):
if not sqlitedb.check_table_exists('predict'):
df.to_sql('predict',sqlitedb.connection,index=False)
else:
for col in df.columns:
sqlitedb.add_column_if_not_exists('predict',col,'TEXT')
for row in df.itertuples(index=False):
row_dict = row._asdict()
columns=row_dict.keys()
check_query = sqlitedb.select_data('predict',where_condition = f"ds = '{row.ds} and model = {row.model}'")
if len(check_query) > 0:
set_clause = ", ".join([f"{key} = '{value}'" for key, value in row_dict.items()])
sqlitedb.update_data('predict',set_clause,where_condition = f"ds = '{row.ds}'")
continue
sqlitedb.insert_data('predict',tuple(row_dict.values()),columns=columns)
save_to_database(df_predict)
# 把预测值上传到eta # 把预测值上传到eta
if is_update_eta: if is_update_eta:
@ -307,28 +326,30 @@ def model_losss(sqlitedb,end_time):
except ValueError : except ValueError :
df_predict['ds'] = pd.to_datetime(df_predict['ds'],format=r'%Y/%m/%d') df_predict['ds'] = pd.to_datetime(df_predict['ds'],format=r'%Y/%m/%d')
def first_row_to_database(df): # def first_row_to_database(df):
# # 取第一行数据存储到数据库中 # # # 取第一行数据存储到数据库中
first_row = df.head(1) # first_row = df.head(1)
first_row['ds'] = first_row['ds'].dt.strftime('%Y-%m-%d 00:00:00') # first_row['ds'] = first_row['ds'].dt.strftime('%Y-%m-%d 00:00:00')
# 将预测结果保存到数据库 # # 将预测结果保存到数据库
if not sqlitedb.check_table_exists('trueandpredict'): # if not sqlitedb.check_table_exists('trueandpredict'):
first_row.to_sql('trueandpredict',sqlitedb.connection,index=False) # first_row.to_sql('trueandpredict',sqlitedb.connection,index=False)
else: # else:
for col in first_row.columns: # for col in first_row.columns:
sqlitedb.add_column_if_not_exists('trueandpredict',col,'TEXT') # sqlitedb.add_column_if_not_exists('trueandpredict',col,'TEXT')
for row in first_row.itertuples(index=False): # for row in first_row.itertuples(index=False):
row_dict = row._asdict() # row_dict = row._asdict()
columns=row_dict.keys() # columns=row_dict.keys()
check_query = sqlitedb.select_data('trueandpredict',where_condition = f"ds = '{row.ds}'") # check_query = sqlitedb.select_data('trueandpredict',where_condition = f"ds = '{row.ds}'")
if len(check_query) > 0: # if len(check_query) > 0:
set_clause = ", ".join([f"{key} = '{value}'" for key, value in row_dict.items()]) # set_clause = ", ".join([f"{key} = '{value}'" for key, value in row_dict.items()])
sqlitedb.update_data('trueandpredict',set_clause,where_condition = f"ds = '{row.ds}'") # sqlitedb.update_data('trueandpredict',set_clause,where_condition = f"ds = '{row.ds}'")
continue # continue
sqlitedb.insert_data('trueandpredict',tuple(row_dict.values()),columns=columns) # sqlitedb.insert_data('trueandpredict',tuple(row_dict.values()),columns=columns)
first_row_to_database(df_predict)
# first_row_to_database(df_predict)
df_combined3 = pd.concat([df_combined3, df_predict]).reset_index(drop=True) df_combined3 = pd.concat([df_combined3, df_predict]).reset_index(drop=True)
# 计算每个模型与最佳模型的绝对误差比例根据设置的阈值rote筛选预测值显示最大最小值 # 计算每个模型与最佳模型的绝对误差比例根据设置的阈值rote筛选预测值显示最大最小值