输出预测结果

main
鸽子 1 year ago
parent 71bc236f76
commit fcff6a72f3

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -27,10 +27,9 @@ def normal(nd):
parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
data = pd.read_excel(os.path.join(parent_dir, '入模数据/丽水.xlsx')) 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['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] data = data.loc[normal(data['售电量']).index]
# list2 = [] # list2 = []
@ -50,20 +49,18 @@ data = data.loc[normal(data['售电量']).index]
data['season'] = data.index.map(season) 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.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 = 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()['售电量']) print(df_train.corr()['售电量'])
X = df_train[['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']] X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']]
y = df_train['售电量'] 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) 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 = 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) 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) 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['eval']) print(result_eval)
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)
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)
# 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 import numpy as np
X_eval = np.array([ X_eval = np.array([
[17.2, 5.7, 10, 0, 0,2023], [11.59, 0.6, 0, 0, 1, 2023],
[21.2, 4.3, 10, 0, 0,2023], [16.6, 2.4, 0, 0, 1, 2023],
[11.5, 6.6, 10, 0, 0,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)) print(model.predict(X_eval))
result = 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 = pd.concat((result_eval['eval'], result))
result.index = result.index.map(lambda x:str(x)[:10]) result.index = result.index.map(lambda x: str(x)[:10])
result.columns = ['预测值']
print(result) 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='丽水')

@ -5,9 +5,11 @@ from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
import matplotlib as mpl import matplotlib as mpl
import matplotlib.pyplot as plt 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): def season(x):
if str(x)[5:7] in ('01', '02', '10', '11'): if str(x)[5:7] in ('01', '02', '10', '11'):
@ -16,17 +18,19 @@ def season(x):
return 1 return 1
else: else:
return 2 return 2
def normal(nd): def normal(nd):
high = nd.describe()['75%'] + 1.5*(nd.describe()['75%']-nd.describe()['25%']) high = nd.describe()['75%'] + 1.5 * (nd.describe()['75%'] - nd.describe()['25%'])
low = nd.describe()['25%'] - 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)] return nd[(nd < high) & (nd > low)]
parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir)) parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
data = pd.read_excel(os.path.join(parent_dir,'入模数据/台州.xlsx')) 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['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] data = data.loc[normal(data['售电量']).index]
# list2 = [] # list2 = []
@ -43,58 +47,55 @@ data = data.loc[normal(data['售电量']).index]
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
# data = data.loc[:'2023-9']
df_eval = data.loc['2023-11']
df_train = data[500:-1] df_eval = data.loc['2023-12']
# df_train = data[500:][:-3]
df_train = data[-180:]
print(df_train) 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 = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']]
X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season','year']] X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']]
y = df_train['售电量'] 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 = 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) 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()) print(abs(y_test - y_pred).mean() / y_test.mean())
eval_pred = model.predict(X_eval) 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['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)
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)
print(result_eval) 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 import numpy as np
X_eval = np.array([ X_eval = np.array([
[18.8, 6.2, 10, 0, 0,2023], [13.1, -0.7, 0, 0, 1, 2023],
[21.7, 6.5, 10, 0, 0,2023], [17.9, 2.8, 0, 0, 1, 2023],
[14.3, 8.4, 10, 0, 0,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)) print(model.predict(X_eval))
result = 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 = pd.concat((result_eval['eval'], result))
result.index = result.index.map(lambda x:str(x)[:10]) result.index = result.index.map(lambda x: str(x)[:10])
result.columns = ['预测值']
print(result) 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='台州')

@ -3,10 +3,7 @@ import pandas as pd
import os import os
from sklearn.metrics import r2_score from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split 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) 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)) parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
data = pd.read_excel(os.path.join(parent_dir, '入模数据/嘉兴.xlsx')) 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['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] data = data.loc[normal(data['售电量']).index]
# list2 = [] # list2 = []
@ -46,22 +43,19 @@ data = data.loc[normal(data['售电量']).index]
# list1.append(i) # list1.append(i)
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
# data = data.loc[:'2023-9']
df_eval = data.loc['2023-11'] df_eval = data.loc['2023-12']
df_train = data.iloc[450:-1] df_train = data.iloc[-180:]
# df_train = data[450:][:-3]
print(df_train) 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 = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']]
X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']]
y = df_train['售电量'] 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) 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) 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() goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum()
print(goal, goal2) print(goal, goal2)
print(result_eval) 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') model.save_model('jiaxing.bin')
@ -93,13 +81,20 @@ loaded_model.load_model('jiaxing.bin')
import numpy as np import numpy as np
X_eval = np.array([ X_eval = np.array([
[14.5, 7.7, 10, 0, 1,2023], [10.7, -2.2, 0, 0, 1, 2023],
[21.6, 10, 10, 0, 1,2023], [12.8, 2.2, 0, 0, 1, 2023],
[11.9, 6.6, 10, 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)) print(model.predict(X_eval))
result = 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 = pd.concat((result_eval['eval'], result))
result.index = result.index.map(lambda x:str(x)[:10]) result.index = result.index.map(lambda x: str(x)[:10])
result.columns = ['预测值']
print(result) 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='嘉兴')

@ -3,10 +3,7 @@ import pandas as pd
import os import os
from sklearn.metrics import r2_score from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split 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) pd.set_option('display.width', None)
@ -20,17 +17,17 @@ def season(x):
def normal(nd): def normal(nd):
high = nd.describe()['75%'] + 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) high = nd.describe()['75%'] + 3 * (nd.describe()['75%'] - nd.describe()['25%'])
low = nd.describe()['25%'] - 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) low = nd.describe()['25%'] - 3 * (nd.describe()['75%'] - nd.describe()['25%'])
return nd[(nd < high) & (nd > low)] return nd[(nd < high) & (nd > low)]
parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
data = pd.read_excel(os.path.join(parent_dir, '入模数据/宁波.xlsx')) 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['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] data = data.loc[normal(data['售电量']).index]
# list2 = [] # list2 = []
@ -48,20 +45,17 @@ data = data.loc[normal(data['售电量']).index]
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
df_eval = data.loc['2023-11'] df_eval = data.loc['2023-12']
# df_train = data.loc['2022-01':'2023-09'][:-3] 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 = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']]
X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']]
y = df_train['售电量'] 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) 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) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150)
@ -83,19 +77,24 @@ print(goal2)
print(result_eval) print(result_eval)
# 保存模型
# model.save_model('ningbo.bin')
import numpy as np import numpy as np
X_eval = np.array([ X_eval = np.array([
[16.5, 6.8, 10, 0, 1,2023], [12.9, -2.9, 0, 0, 1, 2023],
[21.7, 6.8, 10, 0, 1,2023], [16.4, 3.4, 0, 0, 1, 2023],
[13, 8.8, 10, 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)) print(model.predict(X_eval))
result = 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 = pd.concat((result_eval['eval'], result))
result.index = result.index.map(lambda x:str(x)[:10]) result.index = result.index.map(lambda x: str(x)[:10])
result.columns = ['预测值']
print(result) 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='宁波')

@ -35,10 +35,10 @@ def normal(nd):
parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
data = pd.read_excel(os.path.join(parent_dir, '入模数据/杭州.xlsx')) 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['year'] = data['dtdate'].dt.year
# data.index = pd.to_datetime(data.index, format='%Y-%m-%d') # 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] data = data.loc[normal(data['售电量']).index]
# list2 = [] # list2 = []
@ -55,23 +55,17 @@ data = data.loc[normal(data['售电量']).index]
# print(list0,list1,list2) # 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['season'] = data.index.map(season)
# data = data.loc[:'2023-9']
df_train = data[500:-1] df_train = data[-180:]
# df_train = data[500:][:-3]
df_eval = data.loc['2023-11'] df_eval = data.loc['2023-12']
print(df_train) print(df_train)
X = df_train[['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']] X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']]
y = df_train['售电量'] 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) 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 = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150)
model.fit(x_train, y_train) 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('goal2:', goal2)
print(result_eval) print(result_eval)
print('r2:', r2_score(y_test, y_pred)) 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([ X_eval = np.array([
[17.2, 5.7, 10, 0, 0,2023], [11.8, -2.8, 0, 0, 1, 2023],
[21.2, 4.3, 10, 0, 0,2023], [15.1, 0.2, 0, 0, 1, 2023],
[11.5, 6.6, 10, 0, 0,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)) print(model.predict(X_eval))
result = 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 = pd.concat((result_eval['eval'], result))
result.index = result.index.map(lambda x:str(x)[:10]) result.index = result.index.map(lambda x: str(x)[:10])
result.columns = ['预测值']
print(result) 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='杭州')

@ -27,10 +27,10 @@ def normal(nd):
parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
data = pd.read_excel(os.path.join(parent_dir, '入模数据/温州.xlsx')) 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['year'] = data['dtdate'].dt.year
# data.index = pd.to_datetime(data.index, format='%Y-%m-%d') # 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] data = data.loc[normal(data['售电量']).index]
# list2 = [] # list2 = []
@ -48,23 +48,18 @@ data = data.loc[normal(data['售电量']).index]
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
# data = data.loc[:'2023-8'] df_eval = data.loc['2023-12']
df_eval = data.loc['2023-11']
df_train = data[-180:]
df_train = data[450:]
# df_train = data[450:][:-3]
print(df_train) 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 = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']]
X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']]
y = df_train['售电量'] 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) 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 = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150)
model.fit(x_train, y_train) 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() goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum()
print(goal2) print(goal2)
print(result_eval) 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 import numpy as np
X_eval = np.array([ X_eval = np.array([
[19.8, 6.6, 10, 0, 1,2023], [13.7, -0.1, 0, 0, 1, 2023],
[22, 6.1, 10, 0, 1,2023], [18.9, 3.4, 0, 0, 1, 2023],
[18.5, 10.1, 10, 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)) print(model.predict(X_eval))
result = 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 = pd.concat((result_eval['eval'], result))
result.index = result.index.map(lambda x:str(x)[:10]) result.index = result.index.map(lambda x: str(x)[:10])
result.columns = ['预测值']
print(result) 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='温州')

@ -21,17 +21,16 @@ def season(x):
def normal(nd): def normal(nd):
high = nd.describe()['75%'] + 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) high = nd.describe()['75%'] + 3 * (nd.describe()['75%'] - nd.describe()['25%'])
low = nd.describe()['25%'] - 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) low = nd.describe()['25%'] - 3 * (nd.describe()['75%'] - nd.describe()['25%'])
return nd[(nd < high) & (nd > low)] return nd[(nd < high) & (nd > low)]
parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
data = pd.read_excel(os.path.join(parent_dir, '入模数据/湖州.xlsx')) 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['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] data = data.loc[normal(data['售电量']).index]
# list2 = [] # list2 = []
@ -49,16 +48,16 @@ data = data.loc[normal(data['售电量']).index]
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
# data = data.loc[:'2023-9']
df_eval = data.loc['2023-11']
df_train = data[450:-1] df_eval = data.loc['2023-12']
# df_train = data[450:][:-3]
df_train = data[-180:]
print(df_train) 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 = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']]
X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season','year']] X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']]
y = df_train['售电量'] y = df_train['售电量']
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=158) 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() goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum()
print(goal2) print(goal2)
print(result_eval) print(result_eval)
# 保存模型
model.save_model('huzhou.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('huzhou.bin')
import numpy as np import numpy as np
X_eval = np.array([ X_eval = np.array([
[14.9, 7.1, 10, 0, 1,2023], [9.7, -2.3, 0, 0, 1, 2023],
[17.7, 6.6, 10, 0, 1,2023], [13.1, 0.8, 0, 0, 1, 2023],
[10.3, 5.8, 10, 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)) print(model.predict(X_eval))
result = 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 = pd.concat((result_eval['eval'], result))
result.index = result.index.map(lambda x:str(x)[:10]) result.index = result.index.map(lambda x: str(x)[:10])
result.columns = ['预测值']
print(result) 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='湖州')

@ -3,10 +3,8 @@ import pandas as pd
import os import os
from sklearn.metrics import r2_score from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split 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) pd.set_option('display.width', None)
@ -18,18 +16,15 @@ def season(x):
else: else:
return 2 return 2
def normal(nd): def normal(nd):
high = nd.describe()['75%'] + 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) high = nd.describe()['75%'] + 1.5 * (nd.describe()['75%'] - nd.describe()['25%'])
low = nd.describe()['25%'] - 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)] return nd[(nd < high) & (nd > low)]
parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
data = pd.read_excel(os.path.join(parent_dir, '入模数据/绍兴.xlsx')) 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['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] data = data.loc[normal(data['售电量']).index]
@ -48,10 +43,10 @@ data = data.loc[normal(data['售电量']).index]
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
df_eval = data.loc['2023-11'] df_eval = data.loc['2023-12']
# data = data.loc[:'2023-8']
df_train = data[450:] df_train = data[-180:]
# df_train = data[450:][:-3]
print(df_train) 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']]
@ -80,20 +75,25 @@ goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / re
print(goal2) print(goal2)
print(result_eval) print(result_eval)
# # 保存模型
# model.save_model('shaoxing.bin')
# loaded_model = xgb.XGBRegressor()
# loaded_model.load_model('shaoxing.bin')
import numpy as np import numpy as np
X_eval = np.array([ X_eval = np.array([
[17.4, 6.6, 10, 0, 0,2023],
[21.2, 7, 10, 0, 0,2023], [13.1, -3.5, 0, 0, 1, 2023],
[12.1, 7.3, 10, 0, 0,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)) print(model.predict(X_eval))
result = 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 = pd.concat((result_eval['eval'],result))
result.index = result.index.map(lambda x:str(x)[:10]) result.index = result.index.map(lambda x:str(x)[:10])
result.columns = ['预测值']
print(result) 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='绍兴')

@ -7,8 +7,6 @@ import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['kaiti'] mpl.rcParams['font.sans-serif'] = ['kaiti']
pd.set_option('display.width', None) pd.set_option('display.width', None)
import random
import matplotlib.pyplot as plt
def season(x): def season(x):
@ -26,9 +24,9 @@ def normal(nd):
parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
data = pd.read_excel(os.path.join(parent_dir, '入模数据/舟山.xlsx')) 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['year'] = data['dtdate'].dt.year
data.set_index('dtdate',inplace=True) data.set_index('dtdate', inplace=True)
data = data.loc[normal(data['售电量']).index] data = data.loc[normal(data['售电量']).index]
# list2 = [] # list2 = []
# list0 = [] # list0 = []
@ -45,14 +43,12 @@ data = data.loc[normal(data['售电量']).index]
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
df_eval = data.loc['2023-11'] df_eval = data.loc['2023-12']
# data = data.loc[:'2023-8']
df_train = data.iloc[450:] df_train = data.iloc[450:]
# df_train = data.iloc[450:][:-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']]
X = df_train[['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']] X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season', 'year']]
y = df_train['售电量'] y = df_train['售电量']
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42) 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(goal2)
print(result_eval) print(result_eval)
model.save_model('zhoushan.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('zhoushan.bin')
import numpy as np import numpy as np
X_eval = np.array([ X_eval = np.array([
[14.7, 11.4, 10, 0, 1,2023], [11.4, 5.1, 0, 0, 1, 2023],
[19.4, 11.8, 10, 0, 1,2023], [15.5, 9.1, 0, 0, 1, 2023],
[14.9, 9.4, 10, 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)) print(model.predict(X_eval))
result = 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 = pd.concat((result_eval['eval'], result))
result.index = result.index.map(lambda x:str(x)[:10]) result.index = result.index.map(lambda x: str(x)[:10])
result.columns = ['预测值']
print(result) 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='舟山')

@ -3,9 +3,6 @@ import pandas as pd
import os import os
from sklearn.metrics import r2_score from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split 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) pd.set_option('display.width',None)
def season(x): def season(x):
if str(x)[5:7] in ('01', '02'): if str(x)[5:7] in ('01', '02'):
@ -42,12 +39,10 @@ data = data.loc[normal(data['售电量']).index]
# #
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) 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']] 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() goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum()
print(goal2) print(goal2)
print(result_eval) print(result_eval)
# 保存模型
model.save_model('quzhou.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('quzhou.bin')
import numpy as np import numpy as np
X_eval = np.array([ X_eval = np.array([
[18.7, 7, 10, 0, 1,2023], [11.7, -0.8, 0, 0, 1, 2023],
[20.2, 6.5, 10, 0, 1,2023], [15.2, 3.0, 0, 0, 1, 2023],
[11.2, 8, 10, 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)) print(model.predict(X_eval))
result = 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 = pd.concat((result_eval['eval'],result))
result.index = result.index.map(lambda x:str(x)[:10]) result.index = result.index.map(lambda x:str(x)[:10])
result.columns = ['预测值']
print(result) 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 # import torch
# from torch import nn # from torch import nn

@ -3,9 +3,6 @@ import pandas as pd
import os import os
from sklearn.metrics import r2_score from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split 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) pd.set_option('display.width',None)
def season(x): def season(x):
@ -41,11 +38,10 @@ data = data.loc[normal(data['售电量']).index]
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
data = data.loc[:'2023-11'] data = data.loc[:'2023-12']
df_eval = data.loc['2023-11'] df_eval = data.loc['2023-12']
# df_train = data.iloc[450:-1] df_train = data.iloc[-180:]
df_train = data.iloc[450:]
print(df_train) print(df_train)
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] 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']] X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']]
y = df_train['售电量'] 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) 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) 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(goal2)
print(result_eval) 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 import numpy as np
X_eval = np.array([ X_eval = np.array([
[19.5, 7.6, 10, 0, 1], [11.7, -0.8, 0, 0, 1],
[21.7, 6.8, 10, 0, 1], [15.2, 3.0, 0, 0, 1],
[11.6, 8.2, 10, 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)) print(model.predict(X_eval))
result = 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 = pd.concat((result_eval['eval'],result))
result.index = result.index.map(lambda x:str(x)[:10]) result.index = result.index.map(lambda x:str(x)[:10])
result.columns = ['预测值']
print(result) 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='金华')

@ -1,47 +1,55 @@
import pandas as pd 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']) df['pt_date'] = pd.to_datetime(df['pt_date'])
# 移动平均 # 移动平均
dict_big = {} dict_big = {}
dict_ok = {} dict_ok = {}
# for city in df['city_name'].drop_duplicates(): 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'] df_city1 = df[(df['city_name'] == city) & (df['county_name'].isnull())].set_index('pt_date').loc['2023-12']
# resut_df = pd.DataFrame({}) resut_df = pd.DataFrame({})
# index_level = [] index_level = []
# tq_list = [] tq_list = []
# pred_list = [] pred_list = []
# loss_list = [] loss_list = []
# rate_list = [] rate_list = []
# for level in df_city1.columns[2:]:
# for level in df_city1.columns[2:]:
# index_level.append(level)
# # index_level.append(level)
# df_moving_avg = pd.DataFrame(df_city1[:-3][level], index=df_city1[:-3].index) df_moving_avg = pd.DataFrame(df_city1[level], index=df_city1.index)
# future = pd.date_range(start=df_city1.index[-3], periods=3, freq='D') future = pd.date_range(start='2023-12-26', periods=5, freq='D')
#
# for date in future: for date in future:
# df_moving_avg.loc[date, level] = df_moving_avg[-3:].mean().values 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() resut_df = pd.concat([resut_df, df_moving_avg], axis=1)
# tq_list.append(df_city1[level].sum()) print(city[4:6])
# pred_list.append(df_moving_avg[level].sum()) print(resut_df)
# 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()) # loss = (df_city1[level].tail(-3).sum() - df_moving_avg.tail(-3).sum()) / df_city1[level].sum()
# resut_df = pd.DataFrame({'同期电量':tq_list,'预测电量':pred_list,'偏差':loss_list,'偏差率':rate_list},index=index_level) # tq_list.append(df_city1[level].sum())
# with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\11月移动平均分压.xlsx',mode='a',if_sheet_exists='replace',engine='openpyxl') as writer: # pred_list.append(df_moving_avg[level].sum())
# resut_df.to_excel(writer,sheet_name=f'{city[4:6]}') # 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())
excel_file = pd.ExcelFile(r'C:\Users\鸽子\Desktop\11月移动平均分压.xlsx') # resut_df = pd.DataFrame({'同期电量': tq_list, '预测电量': pred_list, '偏差': loss_list, '偏差率': rate_list},
df1 = pd.read_excel(excel_file,sheet_name=1) # index=index_level)
df1.set_index(df1.columns[0],inplace=True) # resut_df = pd.DataFrame({'预测电量': pred_list},index=index_level)
for sheet in excel_file.sheet_names[2:]:
df = pd.read_excel(excel_file,sheet_name=sheet) with pd.ExcelWriter(r'C:\Users\鸽子\Desktop\市分压电量预测_1227.xlsx', mode='a', if_sheet_exists='replace',
df.set_index(df.columns[0],inplace=True) engine='openpyxl') as writer:
df1 += df resut_df.to_excel(writer, sheet_name=f'{city[4:6]}')
df1['偏差'] = df1['同期电量']-df1['预测电量']
df1['偏差率'] = df1['偏差']/df1['同期电量'] # excel_file = pd.ExcelFile(r'C:\Users\鸽子\Desktop\11月移动平均分压.xlsx')
df1.to_excel('移动平均_11月分压汇总.xlsx') # df1 = pd.read_excel(excel_file, sheet_name=1)
print(df1) # 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)

@ -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))

@ -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))
Loading…
Cancel
Save