diff --git a/config_jingbo.py b/config_jingbo.py index c47bfac..1d2ae36 100644 --- a/config_jingbo.py +++ b/config_jingbo.py @@ -250,9 +250,9 @@ is_corr = False # 特征是否参与滞后领先提升相关系数 add_kdj = False # 是否添加kdj指标 if add_kdj and is_edbnamelist: edbnamelist = edbnamelist+['K','D','J'] + ### 模型参数 y = 'Brent连1合约价格' # 原油指标数据的目标变量 Brent连1合约价格 Brent活跃合约 -# y = '期货结算价(连续):布伦特原油:前一个观测值' # ineoil的目标变量 horizon =5 # 预测的步长 input_size = 40 # 输入序列长度 train_steps = 50 if is_debug else 1000 # 训练步数,用来限定epoch次数 @@ -267,10 +267,12 @@ k = 100 # 特征筛选数量,如果是0或者值比特征数量大,代表全 corr_threshold = 0.6 # 相关性大于0.6的特征 rote = 0.06 # 绘图上下界阈值 +### 计算准确率 +weight_dict = [0.4,0.15,0.1,0.1,0.25] # 权重 + + ### 文件 data_set = '原油指标数据.xlsx' # 数据集文件 -# data_set = 'INE_OIL(1).csv' -### 文件夹 dataset = 'yuanyoudataset' # 数据集文件夹 # 数据库名称 diff --git a/models/nerulforcastmodels.py b/models/nerulforcastmodels.py index 44e4c2a..66945d9 100644 --- a/models/nerulforcastmodels.py +++ b/models/nerulforcastmodels.py @@ -495,29 +495,31 @@ def model_losss(sqlitedb,end_time): df['ACCURACY'] = df.apply(calculate_accuracy, axis=1) # df['ACCURACY'] = df.apply(is_within_range, axis=1) # 取结束日期上一周的日期 - endtime = end_time - endtimeweek = datetime.datetime.strptime(endtime, '%Y-%m-%d') - up_week = endtimeweek - datetime.timedelta(days=endtimeweek.weekday() + 14) - up_week_dates = [up_week + datetime.timedelta(days=i) for i in range(14)][4:-2] - up_week_dates = [date.strftime('%Y-%m-%d') for date in up_week_dates] - # - df3 = df.copy() - df3 = df3[df3['CREAT_DATE'].isin(up_week_dates)] - df3 = df3[df3['ds'].isin(up_week_dates)] - # df3.to_csv(os.path.join(dataset,f'accuracy_{endtime}.csv'),index=False) - total = len(df3) - accuracy_rote = 0 - # 设置权重字典 - weight_dict = [0.4,0.15,0.1,0.1,0.25] - for i,group in df3.groupby('ds'): - print('权重:',weight_dict[len(group)-1]) - print('准确率:',(group['ACCURACY'].sum()/len(group))*weight_dict[len(group)-1]) - accuracy_rote += (group['ACCURACY'].sum()/len(group))*weight_dict[len(group)-1] - df4 = pd.DataFrame(columns=['开始日期','结束日期','准确率']) - df4.loc[len(df4)] = {'开始日期':up_week_dates[0],'结束日期':up_week_dates[-1],'准确率':accuracy_rote} - print(df4) - df4.to_sql("accuracy_rote", con=sqlitedb.connection, if_exists='append', index=False) - + def get_week_date(end_time): + endtime = end_time + endtimeweek = datetime.datetime.strptime(endtime, '%Y-%m-%d') + up_week = endtimeweek - datetime.timedelta(days=endtimeweek.weekday() + 14) + up_week_dates = [up_week + datetime.timedelta(days=i) for i in range(14)][4:-2] + up_week_dates = [date.strftime('%Y-%m-%d') for date in up_week_dates] + return up_week_dates + up_week_dates = get_week_date(end_time) + + # 计算准确率并保存结果 + def _get_accuracy_rate(df,up_week_dates,endtime): + df3 = df.copy() + df3 = df3[df3['CREAT_DATE'].isin(up_week_dates)] + df3 = df3[df3['ds'].isin(up_week_dates)] + accuracy_rote = 0 + for i,group in df3.groupby('ds'): + print('权重:',weight_dict[len(group)-1]) + print('准确率:',(group['ACCURACY'].sum()/len(group))*weight_dict[len(group)-1]) + accuracy_rote += (group['ACCURACY'].sum()/len(group))*weight_dict[len(group)-1] + df3.to_csv(os.path.join(dataset,f'accuracy_{endtime}.csv'),index=False) + df4 = pd.DataFrame(columns=['开始日期','结束日期','准确率']) + df4.loc[len(df4)] = {'开始日期':up_week_dates[0],'结束日期':up_week_dates[-1],'准确率':accuracy_rote} + df4.to_sql("accuracy_rote", con=sqlitedb.connection, if_exists='append', index=False) + _get_accuracy_rate(df,up_week_dates,end_time) + def _add_abs_error_rate(): # 计算每个预测值与真实值之间的偏差率 for model in allmodelnames: @@ -534,7 +536,6 @@ def model_losss(sqlitedb,end_time): # 将最小偏差率对应的模型的预测值添加到DataFrame中 df_combined3['min_abs_error_rate_prediction'] = min_abs_error_rate_predictions df_combined3['min_abs_error_rate_column_name'] = min_abs_error_rate_column_name - # _add_abs_error_rate() # 判断 df 的数值列转为float