diff --git a/入模数据/丽水.xlsx b/入模数据/丽水.xlsx index 13e1ea3..78ca218 100644 Binary files a/入模数据/丽水.xlsx and b/入模数据/丽水.xlsx differ diff --git a/入模数据/台州.xlsx b/入模数据/台州.xlsx index dc1536d..7f4d426 100644 Binary files a/入模数据/台州.xlsx and b/入模数据/台州.xlsx differ diff --git a/入模数据/嘉兴.xlsx b/入模数据/嘉兴.xlsx index b526e10..b8dacc6 100644 Binary files a/入模数据/嘉兴.xlsx and b/入模数据/嘉兴.xlsx differ diff --git a/入模数据/宁波.xlsx b/入模数据/宁波.xlsx index aef7fc6..580b786 100644 Binary files a/入模数据/宁波.xlsx and b/入模数据/宁波.xlsx differ diff --git a/入模数据/杭州.xlsx b/入模数据/杭州.xlsx index fe6b7cc..0c6e50a 100644 Binary files a/入模数据/杭州.xlsx and b/入模数据/杭州.xlsx differ diff --git a/入模数据/温州.xlsx b/入模数据/温州.xlsx index f3483c2..5303d5b 100644 Binary files a/入模数据/温州.xlsx and b/入模数据/温州.xlsx differ diff --git a/入模数据/湖州.xlsx b/入模数据/湖州.xlsx index 7350399..de927f5 100644 Binary files a/入模数据/湖州.xlsx and b/入模数据/湖州.xlsx differ diff --git a/入模数据/绍兴.xlsx b/入模数据/绍兴.xlsx index 906f12e..206ecfe 100644 Binary files a/入模数据/绍兴.xlsx and b/入模数据/绍兴.xlsx differ diff --git a/入模数据/舟山.xlsx b/入模数据/舟山.xlsx index 91250fd..d7824d9 100644 Binary files a/入模数据/舟山.xlsx and b/入模数据/舟山.xlsx differ diff --git a/入模数据/衢州.xlsx b/入模数据/衢州.xlsx index 051aa7f..5f996da 100644 Binary files a/入模数据/衢州.xlsx and b/入模数据/衢州.xlsx differ diff --git a/入模数据/金华.xlsx b/入模数据/金华.xlsx index 3dbb063..3608a1c 100644 Binary files a/入模数据/金华.xlsx and b/入模数据/金华.xlsx differ diff --git a/各地级市日电量模型/hangzhou.bin b/各地级市日电量模型/hangzhou.bin index 8a59454..6e30fa0 100644 Binary files a/各地级市日电量模型/hangzhou.bin and b/各地级市日电量模型/hangzhou.bin differ diff --git a/各地级市日电量模型/jiaxing.bin b/各地级市日电量模型/jiaxing.bin index 7714ae6..1e89ba7 100644 Binary files a/各地级市日电量模型/jiaxing.bin and b/各地级市日电量模型/jiaxing.bin differ diff --git a/各地级市日电量模型/jinhua.bin b/各地级市日电量模型/jinhua.bin index 78a095b..75d186f 100644 Binary files a/各地级市日电量模型/jinhua.bin and b/各地级市日电量模型/jinhua.bin differ diff --git a/各地级市日电量模型/quzhou.bin b/各地级市日电量模型/quzhou.bin index e0fa054..2aa7783 100644 Binary files a/各地级市日电量模型/quzhou.bin and b/各地级市日电量模型/quzhou.bin differ diff --git a/各地级市日电量模型/taizhou.bin b/各地级市日电量模型/taizhou.bin index 682d1ee..acf7683 100644 Binary files a/各地级市日电量模型/taizhou.bin and b/各地级市日电量模型/taizhou.bin differ diff --git a/各地级市日电量模型/wenzhou.bin b/各地级市日电量模型/wenzhou.bin index 7375758..d65aa0b 100644 Binary files a/各地级市日电量模型/wenzhou.bin and b/各地级市日电量模型/wenzhou.bin differ diff --git a/各地级市日电量模型/zhoushan.bin b/各地级市日电量模型/zhoushan.bin index d752769..1b9fb8c 100644 Binary files a/各地级市日电量模型/zhoushan.bin and b/各地级市日电量模型/zhoushan.bin differ diff --git a/各地级市日电量模型/丽水.py b/各地级市日电量模型/丽水.py index e5cb964..614f2ad 100644 --- a/各地级市日电量模型/丽水.py +++ b/各地级市日电量模型/丽水.py @@ -27,10 +27,9 @@ def normal(nd): parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) data = pd.read_excel(os.path.join(parent_dir, '入模数据/丽水.xlsx')) -data['dtdate'] = pd.to_datetime(data['dtdate'],format='%Y-%m-%d') +data['dtdate'] = pd.to_datetime(data['dtdate'], format='%Y-%m-%d') data['year'] = data['dtdate'].dt.year -# data.index = pd.to_datetime(data.index, format='%Y-%m-%d') -data.set_index('dtdate',inplace=True) +data.set_index('dtdate', inplace=True) data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -50,20 +49,18 @@ data = data.loc[normal(data['售电量']).index] data['season'] = data.index.map(season) -df_eval = data.loc['2023-11'] +df_eval = data.loc['2023-12'] # df_train = data.loc['2021-1':'2023-8'] -df_train = data[450:-1] + +df_train = data[-180:] # df_train = data.loc['2022-4':'2023-9'][:-3] -df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season','year']] +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season', 'year']] print(df_train.corr()['售电量']) -X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] -X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] y = df_train['售电量'] -# best_goal = 1 -# best_i = {} -# for i in range(200): x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) @@ -72,32 +69,33 @@ y_pred = model.predict(x_test) result_test = pd.DataFrame({'test': y_test, 'pred': y_pred}, index=y_test.index) # 指标打印 -print(abs(y_test - y_pred).mean() / y_test.mean()) +print('abs_test:',abs(y_test - y_pred).mean() / y_test.mean()) eval_pred = model.predict(X_eval) result_eval = pd.DataFrame({'eval': df_eval['售电量'], 'pred': eval_pred}, index=df_eval['售电量'].index) -print(result_eval['eval']) +print(result_eval) goal = (result_eval['eval'][-3:].sum() - result_eval['pred'][-3:].sum()) / result_eval['eval'].sum() print(goal) goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum() print(goal2) -# with open(r'C:\Users\user\Desktop\9月各地市日电量预测结果\偏差率.txt','a',encoding='utf-8') as f: -# f.write(f'丽水月末3天偏差率:{goal},9号-月底偏差率:{goal2}') -# # 保存模型 -# model.save_model('lishui.bin') import numpy as np X_eval = np.array([ - [17.2, 5.7, 10, 0, 0,2023], - [21.2, 4.3, 10, 0, 0,2023], - [11.5, 6.6, 10, 0, 0,2023] + [11.59, 0.6, 0, 0, 1, 2023], + [16.6, 2.4, 0, 0, 1, 2023], + [15.5, 6.7, 0, 0, 1, 2023], + [15, 9.4, 0, 0, 1, 2023], + [18.6, 7.5, 0, 1, 1, 2023] ]) print(model.predict(X_eval)) result = model.predict(X_eval) -result = pd.DataFrame(result,index=['2023-11-28','2023-11-29','2023-11-30']) -result = pd.concat((result_eval['eval'],result)) -result.index = result.index.map(lambda x:str(x)[:10]) +result = pd.DataFrame(result, index=['2023-12-26', '2023-12-27', '2023-12-28','2023-12-29','2023-12-30']) +result = pd.concat((result_eval['eval'], result)) +result.index = result.index.map(lambda x: str(x)[:10]) +result.columns = ['预测值'] print(result) +with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市区日电量预测_1227.xlsx',mode='a',if_sheet_exists='replace',engine='openpyxl') as writer: + result.to_excel(writer,sheet_name='丽水') \ No newline at end of file diff --git a/各地级市日电量模型/台州.py b/各地级市日电量模型/台州.py index 679a1ce..77f7812 100644 --- a/各地级市日电量模型/台州.py +++ b/各地级市日电量模型/台州.py @@ -5,9 +5,11 @@ from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split import matplotlib as mpl import matplotlib.pyplot as plt -mpl.rcParams['font.sans-serif']=['kaiti'] -pd.set_option('display.width',None) +mpl.rcParams['font.sans-serif'] = ['kaiti'] + +pd.set_option('display.width', None) + def season(x): if str(x)[5:7] in ('01', '02', '10', '11'): @@ -16,17 +18,19 @@ def season(x): return 1 else: return 2 + + 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[(ndlow)] + 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)] + -parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir)) -data = pd.read_excel(os.path.join(parent_dir,'入模数据/台州.xlsx')) -data['dtdate'] = pd.to_datetime(data['dtdate'],format='%Y-%m-%d') +parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) +data = pd.read_excel(os.path.join(parent_dir, '入模数据/台州.xlsx')) +data['dtdate'] = pd.to_datetime(data['dtdate'], format='%Y-%m-%d') data['year'] = data['dtdate'].dt.year -# data.index = pd.to_datetime(data.index, format='%Y-%m-%d') -data.set_index('dtdate',inplace=True) +data.set_index('dtdate', inplace=True) data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -43,58 +47,55 @@ data = data.loc[normal(data['售电量']).index] # print(list0,list1,list2) data['season'] = data.index.map(season) -# data = data.loc[:'2023-9'] -df_eval = data.loc['2023-11'] -df_train = data[500:-1] -# df_train = data[500:][:-3] +df_eval = data.loc['2023-12'] + +df_train = data[-180:] + print(df_train) -df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season','year']] +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season', 'year']] -X = df_train[['tem_max','tem_min','24ST','holiday','season','year']] -X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season','year']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] y = df_train['售电量'] -# best_goal = 1 -# best_i = {} -# for i in range(400): -x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=158) +x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.02, random_state=158) 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) y_pred = model.predict(x_test) -result_test = pd.DataFrame({'test':y_test,'pred':y_pred},index=y_test.index) +result_test = pd.DataFrame({'test': y_test, 'pred': y_pred}, index=y_test.index) # 指标打印 print(abs(y_test - y_pred).mean() / y_test.mean()) eval_pred = model.predict(X_eval) -result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index) -# print(result_eval) -# 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() +result_eval = pd.DataFrame({'eval': df_eval['售电量'], 'pred': eval_pred}, index=df_eval['售电量'].index) + +goal = (result_eval['eval'][-3:].sum() - result_eval['pred'][-3:].sum()) / result_eval['eval'].sum() print(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(result_eval) -# if abs(goal) < best_goal: -# best_goal = abs(goal) -# best_i['best_i'] = i -# print(best_i,best_goal) -# 保存模型 -model.save_model('taizhou.bin') import numpy as np X_eval = np.array([ - [18.8, 6.2, 10, 0, 0,2023], - [21.7, 6.5, 10, 0, 0,2023], - [14.3, 8.4, 10, 0, 0,2023] + [13.1, -0.7, 0, 0, 1, 2023], + [17.9, 2.8, 0, 0, 1, 2023], + [15.8, 7.6, 0, 0, 1, 2023], + [15.9, 8, 0, 0, 1, 2023], + [19.7, 7.8, 0, 1, 1, 2023] ]) print(model.predict(X_eval)) result = model.predict(X_eval) -result = pd.DataFrame(result,index=['2023-11-28','2023-11-29','2023-11-30']) -result = pd.concat((result_eval['eval'],result)) -result.index = result.index.map(lambda x:str(x)[:10]) +result = pd.DataFrame(result, index=['2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29', '2023-12-30']) +result = pd.concat((result_eval['eval'], result)) +result.index = result.index.map(lambda x: str(x)[:10]) +result.columns = ['预测值'] print(result) + +with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市区日电量预测_1227.xlsx', mode='a', if_sheet_exists='replace', + engine='openpyxl') as writer: + result.to_excel(writer, sheet_name='台州') diff --git a/各地级市日电量模型/嘉兴.py b/各地级市日电量模型/嘉兴.py index 2205735..0511d7c 100644 --- a/各地级市日电量模型/嘉兴.py +++ b/各地级市日电量模型/嘉兴.py @@ -3,10 +3,7 @@ import pandas as pd import os from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split -import matplotlib as mpl -import matplotlib.pyplot as plt -mpl.rcParams['font.sans-serif'] = ['kaiti'] pd.set_option('display.width', None) @@ -27,10 +24,10 @@ def normal(nd): parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) data = pd.read_excel(os.path.join(parent_dir, '入模数据/嘉兴.xlsx')) -data['dtdate'] = pd.to_datetime(data['dtdate'],format='%Y-%m-%d') +data['dtdate'] = pd.to_datetime(data['dtdate'], format='%Y-%m-%d') data['year'] = data['dtdate'].dt.year -# data.index = pd.to_datetime(data.index, format='%Y-%m-%d') -data.set_index('dtdate',inplace=True) + +data.set_index('dtdate', inplace=True) data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -46,22 +43,19 @@ data = data.loc[normal(data['售电量']).index] # list1.append(i) # print(list0,list1,list2) data['season'] = data.index.map(season) -# data = data.loc[:'2023-9'] -df_eval = data.loc['2023-11'] -df_train = data.iloc[450:-1] -# df_train = data[450:][:-3] + +df_eval = data.loc['2023-12'] +df_train = data.iloc[-180:] + print(df_train) -df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season','year']] +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season', 'year']] -X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] -X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] y = df_train['售电量'] -# best_goal = 1 -# best_i = {} -# for i in range(400): x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=158) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) @@ -78,13 +72,7 @@ goal = (result_eval['eval'][-3:].sum() - result_eval['pred'][-3:].sum()) / resul goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum() print(goal, goal2) print(result_eval) -# print(goal2) -# if abs(goal) < best_goal : -# best_goal = abs(goal) -# best_i['best_i'] = i -# x = goal2 -# -# print(best_i,best_goal,x) + # 保存模型 model.save_model('jiaxing.bin') @@ -93,13 +81,20 @@ loaded_model.load_model('jiaxing.bin') import numpy as np X_eval = np.array([ - [14.5, 7.7, 10, 0, 1,2023], - [21.6, 10, 10, 0, 1,2023], - [11.9, 6.6, 10, 0, 1,2023] + [10.7, -2.2, 0, 0, 1, 2023], + [12.8, 2.2, 0, 0, 1, 2023], + [11.7, 2.3, 0, 0, 1, 2023], + [13, 1, 0, 0, 1, 2023], + [11.5, 5.8, 0, 1, 1, 2023] ]) print(model.predict(X_eval)) result = model.predict(X_eval) -result = pd.DataFrame(result,index=['2023-11-28','2023-11-29','2023-11-30']) -result = pd.concat((result_eval['eval'],result)) -result.index = result.index.map(lambda x:str(x)[:10]) +result = pd.DataFrame(result, index=['2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29', '2023-12-30']) +result = pd.concat((result_eval['eval'], result)) +result.index = result.index.map(lambda x: str(x)[:10]) +result.columns = ['预测值'] print(result) + +with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市区日电量预测_1227.xlsx', mode='a', if_sheet_exists='replace', + engine='openpyxl') as writer: + result.to_excel(writer, sheet_name='嘉兴') diff --git a/各地级市日电量模型/宁波.py b/各地级市日电量模型/宁波.py index 19d4306..5494446 100644 --- a/各地级市日电量模型/宁波.py +++ b/各地级市日电量模型/宁波.py @@ -3,10 +3,7 @@ import pandas as pd import os from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split -import matplotlib as mpl -import matplotlib.pyplot as plt -mpl.rcParams['font.sans-serif'] = ['kaiti'] pd.set_option('display.width', None) @@ -20,17 +17,17 @@ def season(x): 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%']) + high = nd.describe()['75%'] + 3 * (nd.describe()['75%'] - nd.describe()['25%']) + low = nd.describe()['25%'] - 3 * (nd.describe()['75%'] - nd.describe()['25%']) return nd[(nd < high) & (nd > low)] parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) data = pd.read_excel(os.path.join(parent_dir, '入模数据/宁波.xlsx')) -data['dtdate'] = pd.to_datetime(data['dtdate'],format='%Y-%m-%d') +data['dtdate'] = pd.to_datetime(data['dtdate'], format='%Y-%m-%d') data['year'] = data['dtdate'].dt.year -# data.index = pd.to_datetime(data.index, format='%Y-%m-%d') -data.set_index('dtdate',inplace=True) + +data.set_index('dtdate', inplace=True) data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -48,20 +45,17 @@ data = data.loc[normal(data['售电量']).index] data['season'] = data.index.map(season) -df_eval = data.loc['2023-11'] -# df_train = data.loc['2022-01':'2023-09'][:-3] +df_eval = data.loc['2023-12'] +print(df_eval) -df_train = data.loc['2022-01':'2023-11'] +df_train = data.iloc[-180:] -df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season','year']] +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season', 'year']] -X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] -X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] y = df_train['售电量'] -# best_goal = 1 -# best_i = {} -# for i in range(400): x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=142) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) @@ -83,19 +77,24 @@ print(goal2) print(result_eval) -# 保存模型 -# model.save_model('ningbo.bin') import numpy as np X_eval = np.array([ - [16.5, 6.8, 10, 0, 1,2023], - [21.7, 6.8, 10, 0, 1,2023], - [13, 8.8, 10, 0, 1,2023] + [12.9, -2.9, 0, 0, 1, 2023], + [16.4, 3.4, 0, 0, 1, 2023], + [12.8, 4.9, 0, 0, 1, 2023], + [12.9, 4.6, 0, 0, 1, 2023], + [17.9, 6.4, 0, 1, 1, 2023] ]) print(model.predict(X_eval)) result = model.predict(X_eval) -result = pd.DataFrame(result,index=['2023-11-28','2023-11-29','2023-11-30']) -result = pd.concat((result_eval['eval'],result)) -result.index = result.index.map(lambda x:str(x)[:10]) -print(result) \ No newline at end of file +result = pd.DataFrame(result, index=['2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29', '2023-12-30']) +result = pd.concat((result_eval['eval'], result)) +result.index = result.index.map(lambda x: str(x)[:10]) +result.columns = ['预测值'] +print(result) + +with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市区日电量预测_1227.xlsx', mode='a', if_sheet_exists='replace', + engine='openpyxl') as writer: + result.to_excel(writer, sheet_name='宁波') diff --git a/各地级市日电量模型/杭州.py b/各地级市日电量模型/杭州.py index e740cbf..40aadfb 100644 --- a/各地级市日电量模型/杭州.py +++ b/各地级市日电量模型/杭州.py @@ -35,10 +35,10 @@ def normal(nd): parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) data = pd.read_excel(os.path.join(parent_dir, '入模数据/杭州.xlsx')) -data['dtdate'] = pd.to_datetime(data['dtdate'],format='%Y-%m-%d') +data['dtdate'] = pd.to_datetime(data['dtdate'], format='%Y-%m-%d') data['year'] = data['dtdate'].dt.year # data.index = pd.to_datetime(data.index, format='%Y-%m-%d') -data.set_index('dtdate',inplace=True) +data.set_index('dtdate', inplace=True) data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -55,23 +55,17 @@ data = data.loc[normal(data['售电量']).index] # print(list0,list1,list2) -# data['month'] = data.index.strftime('%Y-%m-%d').str[6] -# data['month'] = data['month'].astype('int') data['season'] = data.index.map(season) -# data = data.loc[:'2023-9'] -df_train = data[500:-1] -# df_train = data[500:][:-3] -df_eval = data.loc['2023-11'] + +df_train = data[-180:] + +df_eval = data.loc['2023-12'] print(df_train) -X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] -X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] y = df_train['售电量'] -# best_goal = 1 -# best_i = {} -# for i in range(400): - x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) model.fit(x_train, y_train) @@ -91,40 +85,22 @@ goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / re print('goal2:', goal2) print(result_eval) print('r2:', r2_score(y_test, y_pred)) -# -# # result_eval.to_csv('asda.csv',encoding='gbk') -# # if abs(goal) < best_goal: -# # best_goal = abs(goal) -# # best_i['best_i'] = i -# # x = goal2 -# # print(best_i,best_goal,x) -# -# -# -# # 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: -# # f.write(f'杭州月末3天偏差率:{round(goal,5)},9号-月底偏差率:{round(goal2,5)}\n') -# 保存模型 -model.save_model('hangzhou.bin') - -# X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']] -# df_eval = pd.read_excel(r'C:\Users\user\Desktop\浙江气象1027.xlsx') -# df_eval.columns = df_eval.columns.map(lambda x:x.strip()) -# df_eval = df_eval[['city_name','dtdate','tem_max','tem_min']] -# df_eval['city_name'] = df_eval['city_name'].map(lambda x:x.strip()) -# df_hangzhou = df_eval[df_eval['city_name']=='金华市'].sort_values(by='dtdate') - - -loaded_model = xgb.XGBRegressor() -loaded_model.load_model('hangzhou.bin') + X_eval = np.array([ - [17.2, 5.7, 10, 0, 0,2023], - [21.2, 4.3, 10, 0, 0,2023], - [11.5, 6.6, 10, 0, 0,2023] + [11.8, -2.8, 0, 0, 1, 2023], + [15.1, 0.2, 0, 0, 1, 2023], + [12.9, 2.2, 0, 0, 1, 2023], + [11.4, 1.3, 0, 0, 1, 2023], + [14.8, 3.6, 0, 1, 1, 2023] ]) print(model.predict(X_eval)) result = model.predict(X_eval) -result = pd.DataFrame(result,index=['2023-11-28','2023-11-29','2023-11-30']) -result = pd.concat((result_eval['eval'],result)) -result.index = result.index.map(lambda x:str(x)[:10]) +result = pd.DataFrame(result, index=['2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29', '2023-12-30']) +result = pd.concat((result_eval['eval'], result)) +result.index = result.index.map(lambda x: str(x)[:10]) +result.columns = ['预测值'] print(result) + +with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市区日电量预测_1227.xlsx', mode='a', if_sheet_exists='replace', + engine='openpyxl') as writer: + result.to_excel(writer, sheet_name='杭州') \ No newline at end of file diff --git a/各地级市日电量模型/温州.py b/各地级市日电量模型/温州.py index d089df9..36ceaca 100644 --- a/各地级市日电量模型/温州.py +++ b/各地级市日电量模型/温州.py @@ -27,10 +27,10 @@ def normal(nd): parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) data = pd.read_excel(os.path.join(parent_dir, '入模数据/温州.xlsx')) -data['dtdate'] = pd.to_datetime(data['dtdate'],format='%Y-%m-%d') +data['dtdate'] = pd.to_datetime(data['dtdate'], format='%Y-%m-%d') data['year'] = data['dtdate'].dt.year # data.index = pd.to_datetime(data.index, format='%Y-%m-%d') -data.set_index('dtdate',inplace=True) +data.set_index('dtdate', inplace=True) data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -48,23 +48,18 @@ data = data.loc[normal(data['售电量']).index] # print(list0,list1,list2) data['season'] = data.index.map(season) -# data = data.loc[:'2023-8'] -df_eval = data.loc['2023-11'] +df_eval = data.loc['2023-12'] + +df_train = data[-180:] -df_train = data[450:] -# df_train = data[450:][:-3] print(df_train) -df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season','year']] +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season', 'year']] -X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] -X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] y = df_train['售电量'] -# best_goal = 1 -# best_i = {} -# for i in range(400): - x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) model.fit(x_train, y_train) @@ -84,27 +79,24 @@ print(goal) goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum() print(goal2) print(result_eval) -# if abs(goal) < best_goal : -# best_goal = abs(goal) -# best_i['best_i'] = i -# x = goal2 -# print(best_i,best_goal,x) -# 保存模型 -model.save_model('wenzhou.bin') - -loaded_model = xgb.XGBRegressor() -loaded_model.load_model('wenzhou.bin') import numpy as np X_eval = np.array([ - [19.8, 6.6, 10, 0, 1,2023], - [22, 6.1, 10, 0, 1,2023], - [18.5, 10.1, 10, 0, 1,2023] + [13.7, -0.1, 0, 0, 1, 2023], + [18.9, 3.4, 0, 0, 1, 2023], + [16.3, 11.3, 0, 0, 1, 2023], + [16.6, 9.7, 0, 0, 1, 2023], + [20.6, 20.2, 0, 1, 1, 2023] ]) print(model.predict(X_eval)) result = model.predict(X_eval) -result = pd.DataFrame(result,index=['2023-11-28','2023-11-29','2023-11-30']) -result = pd.concat((result_eval['eval'],result)) -result.index = result.index.map(lambda x:str(x)[:10]) -print(result) \ No newline at end of file +result = pd.DataFrame(result, index=['2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29', '2023-12-30']) +result = pd.concat((result_eval['eval'], result)) +result.index = result.index.map(lambda x: str(x)[:10]) +result.columns = ['预测值'] +print(result) + +with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市区日电量预测_1227.xlsx', mode='a', if_sheet_exists='replace', + engine='openpyxl') as writer: + result.to_excel(writer, sheet_name='温州') diff --git a/各地级市日电量模型/湖州.py b/各地级市日电量模型/湖州.py index 8f685a6..23eab02 100644 --- a/各地级市日电量模型/湖州.py +++ b/各地级市日电量模型/湖州.py @@ -21,17 +21,16 @@ def season(x): 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%']) + high = nd.describe()['75%'] + 3 * (nd.describe()['75%'] - nd.describe()['25%']) + low = nd.describe()['25%'] - 3 * (nd.describe()['75%'] - nd.describe()['25%']) return nd[(nd < high) & (nd > low)] parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) data = pd.read_excel(os.path.join(parent_dir, '入模数据/湖州.xlsx')) -data['dtdate'] = pd.to_datetime(data['dtdate'],format='%Y-%m-%d') +data['dtdate'] = pd.to_datetime(data['dtdate'], format='%Y-%m-%d') data['year'] = data['dtdate'].dt.year -# data.index = pd.to_datetime(data.index, format='%Y-%m-%d') -data.set_index('dtdate',inplace=True) +data.set_index('dtdate', inplace=True) data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -49,16 +48,16 @@ data = data.loc[normal(data['售电量']).index] # print(list0,list1,list2) data['season'] = data.index.map(season) -# data = data.loc[:'2023-9'] -df_eval = data.loc['2023-11'] -df_train = data[450:-1] -# df_train = data[450:][:-3] +df_eval = data.loc['2023-12'] + +df_train = data[-180:] + print(df_train) -df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season','year']] +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season', 'year']] -X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] -X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] y = df_train['售电量'] x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=158) @@ -79,21 +78,26 @@ print(goal) goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum() print(goal2) print(result_eval) -# 保存模型 -model.save_model('huzhou.bin') -loaded_model = xgb.XGBRegressor() -loaded_model.load_model('huzhou.bin') + + import numpy as np X_eval = np.array([ - [14.9, 7.1, 10, 0, 1,2023], - [17.7, 6.6, 10, 0, 1,2023], - [10.3, 5.8, 10, 0, 1,2023] + [9.7, -2.3, 0, 0, 1, 2023], + [13.1, 0.8, 0, 0, 1, 2023], + [10.3, 5.8, 0, 0, 1, 2023], + [10.1, 0.1, 0, 0, 1, 2023], + [12.8, 5.1, 0, 1, 1, 2023] ]) print(model.predict(X_eval)) result = model.predict(X_eval) -result = pd.DataFrame(result,index=['2023-11-28','2023-11-29','2023-11-30']) -result = pd.concat((result_eval['eval'],result)) -result.index = result.index.map(lambda x:str(x)[:10]) +result = pd.DataFrame(result, index=['2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29', '2023-12-30']) +result = pd.concat((result_eval['eval'], result)) +result.index = result.index.map(lambda x: str(x)[:10]) +result.columns = ['预测值'] print(result) + +with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市区日电量预测_1227.xlsx', mode='a', if_sheet_exists='replace', + engine='openpyxl') as writer: + result.to_excel(writer, sheet_name='湖州') \ No newline at end of file diff --git a/各地级市日电量模型/绍兴.py b/各地级市日电量模型/绍兴.py index 915af09..0af9249 100644 --- a/各地级市日电量模型/绍兴.py +++ b/各地级市日电量模型/绍兴.py @@ -3,10 +3,8 @@ import pandas as pd import os from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split -import matplotlib as mpl -import matplotlib.pyplot as plt -mpl.rcParams['font.sans-serif'] = ['kaiti'] + pd.set_option('display.width', None) @@ -18,18 +16,15 @@ def season(x): else: return 2 - 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)] - parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) data = pd.read_excel(os.path.join(parent_dir, '入模数据/绍兴.xlsx')) data['dtdate'] = pd.to_datetime(data['dtdate'],format='%Y-%m-%d') data['year'] = data['dtdate'].dt.year -# data.index = pd.to_datetime(data.index, format='%Y-%m-%d') data.set_index('dtdate',inplace=True) data = data.loc[normal(data['售电量']).index] @@ -48,10 +43,10 @@ data = data.loc[normal(data['售电量']).index] # print(list0,list1,list2) data['season'] = data.index.map(season) -df_eval = data.loc['2023-11'] -# data = data.loc[:'2023-8'] -df_train = data[450:] -# df_train = data[450:][:-3] +df_eval = data.loc['2023-12'] + +df_train = data[-180:] + print(df_train) df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season','year']] @@ -80,20 +75,25 @@ goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / re print(goal2) print(result_eval) -# # 保存模型 -# model.save_model('shaoxing.bin') -# loaded_model = xgb.XGBRegressor() -# loaded_model.load_model('shaoxing.bin') + import numpy as np X_eval = np.array([ - [17.4, 6.6, 10, 0, 0,2023], - [21.2, 7, 10, 0, 0,2023], - [12.1, 7.3, 10, 0, 0,2023] + + [13.1, -3.5, 0, 0, 1, 2023], + [15.6, 0.1, 0, 0, 1, 2023], + [13.2, 3.6, 0, 0, 1, 2023], + [14.4, 3.2, 0, 0, 1, 2023], + [15.5, 5.3, 0, 1, 1, 2023] ]) print(model.predict(X_eval)) result = model.predict(X_eval) -result = pd.DataFrame(result,index=['2023-11-28','2023-11-29','2023-11-30']) +result = pd.DataFrame(result, index=['2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29', '2023-12-30']) result = pd.concat((result_eval['eval'],result)) result.index = result.index.map(lambda x:str(x)[:10]) -print(result) \ No newline at end of file +result.columns = ['预测值'] +print(result) + +with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市区日电量预测_1227.xlsx', mode='a', if_sheet_exists='replace', + engine='openpyxl') as writer: + result.to_excel(writer, sheet_name='绍兴') \ No newline at end of file diff --git a/各地级市日电量模型/舟山.py b/各地级市日电量模型/舟山.py index 520fc49..3a4f6df 100644 --- a/各地级市日电量模型/舟山.py +++ b/各地级市日电量模型/舟山.py @@ -7,8 +7,6 @@ import matplotlib as mpl mpl.rcParams['font.sans-serif'] = ['kaiti'] pd.set_option('display.width', None) -import random -import matplotlib.pyplot as plt def season(x): @@ -26,9 +24,9 @@ def normal(nd): parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) data = pd.read_excel(os.path.join(parent_dir, '入模数据/舟山.xlsx')) -data['dtdate'] = pd.to_datetime(data['dtdate'],format='%Y-%m-%d') +data['dtdate'] = pd.to_datetime(data['dtdate'], format='%Y-%m-%d') data['year'] = data['dtdate'].dt.year -data.set_index('dtdate',inplace=True) +data.set_index('dtdate', inplace=True) data = data.loc[normal(data['售电量']).index] # list2 = [] # list0 = [] @@ -45,14 +43,12 @@ data = data.loc[normal(data['售电量']).index] # print(list0,list1,list2) data['season'] = data.index.map(season) -df_eval = data.loc['2023-11'] -# data = data.loc[:'2023-8'] +df_eval = data.loc['2023-12'] df_train = data.iloc[450:] -# df_train = data.iloc[450:][:-3] -df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season','year']] -X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] -X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season', 'year']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']] y = df_train['售电量'] x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42) @@ -75,19 +71,23 @@ goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / re print(goal2) print(result_eval) -model.save_model('zhoushan.bin') -loaded_model = xgb.XGBRegressor() -loaded_model.load_model('zhoushan.bin') import numpy as np X_eval = np.array([ - [14.7, 11.4, 10, 0, 1,2023], - [19.4, 11.8, 10, 0, 1,2023], - [14.9, 9.4, 10, 0, 1,2023] + [11.4, 5.1, 0, 0, 1, 2023], + [15.5, 9.1, 0, 0, 1, 2023], + [11, 7.9, 0, 0, 1, 2023], + [12.7, 7.2, 0, 0, 1, 2023], + [16, 10.3, 0, 1, 1, 2023] ]) print(model.predict(X_eval)) result = model.predict(X_eval) -result = pd.DataFrame(result,index=['2023-11-28','2023-11-29','2023-11-30']) -result = pd.concat((result_eval['eval'],result)) -result.index = result.index.map(lambda x:str(x)[:10]) -print(result) \ No newline at end of file +result = pd.DataFrame(result, index=['2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29', '2023-12-30']) +result = pd.concat((result_eval['eval'], result)) +result.index = result.index.map(lambda x: str(x)[:10]) +result.columns = ['预测值'] +print(result) + +with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市区日电量预测_1227.xlsx', mode='a', if_sheet_exists='replace', + engine='openpyxl') as writer: + result.to_excel(writer, sheet_name='舟山') diff --git a/各地级市日电量模型/衢州.py b/各地级市日电量模型/衢州.py index 414ef87..d3c8985 100644 --- a/各地级市日电量模型/衢州.py +++ b/各地级市日电量模型/衢州.py @@ -3,9 +3,6 @@ import pandas as pd import os from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split -import matplotlib as mpl -import matplotlib.pyplot as plt -mpl.rcParams['font.sans-serif']=['kaiti'] pd.set_option('display.width',None) def season(x): if str(x)[5:7] in ('01', '02'): @@ -42,12 +39,10 @@ data = data.loc[normal(data['售电量']).index] # # print(list0,list1,list2) data['season'] = data.index.map(season) -# data = data.loc[:'2023-8'] - -df_eval = data.loc['2023-11'] -df_train = data.iloc[450:] -# df_train = data.iloc[450:-3] +df_eval = data.loc['2023-12'] +df_train = data.iloc[-180:] +print(df_train) df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season','year']] @@ -71,23 +66,26 @@ print(goal) goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum() print(goal2) print(result_eval) -# 保存模型 -model.save_model('quzhou.bin') -loaded_model = xgb.XGBRegressor() -loaded_model.load_model('quzhou.bin') + import numpy as np X_eval = np.array([ - [18.7, 7, 10, 0, 1,2023], - [20.2, 6.5, 10, 0, 1,2023], - [11.2, 8, 10, 0, 1,2023] + [11.7, -0.8, 0, 0, 1, 2023], + [15.2, 3.0, 0, 0, 1, 2023], + [14.8, 5.3, 0, 0, 1, 2023], + [15.1, 5.7, 0, 0, 1, 2023], + [17.5, 7.5, 0, 1, 1, 2023] ]) print(model.predict(X_eval)) result = model.predict(X_eval) -result = pd.DataFrame(result,index=['2023-11-28','2023-11-29','2023-11-30']) +result = pd.DataFrame(result, index=['2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29', '2023-12-30']) result = pd.concat((result_eval['eval'],result)) result.index = result.index.map(lambda x:str(x)[:10]) +result.columns = ['预测值'] print(result) +with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市区日电量预测_1227.xlsx', mode='a', if_sheet_exists='replace', + engine='openpyxl') as writer: + result.to_excel(writer, sheet_name='衢州') # import torch # from torch import nn diff --git a/各地级市日电量模型/金华.py b/各地级市日电量模型/金华.py index 4aa2869..02811ca 100644 --- a/各地级市日电量模型/金华.py +++ b/各地级市日电量模型/金华.py @@ -3,9 +3,6 @@ import pandas as pd import os from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split -import matplotlib as mpl -import matplotlib.pyplot as plt -mpl.rcParams['font.sans-serif']=['kaiti'] pd.set_option('display.width',None) def season(x): @@ -41,11 +38,10 @@ data = data.loc[normal(data['售电量']).index] # print(list0,list1,list2) data['season'] = data.index.map(season) -data = data.loc[:'2023-11'] -df_eval = data.loc['2023-11'] +data = data.loc[:'2023-12'] +df_eval = data.loc['2023-12'] -# df_train = data.iloc[450:-1] -df_train = data.iloc[450:] +df_train = data.iloc[-180:] print(df_train) df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] @@ -54,9 +50,6 @@ X = df_train[['tem_max','tem_min','24ST','holiday','season']] X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']] y = df_train['售电量'] -# best_goal = 1 -# best_i = {} -# for i in range(400): x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=100) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) @@ -78,24 +71,23 @@ goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result print(goal2) print(result_eval) - # if abs(goal) < best_goal : - # best_goal = abs(goal) - # best_i['best_i'] = i - # x = goal2 -# # 保存模型 -model.save_model('jinhua.bin') -loaded_model = xgb.XGBRegressor() -loaded_model.load_model('jinhua.bin') import numpy as np X_eval = np.array([ - [19.5, 7.6, 10, 0, 1], - [21.7, 6.8, 10, 0, 1], - [11.6, 8.2, 10, 0, 1] + [11.7, -0.8, 0, 0, 1], + [15.2, 3.0, 0, 0, 1], + [14.8, 5.3, 0, 0, 1], + [15.1, 5.7, 0, 0, 1], + [17.5, 7.5, 0, 1, 1] ]) print(model.predict(X_eval)) result = model.predict(X_eval) -result = pd.DataFrame(result,index=['2023-11-28','2023-11-29','2023-11-30']) +result = pd.DataFrame(result, index=['2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29', '2023-12-30']) result = pd.concat((result_eval['eval'],result)) result.index = result.index.map(lambda x:str(x)[:10]) -print(result) \ No newline at end of file +result.columns = ['预测值'] +print(result) + +with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市区日电量预测_1227.xlsx', mode='a', if_sheet_exists='replace', + engine='openpyxl') as writer: + result.to_excel(writer, sheet_name='金华') \ No newline at end of file diff --git a/浙江电压等级电量/分压_移动平均.py b/浙江电压等级电量/分压_移动平均.py index b788cb1..6fffe55 100644 --- a/浙江电压等级电量/分压_移动平均.py +++ b/浙江电压等级电量/分压_移动平均.py @@ -1,47 +1,55 @@ import pandas as pd -df = pd.read_excel(r'C:\Users\鸽子\Desktop\浙江电量20231202.xlsx', sheet_name=1) + +df = pd.read_excel(r'C:\Users\鸽子\Desktop\浙江power1225.xlsx', sheet_name=1) df['pt_date'] = pd.to_datetime(df['pt_date']) # 移动平均 - dict_big = {} dict_ok = {} -# for city in df['city_name'].drop_duplicates(): -# -# df_city1 = df[(df['city_name'] == city) & (df['county_name'].isnull())].set_index('pt_date').loc['2023-11'] -# resut_df = pd.DataFrame({}) -# index_level = [] -# tq_list = [] -# pred_list = [] -# loss_list = [] -# rate_list = [] -# for level in df_city1.columns[2:]: -# -# index_level.append(level) -# -# df_moving_avg = pd.DataFrame(df_city1[:-3][level], index=df_city1[:-3].index) -# future = pd.date_range(start=df_city1.index[-3], periods=3, freq='D') -# -# for date in future: -# df_moving_avg.loc[date, level] = df_moving_avg[-3:].mean().values -# loss = (df_city1[level].tail(-3).sum() - df_moving_avg.tail(-3).sum()) / df_city1[level].sum() -# tq_list.append(df_city1[level].sum()) -# pred_list.append(df_moving_avg[level].sum()) -# loss_list.append(df_city1[level].sum()-df_moving_avg[level].sum()) -# rate_list.append((df_city1[level].sum()-df_moving_avg[level].sum())/df_city1[level].sum()) -# resut_df = pd.DataFrame({'同期电量':tq_list,'预测电量':pred_list,'偏差':loss_list,'偏差率':rate_list},index=index_level) -# with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\11月移动平均分压.xlsx',mode='a',if_sheet_exists='replace',engine='openpyxl') as writer: -# resut_df.to_excel(writer,sheet_name=f'{city[4:6]}') - -excel_file = pd.ExcelFile(r'C:\Users\鸽子\Desktop\11月移动平均分压.xlsx') -df1 = pd.read_excel(excel_file,sheet_name=1) -df1.set_index(df1.columns[0],inplace=True) -for sheet in excel_file.sheet_names[2:]: - df = pd.read_excel(excel_file,sheet_name=sheet) - df.set_index(df.columns[0],inplace=True) - df1 += df -df1['偏差'] = df1['同期电量']-df1['预测电量'] -df1['偏差率'] = df1['偏差']/df1['同期电量'] -df1.to_excel('移动平均_11月分压汇总.xlsx') -print(df1) \ No newline at end of file +for city in df['city_name'].drop_duplicates(): + + df_city1 = df[(df['city_name'] == city) & (df['county_name'].isnull())].set_index('pt_date').loc['2023-12'] + resut_df = pd.DataFrame({}) + index_level = [] + tq_list = [] + pred_list = [] + loss_list = [] + rate_list = [] + + for level in df_city1.columns[2:]: + + # index_level.append(level) + df_moving_avg = pd.DataFrame(df_city1[level], index=df_city1.index) + future = pd.date_range(start='2023-12-26', periods=5, freq='D') + + for date in future: + df_moving_avg.loc[date, level] = df_moving_avg[-3:].mean().values + resut_df = pd.concat([resut_df, df_moving_avg], axis=1) + print(city[4:6]) + print(resut_df) + + # loss = (df_city1[level].tail(-3).sum() - df_moving_avg.tail(-3).sum()) / df_city1[level].sum() + # tq_list.append(df_city1[level].sum()) + # pred_list.append(df_moving_avg[level].sum()) + # loss_list.append(df_city1[level].sum() - df_moving_avg[level].sum()) + # rate_list.append((df_city1[level].sum() - df_moving_avg[level].sum()) / df_city1[level].sum()) + # resut_df = pd.DataFrame({'同期电量': tq_list, '预测电量': pred_list, '偏差': loss_list, '偏差率': rate_list}, + # index=index_level) + # resut_df = pd.DataFrame({'预测电量': pred_list},index=index_level) + + with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市分压电量预测_1227.xlsx', mode='a', if_sheet_exists='replace', + engine='openpyxl') as writer: + resut_df.to_excel(writer, sheet_name=f'{city[4:6]}') + +# excel_file = pd.ExcelFile(r'C:\Users\鸽子\Desktop\11月移动平均分压.xlsx') +# df1 = pd.read_excel(excel_file, sheet_name=1) +# df1.set_index(df1.columns[0], inplace=True) +# for sheet in excel_file.sheet_names[2:]: +# df = pd.read_excel(excel_file, sheet_name=sheet) +# df.set_index(df.columns[0], inplace=True) +# df1 += df +# df1['偏差'] = df1['同期电量'] - df1['预测电量'] +# df1['偏差率'] = df1['偏差'] / df1['同期电量'] +# df1.to_excel('移动平均_11月分压汇总.xlsx') +# print(df1) diff --git a/浙江行业电量/移动平均.py b/浙江行业电量/移动平均.py deleted file mode 100644 index 3594da5..0000000 --- a/浙江行业电量/移动平均.py +++ /dev/null @@ -1,42 +0,0 @@ -import pandas as pd - -df = pd.read_excel(r'C:\Users\鸽子\Desktop\浙江电量20231202.xlsx', sheet_name=2) -df['stat_date'] = pd.to_datetime(df['stat_date']) - - -# 移动平均 -city = df['city_name'].iloc[0] -print(city) -df_city1 = df[df['city_name'] == city].set_index('stat_date').loc['2023-11'] - -dict_big = {} -dict_ok = {} -resut_df = pd.DataFrame({}) -index_industry = [] -tq_list = [] -pred_list = [] -loss_list = [] -rate_list = [] -for industry in df_city1.columns[1:]: - index_industry.append(industry) - - df_moving_avg = pd.DataFrame(df_city1[:-3][industry], index=df_city1[:-3].index) - future = pd.date_range(start=df_city1.index[-3], periods=3, freq='D') - - for date in future: - df_moving_avg.loc[date, industry] = df_moving_avg[-3:].mean().values - loss = (df_city1[industry].tail(-3).sum() - df_moving_avg.tail(-3).sum()) / df_city1[industry].sum() - tq_list.append(df_city1[industry].sum()) - pred_list.append(df_moving_avg[industry].sum()) - loss_list.append(df_city1[industry].sum()-df_moving_avg[industry].sum()) - rate_list.append((df_city1[industry].sum()-df_moving_avg[industry].sum())/df_city1[industry].sum()) -resut_df = pd.DataFrame({'同期电量':tq_list,'预测电量':pred_list,'偏差':loss_list,'偏差率':rate_list},index=index_industry) -print(resut_df) -resut_df.to_excel(r'C:\Users\鸽子\Desktop\移动平均_丽水_行业.xlsx') - -# if loss.values >= 0.005: -# dict_big[industry] = loss.values[0] -# else: -# dict_ok[industry] = loss.values[0] -# print(len(dict_ok)) -# print(len(dict_big)) \ No newline at end of file diff --git a/浙江行业电量/行业_移动平均.py b/浙江行业电量/行业_移动平均.py new file mode 100644 index 0000000..27bc756 --- /dev/null +++ b/浙江行业电量/行业_移动平均.py @@ -0,0 +1,49 @@ +import pandas as pd + +df = pd.read_excel(r'C:\Users\鸽子\Desktop\浙江power1225.xlsx', sheet_name=2) +df['stat_date'] = pd.to_datetime(df['stat_date']) + +# 移动平均 +for city in df['city_name'].drop_duplicates(): + + print(city) + df_city = df[df['city_name'] == city].set_index('stat_date').loc['2023-12'] + + dict_big = {} + dict_ok = {} + resut_df = pd.DataFrame({}) + index_industry = [] + tq_list = [] + pred_list = [] + loss_list = [] + rate_list = [] + + for industry in df_city.columns[2:]: + # index_industry.append(industry) + df_moving_avg = pd.DataFrame(df_city[industry], index=df_city.index) + future = pd.date_range(start='2023-12-26', periods=5, freq='D') + + for date in future: + df_moving_avg.loc[date, industry] = df_moving_avg[-3:].mean().values + resut_df = pd.concat([resut_df, df_moving_avg], axis=1) + print(city[4:6]) + print(resut_df) + # loss = (df_city1[industry].tail(-3).sum() - df_moving_avg.tail(-3).sum()) / df_city1[industry].sum() + # tq_list.append(df_city1[industry].sum()) + # pred_list.append(df_moving_avg[industry].sum()) + # loss_list.append(df_city1[industry].sum()-df_moving_avg[industry].sum()) + # rate_list.append((df_city1[industry].sum()-df_moving_avg[industry].sum())/df_city1[industry].sum()) + with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\行业电量预测_1227.xlsx', mode='a', if_sheet_exists='replace', + engine='openpyxl') as writer: + resut_df.to_excel(writer, sheet_name=f'{city[4:6]}') + +# resut_df = pd.DataFrame({'同期电量':tq_list,'预测电量':pred_list,'偏差':loss_list,'偏差率':rate_list},index=index_industry) +# print(resut_df) +# resut_df.to_excel(r'C:\Users\鸽子\Desktop\移动平均_丽水_行业.xlsx') + +# if loss.values >= 0.005: +# dict_big[industry] = loss.values[0] +# else: +# dict_ok[industry] = loss.values[0] +# print(len(dict_ok)) +# print(len(dict_big))