鸽子 1 year ago
parent 29b60302b0
commit 8877c0d221

@ -0,0 +1,67 @@
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)
data = pd.read_excel(r'C:\Users\鸽子\Desktop\入模数据\丽水数据.xlsx',index_col='dtdate')
data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
plt.plot(range(len(data)),data['售电量'])
plt.show()
print(data.head())
df_eval = data.loc['2023-8']
df_train = data.loc['2021-1':'2023-7']
# 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','售电量']]
# 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','rh','prs']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','rh','prs']]
y = df_train['售电量']
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.15,random_state=100)
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)
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 = 1
# result_eval.to_csv(r'C:\Users\鸽子\Desktop\各地市日电量预测结果\丽水.csv')
# 保存模型
# model.save_model('lishui.bin')
# loaded_model = xgb.XGBRegressor()
# loaded_model.load_model('lishui.bin')
# model.predict(X_eval)

@ -0,0 +1,68 @@
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)
df = pd.read_excel(r'C:\Users\鸽子\Desktop\入模数据\台州数据(1).xlsx')
df['dtdate'] = pd.to_datetime(df['dtdate'],format='%Y-%m-%d').astype('string')
df.set_index('dtdate',inplace=True)
plt.plot(range(len(df)),df['售电量'])
plt.show()
print(df.head())
df_eval = df[df.index.str[:7]=='2023-08']
df_train = df[(df.index.str[:7]!='2023-08')&(df.index.str[:7]!='2023-09')]
# df_train = df[400:850]
print(len(df_eval),len(df_train),len(df))
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量']]
# 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','rh','prs']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','rh','prs']]
y = df_train['售电量']
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=100)
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)
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())
result_eval.to_csv(r'C:\Users\鸽子\Desktop\各地市日电量预测结果\台州.csv')
# 保存模型
model.save_model('taizhou.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('taizhou.bin')
model.predict(X_eval)

@ -0,0 +1,67 @@
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)
df = pd.read_excel(r'C:\Users\鸽子\Desktop\入模数据\嘉兴数据.xlsx')
df['dtdate'] = pd.to_datetime(df['dtdate'],format='%Y-%m-%d').astype('string')
df.set_index('dtdate',inplace=True)
plt.plot(range(len(df)),df['售电量'])
plt.show()
print(df.head())
df_eval = df[df.index.str[:7]=='2023-08']
# df_train = df[(df.index.str[:7]!='2023-08')&(df.index.str[:7]!='2023-09')]
df_train = df[500:850]
print(len(df_eval),len(df_train),len(df))
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量']]
# 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','rh','prs']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','rh','prs']]
y = df_train['售电量']
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.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)
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())
result_eval.to_csv(r'C:\Users\鸽子\Desktop\各地市日电量预测结果\嘉兴.csv')
# 保存模型
model.save_model('jiaxing.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('jiaxing.bin')
model.predict(X_eval)

@ -0,0 +1,68 @@
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)
df = pd.read_excel(r'C:\Users\鸽子\Desktop\入模数据\宁波数据.xlsx')
df['dtdate'] = pd.to_datetime(df['dtdate'],format='%Y-%m-%d').astype('string')
df.set_index('dtdate',inplace=True)
plt.plot(range(len(df)),df['售电量'])
plt.show()
print(df.head())
df_eval = df[df.index.str[:7]=='2023-08']
# df_train = df[(df.index.str[:7]!='2023-08')&(df.index.str[:7]!='2023-09')]
df_train = df[400:850]
print(len(df_eval),len(df_train),len(df))
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量']]
# 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','rh','prs']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','rh','prs']]
y = df_train['售电量']
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.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)
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())
result_eval.to_csv(r'C:\Users\鸽子\Desktop\各地市日电量预测结果\宁波.csv')
# 保存模型
model.save_model('ningbo.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('ningbo.bin')
model.predict(X_eval)

@ -0,0 +1,68 @@
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)
df = pd.read_excel(r'C:\Users\鸽子\Desktop\入模数据\温州数据(1).xlsx')
df['dtdate'] = pd.to_datetime(df['dtdate'],format='%Y-%m-%d').astype('string')
df.set_index('dtdate',inplace=True)
plt.plot(range(len(df)),df['售电量'])
plt.show()
print(df.head())
df_eval = df[df.index.str[:7]=='2023-08']
# df_train = df[(df.index.str[:7]!='2023-08')&(df.index.str[:7]!='2023-09')]
df_train = df[400:850]
print(len(df_eval),len(df_train),len(df))
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量']]
# 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','rh','prs']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','rh','prs']]
y = df_train['售电量']
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.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)
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())
result_eval.to_csv(r'C:\Users\鸽子\Desktop\各地市日电量预测结果\温州.csv')
# 保存模型
model.save_model('wenzhou.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('wenzhou.bin')
model.predict(X_eval)

@ -0,0 +1,63 @@
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
mpl.rcParams['font.sans-serif']=['kaiti']
import random
import matplotlib.pyplot as plt
pd.set_option('display.width',None)
df = pd.read_excel(r'C:\Users\鸽子\Desktop\入模数据\湖州数据.xlsx')
df['dtdate'] = pd.to_datetime(df['dtdate'],format='%Y-%m-%d').astype('string')
df.set_index('dtdate',inplace=True)
print(df.head())
df_eval = df[df.index.str[:7]=='2023-08']
df_train = df[(df.index.str[:7]!='2023-08')&(df.index.str[:7]!='2023-09')]
print(len(df_eval),len(df_train),len(df))
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量']]
# 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','rh','prs']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','rh','prs']]
y = df_train['售电量']
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.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)
# result_test.to_csv(r'C:\Users\鸽子\Desktop\test.csv',encoding='utf-8')
# 指标打印
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)
print(result_eval)
print((result_eval['eval'].sum()-result_eval['pred'].sum())/result_eval['eval'].sum())
result_eval.to_csv(r'C:\Users\鸽子\Desktop\各地市日电量预测结果\湖州.csv')
print((result_eval['eval'].sum()-(result_eval['eval'][:-3].sum()+result_eval['pred'][-3:].sum()))/result_eval['eval'].sum())
model.save_model('huzhou.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('huzhou.bin')
model.predict(X_eval)

@ -0,0 +1,68 @@
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)
df = pd.read_excel(r'C:\Users\鸽子\Desktop\入模数据\绍兴数据(1).xlsx')
df['dtdate'] = pd.to_datetime(df['dtdate'],format='%Y-%m-%d').astype('string')
df.set_index('dtdate',inplace=True)
plt.plot(range(len(df)),df['售电量'])
plt.show()
print(df.head())
df_eval = df[df.index.str[:7]=='2023-08']
# df_train = df[(df.index.str[:7]!='2023-08')&(df.index.str[:7]!='2023-09')]
df_train = df[400:850]
print(len(df_eval),len(df_train),len(df))
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量']]
# 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','rh','prs']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','rh','prs']]
y = df_train['售电量']
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.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)
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())
result_eval.to_csv(r'C:\Users\鸽子\Desktop\各地市日电量预测结果\绍兴.csv')
# 保存模型
model.save_model('shaoxing.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('shaoxing.bin')
model.predict(X_eval)

@ -0,0 +1,63 @@
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
mpl.rcParams['font.sans-serif']=['kaiti']
import random
import matplotlib.pyplot as plt
pd.set_option('display.width',None)
df = pd.read_excel(r'C:\Users\鸽子\Desktop\入模数据\舟山数据(1).xlsx')
df['dtdate'] = pd.to_datetime(df['dtdate'],format='%Y-%m-%d').astype('string')
df.set_index('dtdate',inplace=True)
print(df.head())
df_eval = df[df.index.str[:7]=='2023-08']
df_train = df[(df.index.str[:7]!='2023-08')&(df.index.str[:7]!='2023-09')]
print(len(df_eval),len(df_train),len(df))
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量']]
# 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','rh','prs']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','rh','prs']]
y = df_train['售电量']
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.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)
# result_test.to_csv(r'C:\Users\鸽子\Desktop\test.csv',encoding='utf-8')
# 指标打印
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)
print(result_eval)
print((result_eval['eval'].sum()-result_eval['pred'].sum())/result_eval['eval'].sum())
result_eval.to_csv(r'C:\Users\鸽子\Desktop\各地市日电量预测结果\舟山.csv')
print((result_eval['eval'].sum()-(result_eval['eval'][:-3].sum()+result_eval['pred'][-3:].sum()))/result_eval['eval'].sum())
model.save_model('zhoushan.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('zhoushan.bin')
model.predict(X_eval)

@ -0,0 +1,143 @@
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)
df = pd.read_excel(r'C:\Users\鸽子\Desktop\入模数据\衢州数据.xlsx')
df['dtdate'] = pd.to_datetime(df['dtdate'],format='%Y-%m-%d').astype('string')
df.set_index('dtdate',inplace=True)
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')]
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')]
# 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_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量']]
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','rh','prs']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','rh','prs']]
y = df_train['售电量']
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=102)
model = xgb.XGBRegressor(max_depth=6, learning_rate=0.1, 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)
print(result_eval)
target = (result_eval['eval'].sum()-result_eval['pred'].sum())/ df[df.index.str[:7]=='2023-08']['售电量'].sum()
print(target)
# 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\鸽子\Desktop\各地市日电量预测结果\衢州.csv')
#
# 保存模型
model.save_model('quzhou.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('quzhou.bin')
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
# from torch import nn
# from torch.utils.data import TensorDataset,DataLoader
#
#
# net = nn.Sequential(
# nn.Linear(6,32),
# nn.ReLU(),
# nn.Linear(32,64),
# nn.ReLU(),
# nn.Linear(64,64),
# nn.ReLU(),
# nn.Linear(64,1)
# )
# opt = torch.optim.Adam(net.parameters(),lr=0.00005)
# loss_fn = nn.MSELoss()
#
# epochs = 200
#
# x_train = torch.from_numpy(x_train.values).type(torch.float32)
# x_train = (x_train - x_train.mean())/x_train.std()
#
# y_train = torch.from_numpy(y_train.values).type(torch.float32)
# std1 = y_train.std()
# mean1 = y_train.mean()
# y_train = (y_train - mean1)/std1
#
# X_eval = torch.from_numpy(X_eval.values).type(torch.float32)
# X_eval = (X_eval - X_eval.mean())/X_eval.std()
#
# y_eval= torch.from_numpy(df_eval['售电量'].values).type(torch.float32)
#
#
# train_ds = TensorDataset(x_train,y_train)
# train_dl = DataLoader(train_ds,shuffle=True,batch_size=64)
#
#
# for i in range(epochs):
# for x,y in train_dl:
# y_pred = net(x)
# loss = loss_fn(y_pred,y)
#
# opt.zero_grad()
# loss.backward()
# opt.step()
# print(round(loss.item(),2))
#
# predict = (net(X_eval) * std1 + mean1).detach().numpy()
# print(y_train.std(),y_train.mean())
# print(net(X_eval))
# print(predict)
# print((y_eval.detach().numpy().sum() - predict.sum())/ y_eval.detach().numpy().sum())

@ -0,0 +1,68 @@
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)
df = pd.read_excel(r'C:\Users\鸽子\Desktop\入模数据\金华数据.xlsx')
df['dtdate'] = pd.to_datetime(df['dtdate'],format='%Y-%m-%d').astype('string')
df.set_index('dtdate',inplace=True)
plt.plot(range(len(df)),df['售电量'])
plt.show()
print(df.head())
df_eval = df[df.index.str[:7]=='2023-08']
# df_train = df[(df.index.str[:7]!='2023-08')&(df.index.str[:7]!='2023-09')]
df_train = df[500:850]
print(len(df_eval),len(df_train),len(df))
df_train = df_train[['tem_max','tem_min','holiday','24ST','rh','prs','售电量']]
# 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','rh','prs']]
X_eval = df_eval[['tem_max','tem_min','holiday','24ST','rh','prs']]
y = df_train['售电量']
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.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)
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())
result_eval.to_csv(r'C:\Users\鸽子\Desktop\各地市日电量预测结果\金华.csv')
# 保存模型
model.save_model('jinhua.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('jinhua.bin')
model.predict(X_eval)

@ -5,7 +5,7 @@ import pandas as pd
import torch import torch
from torch import nn from torch import nn
import os import os
from multiprocessing import Process, Pool from multiprocessing import Pool
DAYS_FOR_TRAIN = 9 DAYS_FOR_TRAIN = 9

Loading…
Cancel
Save