更新入模数据

main
鸽子 1 year ago
parent 9d0daf370e
commit 25c7202b32

@ -6,48 +6,56 @@ 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'] mpl.rcParams['font.sans-serif']=['kaiti']
pd.set_option('display.width',None)
def season(x): def season(x):
if str(x)[5:7] in ('06','07','08','12','01','02'): if str(x)[5:7] in ('01', '10', '11'):
return 0
elif str(x)[5:7] in ('02', '03', '04', '05', '06', '09', '12'):
return 1 return 1
else: else:
return 0 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)]
pd.set_option('display.width',None)
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'),index_col='dtdate') data = pd.read_excel(os.path.join(parent_dir,'入模数据/丽水.xlsx'),index_col='dtdate')
data.index = pd.to_datetime(data.index,format='%Y-%m-%d') data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
data['season'] = data.index.map(season) data = data.loc[normal(data['售电量']).index]
print(data.head())
df_eval = data.loc['2023-9']
df_train = data.loc['2021-1':'2023-8']
# df_train = df[500:850]
print(len(df_eval),len(df_train),len(data))
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量','season']]
# IQR = df['售电量'].describe()['75%'] - df['售电量'].describe()['25%']
# high = df['售电量'].describe()['75%'] + 1.5*IQR
# low = df['售电量'].describe()['25%'] - 1.5*IQR
# print('异常值数量:',len(df[(df['售电量'] >= high) | (df['售电量'] <= low)]))
# df_train = df_train[(df['售电量'] <= high) & (df['售电量'] >= low)]
# list2 = [] # list2 = []
# list0 = [] # list0 = []
# list1 = [] # list1 = []
# for i in ('01','02','03','04','05','06','07','08','09','10','11','12'): # for i in ('01','02','03','04','05','06','07','08','09','10','11','12'):
# month_index = df.index.strftime('%Y-%m-%d').str[5:7] == f'{i}' # month_index = data.index.strftime('%Y-%m-%d').str[5:7] == f'{i}'
# if df.loc[month_index]['售电量'].mean() >= df['售电量'].describe()['75%']: # print(data.loc[month_index]['售电量'].max(),data['售电量'].describe()['75%'])
# if data.loc[month_index]['售电量'].mean() >= data['售电量'].describe()['75%']:
# list2.append(i) # list2.append(i)
# elif df.loc[month_index]['售电量'].mean() <= df['售电量'].describe()['25%']: # elif data.loc[month_index]['售电量'].mean() <= data['售电量'].describe()['25%']:
# list0.append(i) # list0.append(i)
# else: # else:
# list1.append(i) # list1.append(i)
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season)
print(data.head())
df_eval = data.loc['2023-9']
# df_train = data.loc['2021-1':'2023-8']
df_train = data[500:850]
print(len(df_eval),len(df_train),len(data))
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
X = df_train[['tem_max','tem_min','holiday','24ST','season']] X = df_train[['tem_max','tem_min','holiday','24ST','season']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']] X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']]
y = df_train['售电量'] y = df_train['售电量']
@ -56,7 +64,7 @@ y = df_train['售电量']
# for i in range(200): # for i in range(200):
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.15,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)
@ -68,12 +76,12 @@ 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)
# print((result_eval['eval'].sum()-result_eval['pred'].sum())/result_eval['eval'].sum())
print((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() goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum()
print((result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].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)
# if abs(goal) < best_goal: # if abs(goal) < best_goal:
# best_goal = abs(goal) # best_goal = abs(goal)
# best_i['best_i'] = i # best_i['best_i'] = i
@ -83,7 +91,7 @@ goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result
# with open(r'C:\Users\user\Desktop\9月各地市日电量预测结果\偏差率.txt','a',encoding='utf-8') as f: # with open(r'C:\Users\user\Desktop\9月各地市日电量预测结果\偏差率.txt','a',encoding='utf-8') as f:
# f.write(f'丽水月末3天偏差率{goal},9号-月底偏差率:{goal2}') # f.write(f'丽水月末3天偏差率{goal},9号-月底偏差率:{goal2}')
# # 保存模型 # # 保存模型
# model.save_model('lishui.bin') model.save_model('lishui.bin')
import numpy as np import numpy as np
loaded_model = xgb.XGBRegressor() loaded_model = xgb.XGBRegressor()
loaded_model.load_model('lishui.bin') loaded_model.load_model('lishui.bin')

@ -5,39 +5,47 @@ 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
def season(x):
if str(x)[5:7] in ('06','07','08','12','01','02'):
return 1
else:
return 0
mpl.rcParams['font.sans-serif']=['kaiti'] mpl.rcParams['font.sans-serif']=['kaiti']
pd.set_option('display.width',None) pd.set_option('display.width',None)
data = pd.read_excel(r'C:\Users\user\PycharmProjects\pytorch2\入模数据\台州数据(1).xlsx',index_col='dtdate') def season(x):
if str(x)[5:7] in ('01', '02', '10', '11'):
return 0
elif str(x)[5:7] in ('03', '04', '05', '06', '09', '12'):
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[(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'),index_col='dtdate')
data.index = pd.to_datetime(data.index,format='%Y-%m-%d') data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
data = data.loc[normal(data['售电量']).index]
list2 = []
list0 = []
list1 = []
for i in ('01','02','03','04','05','06','07','08','09','10','11','12'):
month_index = data.index.strftime('%Y-%m-%d').str[5:7] == f'{i}'
if data.loc[month_index]['售电量'].mean() >= data['售电量'].describe()['75%']:
list2.append(i)
elif data.loc[month_index]['售电量'].mean() <= data['售电量'].describe()['25%']:
list0.append(i)
else:
list1.append(i)
print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
# plt.plot(range(len(data)),data['售电量'])
# plt.show()
print(data.head())
df_eval = data.loc['2023-8'] df_eval = data.loc['2023-8']
# df_train = data.loc['2021-1':'2023-7']
df_train = data[500:850]
print(len(df_eval),len(df_train),len(data))
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量','season']] df_train = data[500:850]
# IQR = df['售电量'].describe()['75%'] - df['售电量'].describe()['25%']
# high = df['售电量'].describe()['75%'] + 1.5*IQR
# low = df['售电量'].describe()['25%'] - 1.5*IQR
# print('异常值数量:',len(df[(df['售电量'] >= high) | (df['售电量'] <= low)]))
#
# df_train = df_train[(df['售电量'] <= high) & (df['售电量'] >= low)]
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
X = df_train[['tem_max','tem_min','holiday','24ST','season']] X = df_train[['tem_max','tem_min','holiday','24ST','season']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']] X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']]
@ -60,22 +68,17 @@ eval_pred = model.predict(X_eval)
result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index) result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index)
# print(result_eval) # print(result_eval)
# print((result_eval['eval'].sum()-result_eval['pred'].sum())/result_eval['eval'].sum()) # print((result_eval['eval'].sum()-result_eval['pred'].sum())/result_eval['eval'].sum())
print((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() goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum()
print((result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].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)
# if abs(goal) < best_goal: # if abs(goal) < best_goal:
# best_goal = abs(goal) # best_goal = abs(goal)
# best_i['best_i'] = i # best_i['best_i'] = i
# print(best_i,best_goal) # print(best_i,best_goal)
# 保存模型
# model.save_model('taizhou.bin')
# 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('taizhou.bin')
import numpy as np import numpy as np
loaded_model = xgb.XGBRegressor() loaded_model = xgb.XGBRegressor()
loaded_model.load_model('taizhou.bin') loaded_model.load_model('taizhou.bin')

@ -5,41 +5,50 @@ 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)
def season(x): def season(x):
if str(x)[5:7] in ('06','07','08','12','01','02'): if str(x)[5:7] in ('04', '10'):
return 0
elif str(x)[5:7] in ('01', '02', '03', '05', '06', '09', '11', '12'):
return 1 return 1
else: else:
return 0 return 2
mpl.rcParams['font.sans-serif']=['kaiti']
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)]
pd.set_option('display.width',None)
parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir))
data = pd.read_excel(os.path.join(parent_dir,'入模数据/嘉兴.xlsx'),index_col='dtdate')
data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
data = data.loc[normal(data['售电量']).index]
df = pd.read_excel(r'C:\Users\user\PycharmProjects\pytorch2\入模数据\嘉兴数据.xlsx') # list2 = []
df['dtdate'] = pd.to_datetime(df['dtdate'],format='%Y-%m-%d').astype('string') # list0 = []
df.set_index('dtdate',inplace=True) # list1 = []
plt.plot(range(len(df)),df['售电量']) # for i in ('01','02','03','04','05','06','07','08','09','10','11','12'):
plt.show() # month_index = data.index.strftime('%Y-%m-%d').str[5:7] == f'{i}'
print(df.head()) # if data.loc[month_index]['售电量'].mean() >= data['售电量'].describe()['75%']:
df['season'] = df.index.map(season) # list2.append(i)
# elif data.loc[month_index]['售电量'].mean() <= data['售电量'].describe()['25%']:
# list0.append(i)
# else:
# list1.append(i)
# print(list0,list1,list2)
data['season'] = data.index.map(season)
df_eval = df[df.index.str[:7]=='2023-08'] df_eval = data.loc['2023-08']
# df_train = df[(df.index.str[:7]!='2023-08')&(df.index.str[:7]!='2023-09')] df_train = data.iloc[500:850]
df_train = df[500:850] # df_train = data.loc['2021-01':'2023-07']
print(len(df_eval),len(df_train),len(df)) print(len(df_eval),len(df_train),len(data))
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量','season']] df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
# IQR = df['售电量'].describe()['75%'] - df['售电量'].describe()['25%']
# high = df['售电量'].describe()['75%'] + 1.5*IQR
# low = df['售电量'].describe()['25%'] - 1.5*IQR
# print('异常值数量:',len(df[(df['售电量'] >= high) | (df['售电量'] <= low)]))
#
# df_train = df_train[(df['售电量'] <= high) & (df['售电量'] >= low)]
X = df_train[['tem_max','tem_min','holiday','24ST','season']] X = df_train[['tem_max','tem_min','holiday','24ST','season']]
@ -50,7 +59,7 @@ y = df_train['售电量']
# best_i = {} # best_i = {}
# for i in range(400): # for i in range(400):
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=272) 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)
@ -61,14 +70,11 @@ 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)
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)
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(goal2) # print(goal2)
# if abs(goal) < best_goal : # if abs(goal) < best_goal :
# best_goal = abs(goal) # best_goal = abs(goal)
@ -77,13 +83,8 @@ print(goal,goal2)
# #
# print(best_i,best_goal,x) # print(best_i,best_goal,x)
# result_eval.to_csv(r'C:\Users\user\Desktop\8月各地市日电量预测结果\嘉兴.csv')
# with open(r'C:\Users\user\Desktop\8月各地市日电量预测结果\偏差率.txt','a',encoding='utf-8') as f:
# f.write(f'嘉兴月末3天偏差率{round(goal,5)},9号-月底偏差率:{round(goal2,5)}\n')
# 保存模型 # 保存模型
# model.save_model('jiaxing.bin') model.save_model('jiaxing.bin')
loaded_model = xgb.XGBRegressor() loaded_model = xgb.XGBRegressor()
loaded_model.load_model('jiaxing.bin') loaded_model.load_model('jiaxing.bin')
import numpy as np import numpy as np

@ -5,42 +5,47 @@ 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
def season(x):
if str(x)[5:7] in ('06','07','08','12','01','02'):
return 1
else:
return 0
mpl.rcParams['font.sans-serif']=['kaiti'] mpl.rcParams['font.sans-serif']=['kaiti']
pd.set_option('display.width',None) pd.set_option('display.width',None)
def season(x):
df = pd.read_excel(r'C:\Users\user\PycharmProjects\pytorch2\入模数据\宁波数据.xlsx',index_col='dtdate') if str(x)[5:7] in ('01', '04', '10'):
df.index = pd.to_datetime(df.index,format='%Y-%m-%d') return 0
df['season'] = df.index.map(season) elif str(x)[5:7] in ('02', '03', '05', '06', '09', '11', '12'):
plt.plot(range(len(df)),df['售电量']) return 1
plt.show() else:
print(df.head()) return 2
def normal(nd):
high = nd.describe()['75%'] + 1.5*(nd.describe()['75%']-nd.describe()['25%'])
df_eval = df.loc['2023-09'] low = nd.describe()['25%'] - 1.5*(nd.describe()['75%']-nd.describe()['25%'])
df_train = df.loc['2021-01':'2023-08'] return nd[(nd<high)&(nd>low)]
# df_train = df[400:850]
print(len(df_eval),len(df_train),len(df)) parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir))
data = pd.read_excel(os.path.join(parent_dir,'入模数据/宁波.xlsx'),index_col='dtdate')
data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
data = data.loc[normal(data['售电量']).index]
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量','season']]
# list2 = []
# list0 = []
# IQR = df['售电量'].describe()['75%'] - df['售电量'].describe()['25%'] # list1 = []
# high = df['售电量'].describe()['75%'] + 1.5*IQR # for i in ('01','02','03','04','05','06','07','08','09','10','11','12'):
# low = df['售电量'].describe()['25%'] - 1.5*IQR # month_index = data.index.strftime('%Y-%m-%d').str[5:7] == f'{i}'
# print('异常值数量:',len(df[(df['售电量'] >= high) | (df['售电量'] <= low)])) # if data.loc[month_index]['售电量'].mean() >= data['售电量'].describe()['75%']:
# # list2.append(i)
# df_train = df_train[(df['售电量'] <= high) & (df['售电量'] >= low)] # elif data.loc[month_index]['售电量'].mean() <= data['售电量'].describe()['25%']:
# list0.append(i)
# else:
# list1.append(i)
# print(list0,list1,list2)
data['season'] = data.index.map(season)
df_eval = data.loc['2023-09']
df_train = data.loc['2021-01':'2023-08']
# df_train = data[400:850]
print(len(df_eval),len(df_train))
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
X = df_train[['tem_max','tem_min','holiday','24ST','season']] X = df_train[['tem_max','tem_min','holiday','24ST','season']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']] X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']]
@ -53,8 +58,6 @@ y = df_train['售电量']
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=18) x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=18)
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)
@ -76,12 +79,8 @@ print(goal2)
# #
# print(best_i,best_goal,x) # 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('ningbo.bin') model.save_model('ningbo.bin')
import numpy as np import numpy as np
loaded_model = xgb.XGBRegressor() loaded_model = xgb.XGBRegressor()
loaded_model.load_model('ningbo.bin') loaded_model.load_model('ningbo.bin')

@ -7,23 +7,15 @@ 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'] mpl.rcParams['font.sans-serif']=['kaiti']
pd.set_option('display.width',None) pd.set_option('display.width',None)
def hf_season(x):
list1= []
for i in range(1,13):
if x.loc[f'2021-{i}'].mean() >= x.describe()['75%']:
list1.append(i)
return list1
def season(x): def season(x):
if str(x)[5:7] in ('06','07','08','12','01','02'): if str(x)[5:7] in ('04', '10'):
return 0
elif str(x)[5:7] in ('01', '02', '03', '05', '06', '09', '11', '12'):
return 1 return 1
else: else:
return 0 return 2
def month(x): def month(x):
if str(x)[5:7] in ('08','09','10','12','01','02'): if str(x)[5:7] in ('08','09','10','12','01','02'):
return 1 return 1
@ -34,47 +26,45 @@ def normal(nd):
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))
data = pd.read_excel(r'C:\Users\user\PycharmProjects\pytorch2\入模数据\杭州数据.xlsx',index_col='dtdate') data = pd.read_excel(os.path.join(parent_dir,'入模数据/杭州.xlsx'),index_col='dtdate')
data.index = pd.to_datetime(data.index,format='%Y-%m-%d') data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
data = data.loc[normal(data['售电量']).index] data = data.loc[normal(data['售电量']).index]
# plt.plot(range(len(data['售电量']['2021':'2022'])),data['售电量']['2021':'2022'])
# plt.show()
# print(hf_season(data.loc['2021']['售电量']))
data['month'] = data.index.strftime('%Y-%m-%d').str[6] # list2 = []
data['month'] = data['month'].astype('int') # list0 = []
data['season'] = data.index.map(season) # list1 = []
print(data.tail(50)) # for i in ('01','02','03','04','05','06','07','08','09','10','11','12'):
# month_index = data.index.strftime('%Y-%m-%d').str[5:7] == f'{i}'
# if data.loc[month_index]['售电量'].mean() >= data['售电量'].describe()['75%']:
# list2.append(i)
# elif data.loc[month_index]['售电量'].mean() <= data['售电量'].describe()['25%']:
# list0.append(i)
# else:
# list1.append(i)
# print(list0,list1,list2)
df_eval = data.loc['2022-9':'2023-9']
df_train = data.loc['2021-1':'2022-8']
# df_train = df[500:850]
print(len(df_eval),len(df_train),len(data))
print(data.drop(columns='city_name').corr(method='pearson')['售电量']) # data['month'] = data.index.strftime('%Y-%m-%d').str[6]
# data['month'] = data['month'].astype('int')
data['season'] = data.index.map(season)
df_train = df_train[['tem_max','tem_min','24ST','rh','rh_max','prs','prs_max','prs_min','售电量','month','holiday','season']]
# df_train = df[500:850]
# IQR = df['售电量'].describe()['75%'] - df['售电量'].describe()['25%'] df_train = data.loc['2021-01':'2023-08']
# high = df['售电量'].describe()['75%'] + 1.5*IQR df_eval = data.loc['2023-9']
# low = df['售电量'].describe()['25%'] - 1.5*IQR
# print('异常值数量:',len(df[(df['售电量'] >= high) | (df['售电量'] <= low)]))
#
# df_train = df_train[(df['售电量'] <= high) & (df['售电量'] >= low)]
X = df_train[['tem_max','tem_min','24ST','holiday','season']] 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['售电量']
print(y.describe())
# best_goal = 1 # best_goal = 1
# best_i = {} # best_i = {}
# for i in range(400): # for i in range(400):
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42) x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,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)
model.fit(x_train,y_train) model.fit(x_train,y_train)
@ -83,20 +73,17 @@ 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)
# goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum()
# print((result_eval['eval'].sum()-result_eval['pred'].sum())/result_eval['eval'].sum()) print('goal:',goal)
#
# goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum() goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum()
# print('goal:',goal)
# print('goal2:',goal2)
# goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum() print(result_eval)
# print('r2:',r2_score(y_test,y_pred))
# print('goal2:',goal2)
# print(result_eval)
# print('r2:',r2_score(y_test,y_pred))
# #
# # result_eval.to_csv('asda.csv',encoding='gbk') # # result_eval.to_csv('asda.csv',encoding='gbk')
# # if abs(goal) < best_goal: # # if abs(goal) < best_goal:
@ -111,30 +98,17 @@ print(abs(y_test - y_pred).mean() / y_test.mean())
# # with open(r'C:\Users\user\Desktop\9月各地市日电量预测结果\偏差率.txt','a',encoding='utf-8') as f: # # with open(r'C:\Users\user\Desktop\9月各地市日电量预测结果\偏差率.txt','a',encoding='utf-8') as f:
# # f.write(f'杭州月末3天偏差率{round(goal,5)},9号-月底偏差率:{round(goal2,5)}\n') # # f.write(f'杭州月末3天偏差率{round(goal,5)},9号-月底偏差率:{round(goal2,5)}\n')
# 保存模型 # 保存模型
# model.save_model('hangzhou.bin') model.save_model('hangzhou.bin')
# X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']] # 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 = pd.read_excel(r'C:\Users\user\Desktop\浙江气象1027.xlsx')
df_eval.columns = df_eval.columns.map(lambda x:x.strip()) # df_eval.columns = df_eval.columns.map(lambda x:x.strip())
df_eval = df_eval[['city_name','dtdate','tem_max','tem_min']] # 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_eval['city_name'] = df_eval['city_name'].map(lambda x:x.strip())
df_hangzhou = df_eval[df_eval['city_name']=='金华市'].sort_values(by='dtdate') # df_hangzhou = df_eval[df_eval['city_name']=='金华市'].sort_values(by='dtdate')
# list2 = []
# list0 = []
# list1 = []
# for i in ('01','02','03','04','05','06','07','08','09','10','11','12'):
# month_index = df.index.strftime('%Y-%m-%d').str[5:7] == f'{i}'
# if df.loc[month_index]['售电量'].mean() >= df['售电量'].describe()['75%']:
# list2.append(i)
# elif df.loc[month_index]['售电量'].mean() <= df['售电量'].describe()['25%']:
# list0.append(i)
# else:
# list1.append(i)
# print(list0,list1,list2)
print(df_hangzhou)
loaded_model = xgb.XGBRegressor() loaded_model = xgb.XGBRegressor()
loaded_model.load_model('hangzhou.bin') loaded_model.load_model('hangzhou.bin')

@ -5,41 +5,50 @@ 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
def season(x):
if str(x)[5:7] in ('06','07','08','12','01','02'):
return 1
else:
return 0
mpl.rcParams['font.sans-serif']=['kaiti'] mpl.rcParams['font.sans-serif']=['kaiti']
pd.set_option('display.width',None) pd.set_option('display.width',None)
def season(x):
df = pd.read_excel(r'C:\Users\user\PycharmProjects\pytorch2\入模数据\温州数据(1).xlsx',index_col='dtdate') if str(x)[5:7] in ('01', '02', '10'):
df.index = pd.to_datetime(df.index,format='%Y-%m-%d') return 0
df['season'] = df.index.map(season) elif str(x)[5:7] in ('03', '04', '05', '06', '11', '12'):
plt.plot(range(len(df)),df['售电量']) return 1
plt.show() else:
print(df.head()) 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'),index_col='dtdate')
data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
data = data.loc[normal(data['售电量']).index]
# list2 = []
# list0 = []
# list1 = []
# for i in ('01','02','03','04','05','06','07','08','09','10','11','12'):
# month_index = data.index.strftime('%Y-%m-%d').str[5:7] == f'{i}'
# if data.loc[month_index]['售电量'].mean() >= data['售电量'].describe()['75%']:
# list2.append(i)
# elif data.loc[month_index]['售电量'].mean() <= data['售电量'].describe()['25%']:
# list0.append(i)
# else:
# list1.append(i)
#
# print(list0,list1,list2)
data['season'] = data.index.map(season)
df_eval = df.loc['2023-9'] df_eval = data.loc['2023-9']
# df_train = df[(df.index.str[:7]!='2023-08')&(df.index.str[:7]!='2023-09')] # df_train = df[(df.index.str[:7]!='2023-08')&(df.index.str[:7]!='2023-09')]
df_train = df[400:850] df_train = data[400:850]
print(len(df_eval),len(df_train),len(df))
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量','season']]
# IQR = df['售电量'].describe()['75%'] - df['售电量'].describe()['25%'] df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
# high = df['售电量'].describe()['75%'] + 1.5*IQR
# low = df['售电量'].describe()['25%'] - 1.5*IQR
# print('异常值数量:',len(df[(df['售电量'] >= high) | (df['售电量'] <= low)]))
#
# df_train = df_train[(df['售电量'] <= high) & (df['售电量'] >= low)]
X = df_train[['tem_max','tem_min','holiday','24ST','season']] X = df_train[['tem_max','tem_min','holiday','24ST','season']]
@ -54,7 +63,6 @@ x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=
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)
@ -69,18 +77,14 @@ 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)
# if abs(goal) < best_goal : # if abs(goal) < best_goal :
# best_goal = abs(goal) # best_goal = abs(goal)
# best_i['best_i'] = i # best_i['best_i'] = i
# x = goal2 # x = goal2
# print(best_i,best_goal,x) # 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('wenzhou.bin') model.save_model('wenzhou.bin')
loaded_model = xgb.XGBRegressor() loaded_model = xgb.XGBRegressor()

@ -5,42 +5,55 @@ 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
mpl.rcParams['font.sans-serif']=['kaiti'] mpl.rcParams['font.sans-serif']=['kaiti']
pd.set_option('display.width',None)
import random import random
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
def season(x): def season(x):
if str(x)[5:7] in ('06','07','08','12','01','02'): if str(x)[5:7] in ('10'):
return 0
elif str(x)[5:7] in ('01', '02', '03', '04', '05', '06', '09', '11', '12'):
return 1 return 1
else: else:
return 0 return 2
pd.set_option('display.width',None)
def normal(nd):
high = nd.describe()['75%'] + 1.5*(nd.describe()['75%']-nd.describe()['25%'])
df = pd.read_excel(r'C:\Users\user\PycharmProjects\pytorch2\入模数据\湖州数据.xlsx',index_col='dtdate') low = nd.describe()['25%'] - 1.5*(nd.describe()['75%']-nd.describe()['25%'])
df.index = pd.to_datetime(df.index,format='%Y-%m-%d') return nd[(nd<high)&(nd>low)]
df['season'] = df.index.map(season)
print(df.head())
parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir))
df_eval = df.loc['2023-9'] data = pd.read_excel(os.path.join(parent_dir,'入模数据/湖州.xlsx'),index_col='dtdate')
df_train = df.loc['2021-1':'2023-8'] data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
print(len(df_eval),len(df_train),len(df)) data = data.loc[normal(data['售电量']).index]
# list2 = []
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量','season']] # list0 = []
# list1 = []
# for i in ('01','02','03','04','05','06','07','08','09','10','11','12'):
# month_index = data.index.strftime('%Y-%m-%d').str[5:7] == f'{i}'
# if data.loc[month_index]['售电量'].mean() >= data['售电量'].describe()['75%']:
# list2.append(i)
# elif data.loc[month_index]['售电量'].mean() <= data['售电量'].describe()['25%']:
# list0.append(i)
# else:
# list1.append(i)
#
# print(list0,list1,list2)
data['season'] = data.index.map(season)
# IQR = df['售电量'].describe()['75%'] - df['售电量'].describe()['25%'] df_eval = data.loc['2023-9']
# high = df['售电量'].describe()['75%'] + 1.5*IQR df_train = data.loc['2021-1':'2023-8']
# low = df['售电量'].describe()['25%'] - 1.5*IQR df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
# print('异常值数量:',len(df[(df['售电量'] >= high) | (df['售电量'] <= low)]))
#
# df_train = df_train[(df['售电量'] <= high) & (df['售电量'] >= low)]
X = df_train[['tem_max','tem_min','holiday','24ST','season']] X = df_train[['tem_max','tem_min','holiday','24ST','season']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']] X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']]
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=100) 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)
@ -48,7 +61,6 @@ 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)
@ -57,19 +69,11 @@ result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=
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)
# 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('huzhou.bin')
# model.save_model('huzhou.bin')
loaded_model = xgb.XGBRegressor() loaded_model = xgb.XGBRegressor()
loaded_model.load_model('huzhou.bin') loaded_model.load_model('huzhou.bin')
import numpy as np import numpy as np

@ -5,48 +5,57 @@ 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
def season(x):
if str(x)[5:7] in ('06','07','08','12','01','02'):
return 1
else:
return 0
mpl.rcParams['font.sans-serif']=['kaiti'] mpl.rcParams['font.sans-serif']=['kaiti']
pd.set_option('display.width',None) pd.set_option('display.width',None)
def season(x):
if str(x)[5:7] in ('01', '02', '10', '11'):
return 0
elif str(x)[5:7] in ('03', '04', '05', '06', '09', '12'):
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[(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'),index_col='dtdate')
data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
data = data.loc[normal(data['售电量']).index]
# list2 = []
# list0 = []
# list1 = []
# for i in ('01','02','03','04','05','06','07','08','09','10','11','12'):
# month_index = data.index.strftime('%Y-%m-%d').str[5:7] == f'{i}'
# if data.loc[month_index]['售电量'].mean() >= data['售电量'].describe()['75%']:
# list2.append(i)
# elif data.loc[month_index]['售电量'].mean() <= data['售电量'].describe()['25%']:
# list0.append(i)
# else:
# list1.append(i)
#
# print(list0,list1,list2)
data['season'] = data.index.map(season)
df = pd.read_excel(r'C:\Users\user\PycharmProjects\pytorch2\入模数据\绍兴数据(1).xlsx',index_col='dtdate') df_eval = data.loc['2023-9']
df.index = pd.to_datetime(df.index ,format='%Y-%m-%d') df_train = data.loc['2021-1':'2023-8']
df['season'] = df.index.map(season)
plt.plot(range(len(df)),df['售电量'])
plt.show()
print(df.head())
df_eval = df.loc['2023-9']
df_train = df.loc['2021-1':'2023-8']
# df_train = df[400:850] # df_train = df[400:850]
print(len(df_eval),len(df_train),len(df)) print(len(df_eval),len(df_train),len(data))
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量','season']] df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
# IQR = df['售电量'].describe()['75%'] - df['售电量'].describe()['25%']
# high = df['售电量'].describe()['75%'] + 1.5*IQR
# low = df['售电量'].describe()['25%'] - 1.5*IQR
# print('异常值数量:',len(df[(df['售电量'] >= high) | (df['售电量'] <= low)]))
#
# df_train = df_train[(df['售电量'] <= high) & (df['售电量'] >= low)]
X = df_train[['tem_max','tem_min','holiday','24ST','season']] X = df_train[['tem_max','tem_min','holiday','24ST','season']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']] X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']]
y = df_train['售电量'] y = df_train['售电量']
# best_goal = 1 # best_goal = 1
# best_i = {} # best_i = {}
# for i in range(400): # for i in range(400):
@ -74,16 +83,8 @@ print(goal2)
# best_goal = abs(goal) # best_goal = abs(goal)
# best_i['best_i'] = i # best_i['best_i'] = i
# x = goal2 # x = goal2
# print(best_i,best_goal,x) # 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('shaoxing.bin') model.save_model('shaoxing.bin')
loaded_model = xgb.XGBRegressor() loaded_model = xgb.XGBRegressor()

@ -5,49 +5,59 @@ 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
mpl.rcParams['font.sans-serif']=['kaiti'] mpl.rcParams['font.sans-serif']=['kaiti']
pd.set_option('display.width',None)
import random import random
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
def season(x): def season(x):
if str(x)[5:7] in ('06','07','08','12','01','02'): if str(x)[5:7] in ('01', '02', '03', '04', '05', '06', '09', '10', '11', '12'):
return 1 return 1
else: else:
return 0 return 2
pd.set_option('display.width',None) 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%'])
df = pd.read_excel(r'C:\Users\user\PycharmProjects\pytorch2\入模数据\舟山数据(1).xlsx',index_col='dtdate') return nd[(nd<high)&(nd>low)]
df.index = pd.to_datetime(df.index,format='%Y-%m-%d')
df['season'] = df.index.map(season)
print(df.head()) parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir))
data = pd.read_excel(os.path.join(parent_dir,'入模数据/舟山.xlsx'),index_col='dtdate')
df_eval = df.loc['2023-9'] data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
df_train = df.loc['2021-1':'2023-8'] data = data.loc[normal(data['售电量']).index]
print(len(df_eval),len(df_train),len(df))
# list2 = []
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量','season']] # list0 = []
# list1 = []
# for i in ('01','02','03','04','05','06','07','08','09','10','11','12'):
# IQR = df['售电量'].describe()['75%'] - df['售电量'].describe()['25%'] # month_index = data.index.strftime('%Y-%m-%d').str[5:7] == f'{i}'
# high = df['售电量'].describe()['75%'] + 1.5*IQR # if data.loc[month_index]['售电量'].mean() >= data['售电量'].describe()['75%']:
# low = df['售电量'].describe()['25%'] - 1.5*IQR # list2.append(i)
# print('异常值数量:',len(df[(df['售电量'] >= high) | (df['售电量'] <= low)])) # elif data.loc[month_index]['售电量'].mean() <= data['售电量'].describe()['25%']:
# list0.append(i)
# else:
# list1.append(i)
# #
# df_train = df_train[(df['售电量'] <= high) & (df['售电量'] >= low)] # print(list0,list1,list2)
data['season'] = data.index.map(season)
df_eval = data.loc['2023-9']
# df_train = data.loc['2021-1':'2023-8']
df_train = data.iloc[500:850]
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
X = df_train[['tem_max','tem_min','holiday','24ST','season']] X = df_train[['tem_max','tem_min','holiday','24ST','season']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']] X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']]
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=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)
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)
# result_test.to_csv(r'C:\Users\鸽子\Desktop\test.csv',encoding='utf-8')
# 指标打印 # 指标打印
print(abs(y_test - y_pred).mean() / y_test.mean()) print(abs(y_test - y_pred).mean() / y_test.mean())
@ -61,9 +71,7 @@ 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)
# 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('zhoushan.bin') model.save_model('zhoushan.bin')
loaded_model = xgb.XGBRegressor() loaded_model = xgb.XGBRegressor()
loaded_model.load_model('zhoushan.bin') loaded_model.load_model('zhoushan.bin')

@ -5,46 +5,47 @@ 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)
def season(x): def season(x):
if str(x)[5:7] in ('06','07','08','12','01','02'): if str(x)[5:7] in ('01', '02'):
return 0
elif str(x)[5:7] in ('03', '04', '05', '06', '09', '10', '11', '12'):
return 1 return 1
else: else:
return 0 return 2
mpl.rcParams['font.sans-serif']=['kaiti']
pd.set_option('display.width',None)
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)]
df = pd.read_excel(r'C:\Users\user\PycharmProjects\pytorch2\入模数据\衢州数据.xlsx') parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir))
df['dtdate'] = pd.to_datetime(df['dtdate'],format='%Y-%m-%d').astype('string') data = pd.read_excel(os.path.join(parent_dir,'入模数据/衢州.xlsx'),index_col='dtdate')
df.set_index('dtdate',inplace=True) data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
df['season'] = df.index.map(season) data = data.loc[normal(data['售电量']).index]
plt.plot(range(len(df)),df['售电量'])
plt.show()
print(df.head())
# df_eval = df[(df.index.str[:10]=='2023-08-29')|(df.index.str[:10]=='2023-08-30')|(df.index.str[:10]=='2023-08-31')] # list2 = []
# list0 = []
# list1 = []
# for i in ('01','02','03','04','05','06','07','08','09','10','11','12'):
# month_index = data.index.strftime('%Y-%m-%d').str[5:7] == f'{i}'
# if data.loc[month_index]['售电量'].mean() >= data['售电量'].describe()['75%']:
# list2.append(i)
# elif data.loc[month_index]['售电量'].mean() <= data['售电量'].describe()['25%']:
# list0.append(i)
# else:
# list1.append(i)
# #
# df_train = df[(df.index.str[:7]!='2023-09')&(df.index.str!='2023-08-29')&(df.index.str!='2023-08-30')&(df.index.str!='2023-08-31')] # print(list0,list1,list2)
data['season'] = data.index.map(season)
df_eval = df[df.index.str[:7]=='2023-07']
df_train = df[(df.index.str[:7]!='2023-08')&(df.index.str[:7]!='2023-09')]
# df_train = df[450:900]
# max_8,min_8 = df_eval['售电量'].max(),df_eval['售电量'].min()
print(len(df_eval),len(df_train),len(df)) df_eval = data.loc['2023-08']
df_train = data.iloc[450:900]
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量','season']]
IQR = df['售电量'].describe()['75%'] - df['售电量'].describe()['25%']
high = df['售电量'].describe()['75%'] + 1.5*IQR
low = df['售电量'].describe()['25%'] - 1.5*IQR
print('异常值数量:',len(df[(df['售电量'] >= high) | (df['售电量'] <= low)]))
df_train = df_train[(df['售电量'] <= high) & (df['售电量'] >= low)]
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
X = df_train[['tem_max','tem_min','holiday','24ST','season']] X = df_train[['tem_max','tem_min','holiday','24ST','season']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']] X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']]
@ -66,17 +67,8 @@ 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['eval'].sum()-result_eval['pred'].sum())/result_eval['eval'].sum())
# print((result_eval['eval'].sum()-(result_eval['eval'][:-3].sum()+result_eval['pred'][-3:].sum()))/result_eval['eval'].sum())
# #
# 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('quzhou.bin') model.save_model('quzhou.bin')
loaded_model = xgb.XGBRegressor() loaded_model = xgb.XGBRegressor()
loaded_model.load_model('quzhou.bin') loaded_model.load_model('quzhou.bin')
import numpy as np import numpy as np
@ -89,22 +81,6 @@ X_eval = np.array([[24.0,15.6,23,0,0],
print(model.predict(X_eval)) print(model.predict(X_eval))
# from sklearn.ensemble import RandomForestRegressor
# from sklearn.metrics import mean_squared_error
# rf = RandomForestRegressor(n_estimators=150,max_depth=6)
#
# # 在训练集上训练模型
# rf.fit(x_train, y_train)
#
# # 在测试集上进行预测
# y_pred = rf.predict(x_test)
# eval_pred = rf.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())
# print((result_eval['eval'].sum()-(result_eval['eval'][:-3].sum()+result_eval['pred'][-3:].sum()))/result_eval['eval'].sum())
# import torch # import torch
# from torch import nn # from torch import nn

@ -5,43 +5,48 @@ 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
def season(x):
if str(x)[5:7] in ('06','07','08','12','01','02'):
return 1
else:
return 0
mpl.rcParams['font.sans-serif']=['kaiti'] mpl.rcParams['font.sans-serif']=['kaiti']
pd.set_option('display.width',None) pd.set_option('display.width',None)
def season(x):
df = pd.read_excel(r'C:\Users\user\PycharmProjects\pytorch2\入模数据\金华数据.xlsx') if str(x)[5:7] in ('01', '02', '04', '10'):
df['dtdate'] = pd.to_datetime(df['dtdate'],format='%Y-%m-%d').astype('string') return 0
df.set_index('dtdate',inplace=True) elif str(x)[5:7] in ('03', '05', '06', '09', '11', '12'):
df['season'] = df.index.map(season) return 1
plt.plot(range(len(df)),df['售电量']) else:
plt.show() return 2
print(df.head())
def normal(nd):
high = nd.describe()['75%'] + 1.5*(nd.describe()['75%']-nd.describe()['25%'])
df_eval = df[df.index.str[:7]=='2023-09'] low = nd.describe()['25%'] - 1.5*(nd.describe()['75%']-nd.describe()['25%'])
df_train = df[(df.index.str[:7]!='2023-08')&(df.index.str[:7]!='2023-09')] return nd[(nd<high)&(nd>low)]
# df_train = df[500:850]
print(len(df_eval),len(df_train),len(df)) parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir))
data = pd.read_excel(os.path.join(parent_dir,'入模数据/金华.xlsx'),index_col='dtdate')
data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
data = data.loc[normal(data['售电量']).index]
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量','season']]
# list2 = []
# list0 = []
# list1 = []
# for i in ('01','02','03','04','05','06','07','08','09','10','11','12'):
# month_index = data.index.strftime('%Y-%m-%d').str[5:7] == f'{i}'
# if data.loc[month_index]['售电量'].mean() >= data['售电量'].describe()['75%']:
# list2.append(i)
# elif data.loc[month_index]['售电量'].mean() <= data['售电量'].describe()['25%']:
# list0.append(i)
# else:
# list1.append(i)
#
# print(list0,list1,list2)
data['season'] = data.index.map(season)
# IQR = df['售电量'].describe()['75%'] - df['售电量'].describe()['25%'] df_eval = data.loc['2023-09']
# high = df['售电量'].describe()['75%'] + 1.5*IQR # df_train = data.loc['2021-01':'2023-08']
# low = df['售电量'].describe()['25%'] - 1.5*IQR df_train = data.iloc[450:900]
# print('异常值数量:',len(df[(df['售电量'] >= high) | (df['售电量'] <= low)]))
#
# df_train = df_train[(df['售电量'] <= high) & (df['售电量'] >= low)]
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
X = df_train[['tem_max','tem_min','holiday','24ST','season']] X = df_train[['tem_max','tem_min','holiday','24ST','season']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']] X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']]
@ -51,11 +56,10 @@ y = df_train['售电量']
# best_i = {} # best_i = {}
# for i in range(400): # for i in range(400):
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=142) 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)
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)
@ -75,13 +79,8 @@ print(goal2)
# best_i['best_i'] = i # best_i['best_i'] = i
# x = goal2 # 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('jinhua.bin') model.save_model('jinhua.bin')
loaded_model = xgb.XGBRegressor() loaded_model = xgb.XGBRegressor()
loaded_model.load_model('jinhua.bin') loaded_model.load_model('jinhua.bin')
import numpy as np import numpy as np

Loading…
Cancel
Save