diff --git a/config_jingbo.py b/config_jingbo.py index d11207b..ff08ed8 100644 --- a/config_jingbo.py +++ b/config_jingbo.py @@ -225,7 +225,7 @@ table_name = 'v_tbl_crude_oil_warning' ### 开关 is_train = True # 是否训练 is_debug = False # 是否调试 -is_eta = False # 是否使用eta接口 +is_eta = True # 是否使用eta接口 is_timefurture = True # 是否使用时间特征 is_fivemodels = False # 是否使用之前保存的最佳的5个模型 is_edbcode = False # 特征使用edbcoding列表中的 diff --git a/models/nerulforcastmodels.py b/models/nerulforcastmodels.py index f91f215..dd39d1b 100644 --- a/models/nerulforcastmodels.py +++ b/models/nerulforcastmodels.py @@ -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.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_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 if is_update_eta: @@ -307,28 +326,30 @@ def model_losss(sqlitedb,end_time): except ValueError : df_predict['ds'] = pd.to_datetime(df_predict['ds'],format=r'%Y/%m/%d') - def first_row_to_database(df): - # # 取第一行数据存储到数据库中 - first_row = df.head(1) - first_row['ds'] = first_row['ds'].dt.strftime('%Y-%m-%d 00:00:00') - # 将预测结果保存到数据库 - if not sqlitedb.check_table_exists('trueandpredict'): - first_row.to_sql('trueandpredict',sqlitedb.connection,index=False) - else: - for col in first_row.columns: - sqlitedb.add_column_if_not_exists('trueandpredict',col,'TEXT') - for row in first_row.itertuples(index=False): - row_dict = row._asdict() - columns=row_dict.keys() - 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()]) - sqlitedb.update_data('trueandpredict',set_clause,where_condition = f"ds = '{row.ds}'") - continue - sqlitedb.insert_data('trueandpredict',tuple(row_dict.values()),columns=columns) - - first_row_to_database(df_predict) + # def first_row_to_database(df): + # # # 取第一行数据存储到数据库中 + # first_row = df.head(1) + # first_row['ds'] = first_row['ds'].dt.strftime('%Y-%m-%d 00:00:00') + # # 将预测结果保存到数据库 + # if not sqlitedb.check_table_exists('trueandpredict'): + # first_row.to_sql('trueandpredict',sqlitedb.connection,index=False) + # else: + # for col in first_row.columns: + # sqlitedb.add_column_if_not_exists('trueandpredict',col,'TEXT') + # for row in first_row.itertuples(index=False): + # row_dict = row._asdict() + # columns=row_dict.keys() + # 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()]) + # sqlitedb.update_data('trueandpredict',set_clause,where_condition = f"ds = '{row.ds}'") + # continue + # sqlitedb.insert_data('trueandpredict',tuple(row_dict.values()),columns=columns) + # first_row_to_database(df_predict) + + + df_combined3 = pd.concat([df_combined3, df_predict]).reset_index(drop=True) # 计算每个模型与最佳模型的绝对误差比例,根据设置的阈值rote筛选预测值显示最大最小值