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