鸽子 1 year ago
parent dfaab93f61
commit 6049161ed9

@ -49,7 +49,7 @@ data['season'] = data.index.map(season)
df_eval = data.loc['2023-10'] df_eval = data.loc['2023-10']
# df_train = data.loc['2021-1':'2023-8'] # df_train = data.loc['2021-1':'2023-8']
df_train = data[450:-1] df_train = data[450:-1]
# df_train = data.loc['2022-4':'2023-9'][:-3]
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
X = df_train[['tem_max','tem_min','24ST','holiday','season']] X = df_train[['tem_max','tem_min','24ST','holiday','season']]
@ -85,7 +85,7 @@ print(goal2)
# 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')

@ -40,10 +40,12 @@ data = data.loc[normal(data['售电量']).index]
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
# data = data.loc[:'2023-9']
df_eval = data.loc['2023-10'] df_eval = data.loc['2023-10']
df_train = data[500:-1] df_train = data[500:-1]
# df_train = data[500:][:-3]
print(df_train)
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
@ -79,7 +81,7 @@ print(result_eval)
# print(best_i,best_goal) # print(best_i,best_goal)
# 保存模型 # 保存模型
# model.save_model('taizhou.bin') 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')

@ -40,11 +40,11 @@ data = data.loc[normal(data['售电量']).index]
# list1.append(i) # list1.append(i)
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
# data = data.loc[:'2023-9']
df_eval = data.loc['2023-10'] df_eval = data.loc['2023-10']
df_train = data.iloc[450:-1] df_train = data.iloc[450:-1]
# df_train = data.loc['2021-01':'2023-07'] # df_train = data[450:][:-3]
print(df_train)
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
@ -83,7 +83,7 @@ print(result_eval)
# print(best_i,best_goal,x) # print(best_i,best_goal,x)
# 保存模型 # 保存模型
# 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

@ -41,8 +41,8 @@ data = data.loc[normal(data['售电量']).index]
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
df_eval = data.loc['2023-10'] df_eval = data.loc['2023-10']
# df_train = data.loc['2021-01':'2023-08'] # df_train = data.loc['2022-01':'2023-09'][:-3]
# df_train = data[450:900]
df_train = data.loc['2022-01':'2023-10'][:-1] df_train = data.loc['2022-01':'2023-10'][:-1]

@ -48,13 +48,12 @@ data = data.loc[normal(data['售电量']).index]
# data['month'] = data.index.strftime('%Y-%m-%d').str[6] # data['month'] = data.index.strftime('%Y-%m-%d').str[6]
# data['month'] = data['month'].astype('int') # data['month'] = data['month'].astype('int')
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
# data = data.loc[:'2023-9']
df_train = data[500:-1] df_train = data[500:-1]
# df_train = data[500:][:-3]
# df_train = data.loc['2021-01':'2023-08']
df_eval = data.loc['2023-10'] df_eval = data.loc['2023-10']
print(df_train)
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['售电量']
@ -96,7 +95,7 @@ print('r2:',r2_score(y_test,y_pred))
# # 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')

@ -42,15 +42,15 @@ data = data.loc[normal(data['售电量']).index]
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
# data = data.loc[:'2023-8']
df_eval = data.loc['2023-10'] df_eval = data.loc['2023-10']
# df_train = data.loc['2021-1':'2023-8']
df_train = data[450:-1]
df_train = data[450:-1]
# df_train = data[450:][:-3]
print(df_train)
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
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['售电量']
@ -77,14 +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)
print(result_eval)
# 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)
# 保存模型 # 保存模型
# model.save_model('wenzhou.bin') model.save_model('wenzhou.bin')
loaded_model = xgb.XGBRegressor() loaded_model = xgb.XGBRegressor()

@ -43,10 +43,12 @@ data = data.loc[normal(data['售电量']).index]
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
# data = data.loc[:'2023-9']
df_eval = data.loc['2023-10'] df_eval = data.loc['2023-10']
# df_train = data.loc['2022-6':'2023-8']
df_train = data[450:-1] df_train = data[450:-1]
# df_train = data[450:][:-3]
print(df_train)
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
@ -74,7 +76,7 @@ goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result
print(goal2) print(goal2)
print(result_eval) print(result_eval)
# 保存模型 # 保存模型
# 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

@ -43,15 +43,14 @@ data = data.loc[normal(data['售电量']).index]
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
df_eval = data.loc['2023-10'] df_eval = data.loc['2023-10']
# df_train = data.loc['2021-1':'2023-8'] # data = data.loc[:'2023-8']
df_train = data[450:-1] df_train = data[450:-1]
print(len(df_eval),len(df_train),len(data)) # df_train = data[450:][:-3]
print(df_train)
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
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['售电量']
@ -86,7 +85,7 @@ print(goal2)
# print(best_i,best_goal,x) # print(best_i,best_goal,x)
print(result_eval) print(result_eval)
# 保存模型 # 保存模型
# model.save_model('shaoxing.bin') model.save_model('shaoxing.bin')
loaded_model = xgb.XGBRegressor() loaded_model = xgb.XGBRegressor()
loaded_model.load_model('shaoxing.bin') loaded_model.load_model('shaoxing.bin')
import numpy as np import numpy as np

@ -42,8 +42,9 @@ data = data.loc[normal(data['售电量']).index]
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
df_eval = data.loc['2023-10'] df_eval = data.loc['2023-10']
# df_train = data.loc['2021-1':'2023-8'] # data = data.loc[:'2023-8']
df_train = data.iloc[450:-1] df_train = data.iloc[450:-1]
# df_train = data.iloc[450:][:-3]
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
X = df_train[['tem_max','tem_min','24ST','holiday','season']] X = df_train[['tem_max','tem_min','24ST','holiday','season']]

@ -39,10 +39,11 @@ data = data.loc[normal(data['售电量']).index]
# #
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
# data = data.loc[:'2023-8']
df_eval = data.loc['2023-10'] df_eval = data.loc['2023-10']
df_train = data.iloc[450:-1] df_train = data.iloc[450:-1]
# df_train = data.iloc[450:-3]
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
@ -68,7 +69,7 @@ goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result
print(goal2) print(goal2)
print(result_eval) print(result_eval)
# 保存模型 # 保存模型
# 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
@ -79,7 +80,6 @@ X_eval = np.array([[23.69,16.5,23,1,0],
print(model.predict(X_eval)) print(model.predict(X_eval))
# import torch # import torch
# from torch import nn # from torch import nn
# from torch.utils.data import TensorDataset,DataLoader # from torch.utils.data import TensorDataset,DataLoader

@ -41,9 +41,12 @@ data = data.loc[normal(data['售电量']).index]
# print(list0,list1,list2) # print(list0,list1,list2)
data['season'] = data.index.map(season) data['season'] = data.index.map(season)
df_eval = data.loc['2023-10'] data = data.loc[:'2023-9']
# df_train = data.loc['2021-01':'2023-10'][:-3] df_eval = data.loc['2023-9']
df_train = data.iloc[450:-1]
# df_train = data.iloc[450:-1]
df_train = data.iloc[450:-3]
print(df_train)
df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']]
@ -81,7 +84,7 @@ print(result_eval)
# x = goal2 # x = goal2
# # 保存模型 # # 保存模型
# 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

@ -0,0 +1,19 @@
from statsmodels.tsa.x13 import x13_arima_analysis
import pandas as pd
import os
# 读取时间序列数据
data = pd.read_excel('./江苏省日电量数据.xlsx', index_col='pt_date', parse_dates=True)
data = data['power_sal']
# 调用X12-ARIMA模型进行季节性调整和预测
result = x13_arima_analysis(data)
# 输出调整后的时间序列数据
adjusted_data = result.seasadj
# 输出预测结果
forecasts = result.forecasts
# 打印结果
print(adjusted_data)
print(forecasts)

@ -3,6 +3,9 @@ import pandas as pd
import torch import torch
from torch import nn from torch import nn
import os import os
import time
t1 = time.time()
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE" os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
DAYS_FOR_TRAIN = 10 DAYS_FOR_TRAIN = 10
torch.manual_seed(42) torch.manual_seed(42)
@ -38,8 +41,7 @@ def normal(nd):
def data_preprocessing(data): def data_preprocessing(data):
data.columns = data.columns.map(lambda x: x.strip()) data.columns = data.columns.map(lambda x: x.strip())
data.index = data.index.map(lambda x:x.strip()) data.index = data.index.map(lambda x:str(x).strip()[:10])
data.index = pd.to_datetime(data.index,format='%Y-%m-%d') data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
data.sort_index(inplace=True) data.sort_index(inplace=True)
data = data.loc['2021-01':'2023-08'] data = data.loc['2021-01':'2023-08']
@ -52,78 +54,78 @@ def data_preprocessing(data):
return data return data
# 拼接数据集 # 拼接数据集
file_dir = r'C:\Users\user\Desktop\浙江各地市行业电量数据' # file_dir = './浙江各地市行业电量数据'
excel = os.listdir(file_dir)[0] # excel = os.listdir(file_dir)[0]
data = pd.read_excel(os.path.join(file_dir, excel), sheet_name=0, index_col='stat_date') # data = pd.read_excel(os.path.join(file_dir, excel), sheet_name=0, index_col='stat_date')
data.drop(columns='地市',inplace=True) # data.drop(columns='地市',inplace=True)
data = data_preprocessing(data) # data = data_preprocessing(data)
#
df = data[data.columns[0]] # df = data[data.columns[0]]
df.dropna(inplace = True) # df.dropna(inplace = True)
dataset_x, dataset_y = create_dataset(df, DAYS_FOR_TRAIN) # dataset_x, dataset_y = create_dataset(df, DAYS_FOR_TRAIN)
#
for level in data.columns[1:]: # for level in data.columns[1:]:
df = data[level] # df = data[level]
df.dropna(inplace=True) # df.dropna(inplace=True)
x, y = create_dataset(df, DAYS_FOR_TRAIN) # x, y = create_dataset(df, DAYS_FOR_TRAIN)
dataset_x = np.concatenate((dataset_x, x)) # dataset_x = np.concatenate((dataset_x, x))
dataset_y = np.concatenate((dataset_y, y)) # dataset_y = np.concatenate((dataset_y, y))
#
#
for excel in os.listdir(file_dir)[1:]: # for excel in os.listdir(file_dir)[1:]:
#
data = pd.read_excel(os.path.join(file_dir,excel), sheet_name=0,index_col='stat_date') # data = pd.read_excel(os.path.join(file_dir,excel), sheet_name=0,index_col='stat_date')
data.drop(columns='地市', inplace=True) # data.drop(columns='地市', inplace=True)
data = data_preprocessing(data) # data = data_preprocessing(data)
#
for level in data.columns: # for level in data.columns:
df = data[level] # df = data[level]
df.dropna(inplace=True) # df.dropna(inplace=True)
x,y = create_dataset(df,DAYS_FOR_TRAIN) # x,y = create_dataset(df,DAYS_FOR_TRAIN)
dataset_x = np.concatenate((dataset_x,x)) # dataset_x = np.concatenate((dataset_x,x))
dataset_y = np.concatenate((dataset_y,y)) # dataset_y = np.concatenate((dataset_y,y))
#
#
print(dataset_x.shape,dataset_y.shape) # print(dataset_x.shape,dataset_y.shape)
# # 训练 # # 训练
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# #
# # 标准化到0~1 # # 标准化到0~1
max_value = np.max(dataset_x) # max_value = np.max(dataset_x)
min_value = np.min(dataset_x) # min_value = np.min(dataset_x)
dataset_x = (dataset_x - min_value) / (max_value - min_value) # dataset_x = (dataset_x - min_value) / (max_value - min_value)
dataset_y = (dataset_y - min_value) / (max_value - min_value) # dataset_y = (dataset_y - min_value) / (max_value - min_value)
print('max_value:',max_value,'min_value:',min_value) # print('max_value:',max_value,'min_value:',min_value)
# 划分训练集和测试集 # # 划分训练集和测试集
train_size = int(len(dataset_x)*0.7) # train_size = int(len(dataset_x)*0.7)
train_x = dataset_x[:train_size] # train_x = dataset_x[:train_size]
train_y = dataset_y[:train_size] # train_y = dataset_y[:train_size]
#
# # 将数据改变形状RNN 读入的数据维度是 (seq_size, batch_size, feature_size) # # # 将数据改变形状RNN 读入的数据维度是 (seq_size, batch_size, feature_size)
train_x = train_x.reshape(-1, 1, DAYS_FOR_TRAIN) # train_x = train_x.reshape(-1, 1, DAYS_FOR_TRAIN)
train_y = train_y.reshape(-1, 1, 5) # train_y = train_y.reshape(-1, 1, 5)
#
# # 转为pytorch的tensor对象 # # # 转为pytorch的tensor对象
train_x = torch.from_numpy(train_x).to(device).type(torch.float32) # train_x = torch.from_numpy(train_x).to(device).type(torch.float32)
train_y = torch.from_numpy(train_y).to(device).type(torch.float32) # train_y = torch.from_numpy(train_y).to(device).type(torch.float32)
#
model = LSTM_Regression(DAYS_FOR_TRAIN, 32, output_size=5, num_layers=2).to(device) # 导入模型并设置模型的参数输入输出层、隐藏层等 model = LSTM_Regression(DAYS_FOR_TRAIN, 32, output_size=5, num_layers=2).to(device) # 导入模型并设置模型的参数输入输出层、隐藏层等
#
train_loss = [] # train_loss = []
loss_function = nn.MSELoss() # loss_function = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.005, betas=(0.9, 0.999), eps=1e-08, weight_decay=0) # optimizer = torch.optim.Adam(model.parameters(), lr=0.005, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)
for i in range(1500): # for i in range(1500):
out = model(train_x) # out = model(train_x)
loss = loss_function(out, train_y) # loss = loss_function(out, train_y)
loss.backward() # loss.backward()
optimizer.step() # optimizer.step()
optimizer.zero_grad() # optimizer.zero_grad()
train_loss.append(loss.item()) # train_loss.append(loss.item())
if i % 100 == 0: # if i % 100 == 0:
print(f'epoch {i+1}: loss:{loss}') # print(f'epoch {i+1}: loss:{loss}')
#
# 保存/读取模型 # # 保存/读取模型
torch.save(model.state_dict(),'hy5.pth') # torch.save(model.state_dict(),'hy5.pth')
# model.load_state_dict(torch.load('hy5.pth')) # model.load_state_dict(torch.load('hy5.pth'))
# # for test # # for test
@ -148,7 +150,7 @@ max_value = 354024930.8
min_value = 0.0 min_value = 0.0
# 创建测试集 # 创建测试集
file_dir = r'C:\Users\鸽子\Desktop\浙江各地市行业电量数据' file_dir = './浙江各地市行业电量数据'
for excel in os.listdir(file_dir): for excel in os.listdir(file_dir):
df_city = pd.read_excel(os.path.join(file_dir,excel)) df_city = pd.read_excel(os.path.join(file_dir,excel))
city = df_city['地市'].iloc[0] city = df_city['地市'].iloc[0]
@ -164,7 +166,8 @@ for excel in os.listdir(file_dir):
result = pred.detach().numpy()[-5:-2] result = pred.detach().numpy()[-5:-2]
result_dict[industry] = list(result) result_dict[industry] = list(result)
df = pd.DataFrame(result_dict,index=['2023-10-29','2023-10-30','2023-10-31']) df = pd.DataFrame(result_dict,index=['2023-10-29','2023-10-30','2023-10-31'])
df.to_excel(fr'C:\Users\鸽子\Desktop\行业电量预测29-31\{city}.xlsx') # df.to_excel(fr'./行业电量预测29-31/{city}.xlsx')
print(time.time()-t1)
print(result_dict) print(result_dict)
# 反归一化 # 反归一化

Loading…
Cancel
Save