补充9月数据

main
鸽子 1 year ago
parent 4224e07dee
commit d0b1a172ac

@ -9,20 +9,52 @@ mpl.rcParams['font.sans-serif']=['kaiti']
pd.set_option('display.width',None) pd.set_option('display.width',None)
data = pd.read_excel(r'C:\Users\user\PycharmProjects\pytorch2\入模数据\杭州数据.xlsx',index_col='dtdate') def hf_season(x):
list1= []
for i in range(1,13):
if x.loc[f'2021-{i}'].mean() >= x.describe()['75%']:
list1.append(i)
return list1
def season(x):
if str(x)[5:7] in ('06','07','08','12','01','02'):
return 1
else:
return 0
def month(x):
if str(x)[5:7] in ('08','09','10','12','01','02'):
return 1
else:
return 0
def normal(nd):
high = nd.describe()['75%'] + 1.5*(nd.describe()['75%']-nd.describe()['25%'])
low = nd.describe()['25%'] - 1.5*(nd.describe()['75%']-nd.describe()['25%'])
return nd[(nd<high)&(nd>low)]
data = pd.read_excel(r'C:\python-project\pytorch3\入模数据\杭州数据.xlsx',index_col='dtdate')
data.index = pd.to_datetime(data.index,format='%Y-%m-%d') data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
data = data.loc[normal(data['售电量']).index]
plt.plot(range(len(data)),data['售电量']) plt.plot(range(len(data)),data['售电量'])
plt.show() plt.show()
print(data.head())
# print(hf_season(data.loc['2021']['售电量']))
data['month'] = data.index.strftime('%Y-%m-%d').str[6]
data['month'] = data['month'].astype('int')
data['season'] = data.index.map(season)
print(data.head(50))
df_eval = data.loc['2023-9'] df_eval = data.loc['2023-9']
df_train = data.loc['2021-1':'2023-8'] df_train = data.loc['2021-1':'2023-8']
# df_train = df[500:850] # df_train = df[500:850]
print(len(df_eval),len(df_train),len(data)) print(len(df_eval),len(df_train),len(data))
print(data.drop(columns='city_name').corr(method='pearson')['season'])
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量']] df_train = df_train[['tem_max','tem_min','24ST','rh','rh_max','prs','prs_max','prs_min','售电量','month','holiday','season']]
# IQR = df['售电量'].describe()['75%'] - df['售电量'].describe()['25%'] # IQR = df['售电量'].describe()['75%'] - df['售电量'].describe()['25%']
@ -33,14 +65,15 @@ df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量'
# df_train = df_train[(df['售电量'] <= high) & (df['售电量'] >= low)] # df_train = df_train[(df['售电量'] <= high) & (df['售电量'] >= low)]
X = df_train[['tem_max','tem_min','holiday','24ST','rh','prs']] X = df_train[['tem_max','tem_min','24ST','holiday','season']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','rh','prs']] X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']]
y = df_train['售电量'] y = df_train['售电量']
print(y.describe())
# best_goal = 1 # best_goal = 1
# best_i = {} # best_i = {}
# for i in range(400): # for i in range(400):
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.15,random_state=216) x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.15,random_state=209)
model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150)
model.fit(x_train,y_train) model.fit(x_train,y_train)
@ -53,13 +86,14 @@ eval_pred = model.predict(X_eval)
result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index) result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index)
# print(result_eval) # print(result_eval)
# print((result_eval['eval'].sum()-result_eval['pred'].sum())/result_eval['eval'].sum()) print((result_eval['eval'].sum()-result_eval['pred'].sum())/result_eval['eval'].sum())
goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum() goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum()
print(goal) print('goal:',goal)
goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum() goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum()
print(goal2) print('goal2:',goal2)
print('r2:',r2_score(y_test,y_pred))
# if abs(goal) < best_goal: # if abs(goal) < best_goal:
# best_goal = abs(goal) # best_goal = abs(goal)
# best_i['best_i'] = i # best_i['best_i'] = i
@ -68,9 +102,9 @@ print(goal2)
result_eval.to_csv(r'C:\Users\user\Desktop\9月各地市日电量预测结果\杭州.csv') # result_eval.to_csv(r'C:\Users\user\Desktop\9月各地市日电量预测结果\杭州.csv')
with open(r'C:\Users\user\Desktop\9月各地市日电量预测结果\偏差率.txt','a',encoding='utf-8') as f: # with open(r'C:\Users\user\Desktop\9月各地市日电量预测结果\偏差率.txt','a',encoding='utf-8') as f:
f.write(f'杭州月末3天偏差率{round(goal,5)},9号-月底偏差率:{round(goal2,5)}\n') # f.write(f'杭州月末3天偏差率{round(goal,5)},9号-月底偏差率:{round(goal2,5)}\n')
# # 保存模型 # # 保存模型
# model.save_model('hangzhou.bin') # model.save_model('hangzhou.bin')
# loaded_model = xgb.XGBRegressor() # loaded_model = xgb.XGBRegressor()

@ -1,62 +1,62 @@
import pandas as pd # import pandas as pd
import os # import os
import re # import re
file_dir1 = r'C:\Users\鸽子\Desktop\一版结果\电压等级电量预测结果\偏差率' # file_dir1 = r'C:\Users\鸽子\Desktop\一版结果\电压等级电量预测结果\偏差率'
file_dir2 = r'C:\Users\鸽子\Desktop\一版结果\电压等级电量预测结果\月底3天预测结果' # file_dir2 = r'C:\Users\鸽子\Desktop\一版结果\电压等级电量预测结果\月底3天预测结果'
file_dir3 = r'C:\Users\鸽子\Desktop\一版结果\行业电量预测结果\偏差' # file_dir3 = r'C:\Users\鸽子\Desktop\一版结果\行业电量预测结果\偏差'
import numpy as np # import numpy as np
np.set_printoptions(threshold=np.inf) # np.set_printoptions(threshold=np.inf)
#
# print(os.listdir(file_dir3)) # # print(os.listdir(file_dir3))
# str1 = '丽水电压等级10kv以下月底偏差率:0.00229' # # str1 = '丽水电压等级10kv以下月底偏差率:0.00229'
# # #
# print(re.split('电压等级|月底偏差率:',str1)) # # print(re.split('电压等级|月底偏差率:',str1))
# with open(os.path.join(file_dir3,'9月底偏差率.txt'),'r',encoding='utf-8') as f: # # with open(os.path.join(file_dir3,'9月底偏差率.txt'),'r',encoding='utf-8') as f:
# lines = f.readlines() # # lines = f.readlines()
# list_city = [] # # list_city = []
# list_industry = [] # # list_industry = []
# list_loss = [] # # list_loss = []
# for i in lines: # # for i in lines:
# i = re.split(':||其中', i) # # i = re.split(':||其中', i)
# # print(i)
# # list_city.append(i[0][:2])
# # list_industry.append(i[-2].replace(i[0][:2],''))
# # list_loss.append(i[-1][:-2])
# # df_level = pd.DataFrame({'城市':list_city,'行业':list_industry,'偏差':list_loss})
# # # df_level.to_csv(os.path.join(file_dir3,'9月底偏差率.csv'),encoding='gbk')
# # print(df_level)
# file_dir = r'C:\python-project\pytorch3\浙江行业电量\浙江所有地市133行业数据'
# # print(os.listdir(file_dir))
# dict1 = {}
#
# for file in os.listdir(file_dir):
#
# df = pd.read_excel(os.path.join(file_dir,file),index_col=' stat_date ')
#
# col_list = df.drop(columns=[i for i in df.columns if (df[i] == 0).sum() / len(df) >= 0.5]).columns
# dict1[file[:2]] = col_list
# print(dict1)
#
# # print(len(df.drop(columns=[i for i in df.columns if (df[i] == 0).sum() / len(df) >= 0.5]).columns))
#
# read_path = r'C:\Users\鸽子\Desktop\一版结果\行业电量预测结果\月底预测结果'
# list1 = []
# for i in os.listdir(read_path):
# print(i) # print(i)
# list_city.append(i[0][:2]) # data = pd.read_csv(os.path.join(read_path, i), sep='\t',header=None)
# list_industry.append(i[-2].replace(i[0][:2],'')) # data = data[data.columns[1:]]
# list_loss.append(i[-1][:-2]) #
# df_level = pd.DataFrame({'城市':list_city,'行业':list_industry,'偏差':list_loss}) #
# # df_level.to_csv(os.path.join(file_dir3,'9月底偏差率.csv'),encoding='gbk') # for j,step in enumerate(range(0, len(data), 4)):
# print(df_level) # df = data.iloc[step+1:step + 4, :]
file_dir = r'C:\python-project\pytorch3\浙江行业电量\浙江所有地市133行业数据' # df.columns = ['预测值', '实际值', '偏差率']
# print(os.listdir(file_dir)) # try:
dict1 = {} # df['行业'] = dict1[i[2:4]][j]
# except:
for file in os.listdir(file_dir): # pass
# df['城市'] = i[2:4]
df = pd.read_excel(os.path.join(file_dir,file),index_col=' stat_date ') # list1.append(df)
# print(df)
col_list = df.drop(columns=[i for i in df.columns if (df[i] == 0).sum() / len(df) >= 0.5]).columns # df = pd.concat(list1,ignore_index=True)
dict1[file[:2]] = col_list # df.to_csv('各市行业电量预测结果.csv',encoding='gbk')
print(dict1) # print(df)
# print(len(df.drop(columns=[i for i in df.columns if (df[i] == 0).sum() / len(df) >= 0.5]).columns))
read_path = r'C:\Users\鸽子\Desktop\一版结果\行业电量预测结果\月底预测结果'
list1 = []
for i in os.listdir(read_path):
print(i)
data = pd.read_csv(os.path.join(read_path, i), sep='\t',header=None)
data = data[data.columns[1:]]
for j,step in enumerate(range(0, len(data), 4)):
df = data.iloc[step+1:step + 4, :]
df.columns = ['预测值', '实际值', '偏差率']
try:
df['行业'] = dict1[i[2:4]][j]
except:
pass
df['城市'] = i[2:4]
list1.append(df)
print(df)
df = pd.concat(list1,ignore_index=True)
df.to_csv('各市行业电量预测结果.csv',encoding='gbk')
print(df)

@ -147,6 +147,7 @@ def run(file_dir,excel):
print(target) print(target)
print(result_eight) print(result_eight)
final_df = pd.concat(list_app,ignore_index=True) final_df = pd.concat(list_app,ignore_index=True)
final_df.to_csv('市行业电量.csv',encoding='gbk')
print(final_df) print(final_df)
# result_eight.to_csv(f'./月底预测结果/9月{excel[:2]}.txt', sep='\t', mode='a') # result_eight.to_csv(f'./月底预测结果/9月{excel[:2]}.txt', sep='\t', mode='a')

Loading…
Cancel
Save