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.

95 lines
3.0 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
mpl.rcParams['font.sans-serif']=['kaiti']
pd.set_option('display.width',None)
def season(x):
if str(x)[5:7] in ('01', '02', '10', '11'):
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')
1 year ago
data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
data = data.loc[normal(data['售电量']).index]
1 year ago
# 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)
1 year ago
data['season'] = data.index.map(season)
1 year ago
# data = data.loc[:'2023-9']
df_eval = data.loc['2023-10']
1 year ago
df_train = data[500:-1]
1 year ago
# df_train = data[500:][:-3]
print(df_train)
1 year ago
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
1 year ago
X = df_train[['tem_max','tem_min','24ST','holiday','season']]
X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']]
1 year ago
y = df_train['售电量']
1 year ago
# best_goal = 1
# best_i = {}
1 year ago
# 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=158)
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())
goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum()
print(goal)
1 year ago
goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum()
print(goal2)
print(result_eval)
1 year ago
# if abs(goal) < best_goal:
# best_goal = abs(goal)
# best_i['best_i'] = i
# print(best_i,best_goal)
# 保存模型
1 year ago
model.save_model('taizhou.bin')
1 year ago
import numpy as np
X_eval = np.array([
[19,11,10,1,0],
[21, 7, 10, 0, 0],
[19, 5, 10, 0, 0],
[17, 8, 10, 0, 0],
[16, 7, 10, 0, 0]
])
1 year ago
print(model.predict(X_eval))