You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.2 KiB
Python

1 year ago
import xgboost as xgb
import pandas as pd
import os
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
import matplotlib as mpl
import matplotlib.pyplot as plt
1 year ago
def season(x):
if str(x)[5:7] in ('06','07','08','12','01','02'):
return 1
else:
return 0
1 year ago
mpl.rcParams['font.sans-serif']=['kaiti']
pd.set_option('display.width',None)
1 year ago
data = pd.read_excel(r'C:\Users\user\PycharmProjects\pytorch2\入模数据\台州数据(1).xlsx',index_col='dtdate')
data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
1 year ago
data['season'] = data.index.map(season)
# plt.plot(range(len(data)),data['售电量'])
# plt.show()
1 year ago
print(data.head())
1 year ago
1 year ago
df_eval = data.loc['2023-8']
1 year ago
# df_train = data.loc['2021-1':'2023-7']
df_train = data[500:850]
print(len(df_eval),len(df_train),len(data))
1 year ago
1 year ago
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量','season']]
1 year ago
# 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)]
1 year ago
X = df_train[['tem_max','tem_min','holiday','24ST','season']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','season']]
1 year ago
y = df_train['售电量']
1 year ago
best_goal = 1
best_i = {}
# for i in range(400):
1 year ago
1 year ago
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=163)
1 year ago
model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150)
model.fit(x_train,y_train)
y_pred = model.predict(x_test)
result_test = pd.DataFrame({'test':y_test,'pred':y_pred},index=y_test.index)
# 指标打印
print(abs(y_test - y_pred).mean() / y_test.mean())
eval_pred = model.predict(X_eval)
result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index)
1 year ago
# 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()
print((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()
# if abs(goal) < best_goal:
# best_goal = abs(goal)
# best_i['best_i'] = i
# print(best_i,best_goal)
1 year ago
#
# 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
1 year ago
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('taizhou.bin')
1 year ago
X_eval = np.array([[25.1,16.8,23,0,0],
[22.8,16.3,23,1,0],
[22.7,14.6,23,1,0],
[22.5,14.4,23,0,0],
[22.6,15.6,23,0,0]])
print(model.predict(X_eval))
1 year ago