From d1415d065f03323298e09afc4a29b7bdfd63dbac Mon Sep 17 00:00:00 2001 From: workpc Date: Thu, 12 Dec 2024 17:07:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=86=E7=A1=AE=E7=8E=87=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config_jingbo.py | 6 ++-- models/nerulforcastmodels.py | 62 ++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/config_jingbo.py b/config_jingbo.py index b601ed8..25efd63 100644 --- a/config_jingbo.py +++ b/config_jingbo.py @@ -135,10 +135,6 @@ ClassifyId = 1214 # } # 生产环境数据库 -# url: jdbc:mysql://rm-2zehj3r1n60ttz9x5.mysql.rds.aliyuncs.com:3306/jingbo?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&useSSL=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true -# username: jingbo -# password: shihua@123 -# table-schema: jingbo # host = 'rm-2zehj3r1n60ttz9x5.mysql.rds.aliyuncs.com' # port = 3306 # dbusername ='jingbo' @@ -146,6 +142,8 @@ ClassifyId = 1214 # dbname = 'jingbo' # table_name = 'v_tbl_crude_oil_warning' + + # 预生产环境 # host = 'rm-2zehj3r1n60ttz9x5ko.mysql.rds.aliyuncs.com' # port = 3306 diff --git a/models/nerulforcastmodels.py b/models/nerulforcastmodels.py index 0ddda2a..5946441 100644 --- a/models/nerulforcastmodels.py +++ b/models/nerulforcastmodels.py @@ -351,6 +351,27 @@ def model_losss(sqlitedb): lower_bound = df_combined3.loc[row.name,row['columns']].min() return pd.Series([lower_bound, upper_bound], index=['min_within_quantile', 'max_within_quantile']) df_combined3[['min_within_quantile','max_within_quantile']] = names_df.apply(add_upper_lower_bound, axis=1) + + + + def find_closest_values(row): + x = row.y + if x is None: + return pd.Series([None, None], index=['min_within_quantile','max_within_quantile']) + row = row.drop('ds') + row = row[:,0].values.tolist() + row = row.sort() + print(row) + # x 在row中的索引 + index = row.index(x) + if index == 0: + return pd.Series([row[index+1], row[index+2]], index=['min_within_quantile','max_within_quantile']) + elif index == len(row)-1: + return pd.Series([row[index-2], row[index-1]], index=['min_within_quantile','max_within_quantile']) + else: + return pd.Series([row[index-1], row[index+1]], index=['min_within_quantile','max_within_quantile']) + + # df_combined3[['min_within_quantile','max_within_quantile']] = df_combined3.apply(find_closest_values, axis=1) def find_most_common_model(): @@ -358,7 +379,7 @@ def model_losss(sqlitedb): min_model_max_frequency_model = df_combined3['min_model'].tail(60).value_counts().idxmax() max_model_max_frequency_model = df_combined3['max_model'].tail(60).value_counts().idxmax() if min_model_max_frequency_model == max_model_max_frequency_model: - # 取20天第二多的模型 + # 取60天第二多的模型 max_model_max_frequency_model = df_combined3['max_model'].tail(60).value_counts().nlargest(2).index[1] df_predict['min_model'] = min_model_max_frequency_model @@ -369,26 +390,33 @@ def model_losss(sqlitedb): # find_most_common_model() + df_combined3['ds'] = pd.to_datetime(df_combined3['ds']) + df_combined3['ds'] = df_combined3['ds'].dt.strftime('%Y-%m-%d') df_predict2 = df_combined3.tail(horizon) - df_predict2['ds'] = pd.to_datetime(df_predict2['ds']) - df_predict2['ds'] = df_predict2['ds'].dt.strftime('%Y-%m-%d') # 保存到数据库 - if not sqlitedb.check_table_exists('accuracy'): - sqlitedb.create_table('accuracy', columns="id int,PREDICT_DATE datetime,CREAT_DATE datetime, MIN_PRICE TEXT,MAX_PRICE TEXT,HIGH_PRICE TEXT,LOW_PRICE TEXT,RIGHT_ROTE ") - existing_data = sqlitedb.select_data(table_name = "accuracy") - if not existing_data.empty: - max_id = existing_data['id'].astype(int).max() - df_predict2['id'] = range(max_id + 1, max_id + 1 + len(df_predict2)) - else: - df_predict2['id'] = range(1, 1 + len(df_predict2)) + # if not sqlitedb.check_table_exists('accuracy'): + # columns = ','.join(df_combined3.columns.to_list()+['id','CREAT_DATE']) + # sqlitedb.create_table('accuracy',columns=columns) + # existing_data = sqlitedb.select_data(table_name = "accuracy") + # update_y = sqlitedb.select_data(table_name = "accuracy",where_condition='y is null') + # df_combined4 = df_combined3[(df_combined3['ds'].isin(update_y['ds'])) & (df_combined3['y'].notnull())] + # if len(df_combined4) > 0: + # for index, row in df_combined4.iterrows(): + # sqlitedb.update_data('accuracy',f"y = {row['y']}",f"ds = '{row['ds']}'") + # print(df_combined4) + # if not existing_data.empty: + # max_id = existing_data['id'].astype(int).max() + # df_predict2['id'] = range(max_id + 1, max_id + 1 + len(df_predict2)) + # else: + # df_predict2['id'] = range(1, 1 + len(df_predict2)) - df_predict2['CREAT_DATE'] = end_time - df_predict2['PREDICT_DATE'] = df_predict2['ds'] - df_predict2['MIN_PRICE'] = df_predict2['min_within_quantile'] - df_predict2['MAX_PRICE'] = df_predict2['max_within_quantile'] - df_predict2 = df_predict2[['id','PREDICT_DATE','CREAT_DATE','MIN_PRICE','MAX_PRICE']] - df_predict2.to_sql("accuracy", con=sqlitedb.connection, if_exists='append', index=False) + # df_predict2['CREAT_DATE'] = now if end_time == '' else end_time + # df_predict2['PREDICT_DATE'] = df_predict2['ds'] + # df_predict2['MIN_PRICE'] = df_predict2['min_within_quantile'] + # df_predict2['MAX_PRICE'] = df_predict2['max_within_quantile'] + # df_predict2 = df_predict2[['id','PREDICT_DATE','CREAT_DATE','MIN_PRICE','MAX_PRICE']] + # df_predict2.to_sql("accuracy", con=sqlitedb.connection, if_exists='append', index=False)