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.

194 lines
6.9 KiB
Python

11 months ago
import numpy as np
import pandas as pd
import torch
from torch import nn
import os
11 months ago
import time
t1 = time.time()
11 months ago
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
DAYS_FOR_TRAIN = 10
torch.manual_seed(42)
class LSTM_Regression(nn.Module):
def __init__(self, input_size, hidden_size, output_size=1, num_layers=2):
super().__init__()
self.lstm = nn.LSTM(input_size, hidden_size, num_layers)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, _x):
x, _ = self.lstm(_x) # _x is input, size (seq_len, batch, input_size)
s, b, h = x.shape # x is output, size (seq_len, batch, hidden_size)
x = x.view(s * b, h)
x = self.fc(x)
x = x.view(s, b, -1) # 把形状改回来
return x
def create_dataset(data, days_for_train=5) -> (np.array, np.array):
dataset_x, dataset_y = [], []
for i in range(len(data) - days_for_train-5):
dataset_x.append(data[i:(i + days_for_train)])
dataset_y.append(data[i + days_for_train:i + days_for_train+5])
# print(dataset_x,dataset_y)
return (np.array(dataset_x), np.array(dataset_y))
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)]
def data_preprocessing(data):
data.columns = data.columns.map(lambda x: x.strip())
11 months ago
data.index = data.index.map(lambda x:str(x).strip()[:10])
11 months ago
data.index = pd.to_datetime(data.index,format='%Y-%m-%d')
data.sort_index(inplace=True)
data = data.loc['2021-01':'2023-08']
data.drop(columns=[i for i in data.columns if (data[i] == 0).sum() / len(data) >= 0.5], inplace=True) # 去除0值列
data = data.astype(float)
for col in data.columns:
data[col] = normal(data[col])
return data
# 拼接数据集
11 months ago
# file_dir = './浙江各地市行业电量数据'
# excel = os.listdir(file_dir)[0]
# data = pd.read_excel(os.path.join(file_dir, excel), sheet_name=0, index_col='stat_date')
# data.drop(columns='地市',inplace=True)
# data = data_preprocessing(data)
#
# df = data[data.columns[0]]
# df.dropna(inplace = True)
# dataset_x, dataset_y = create_dataset(df, DAYS_FOR_TRAIN)
#
# for level in data.columns[1:]:
# df = data[level]
# df.dropna(inplace=True)
# x, y = create_dataset(df, DAYS_FOR_TRAIN)
# dataset_x = np.concatenate((dataset_x, x))
# dataset_y = np.concatenate((dataset_y, y))
#
#
# 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.drop(columns='地市', inplace=True)
# data = data_preprocessing(data)
#
# for level in data.columns:
# df = data[level]
# df.dropna(inplace=True)
# x,y = create_dataset(df,DAYS_FOR_TRAIN)
# dataset_x = np.concatenate((dataset_x,x))
# dataset_y = np.concatenate((dataset_y,y))
#
#
# print(dataset_x.shape,dataset_y.shape)
11 months ago
# # 训练
11 months ago
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
11 months ago
#
# # 标准化到0~1
11 months ago
# max_value = np.max(dataset_x)
# min_value = np.min(dataset_x)
# dataset_x = (dataset_x - 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)
# # 划分训练集和测试集
# train_size = int(len(dataset_x)*0.7)
# train_x = dataset_x[:train_size]
# train_y = dataset_y[:train_size]
#
# # # 将数据改变形状RNN 读入的数据维度是 (seq_size, batch_size, feature_size)
# train_x = train_x.reshape(-1, 1, DAYS_FOR_TRAIN)
# train_y = train_y.reshape(-1, 1, 5)
#
# # # 转为pytorch的tensor对象
# train_x = torch.from_numpy(train_x).to(device).type(torch.float32)
# train_y = torch.from_numpy(train_y).to(device).type(torch.float32)
#
11 months ago
model = LSTM_Regression(DAYS_FOR_TRAIN, 32, output_size=5, num_layers=2).to(device) # 导入模型并设置模型的参数输入输出层、隐藏层等
11 months ago
#
# train_loss = []
# loss_function = nn.MSELoss()
# 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):
# out = model(train_x)
# loss = loss_function(out, train_y)
# loss.backward()
# optimizer.step()
# optimizer.zero_grad()
# train_loss.append(loss.item())
# if i % 100 == 0:
# print(f'epoch {i+1}: loss:{loss}')
#
# # 保存/读取模型
# torch.save(model.state_dict(),'hy5.pth')
11 months ago
11 months ago
# model.load_state_dict(torch.load('hy5.pth'))
# # for test
# model = model.eval() # 转换成测试模式
# # model.load_state_dict(torch.load(os.path.join(model_save_dir,model_file))) # 读取参数
# dataset_x = dataset_x.reshape(-1, 1, DAYS_FOR_TRAIN) # (seq_size, batch_size, feature_size)
# dataset_x = torch.from_numpy(dataset_x).to(device).type(torch.float32)
#
# pred_test = model(dataset_x) # 全量训练集
# # 模型输出 (seq_size, batch_size, output_size)
# pred_test = pred_test.view(-1)
# pred_test = np.concatenate((np.zeros(DAYS_FOR_TRAIN), pred_test.cpu().detach().numpy()))
11 months ago
11 months ago
# plt.plot(pred_test.reshape(-1), 'r', label='prediction')
# plt.plot(dataset_y.reshape(-1), 'b', label='real')
# plt.plot((train_size*5, train_size*5), (0, 1), 'g--') # 分割线 左边是训练数据 右边是测试数据的输出
# plt.legend(loc='best')
# plt.show()
11 months ago
11 months ago
model.load_state_dict(torch.load('hy5.pth',map_location=torch.device('cpu')))
11 months ago
max_value = 354024930.8
min_value = 0.0
11 months ago
# 创建测试集
11 months ago
file_dir = './浙江各地市行业电量数据'
11 months ago
for excel in os.listdir(file_dir):
df_city = pd.read_excel(os.path.join(file_dir,excel))
city = df_city['地市'].iloc[0]
result_dict = {}
for industry in df_city.columns[3:]:
df_city[industry] = df_city[industry].astype('float')
x, y = create_dataset(df_city[industry], 10)
x = (x - min_value) / (max_value - min_value)
x = x.reshape(-1, 1, 10)
x = torch.from_numpy(x).type(torch.float32).to(device)
pred = model(x).view(-1)
pred = pred * (max_value - min_value) + min_value
result = pred.detach().numpy()[-5:-2]
result_dict[industry] = list(result)
df = pd.DataFrame(result_dict,index=['2023-10-29','2023-10-30','2023-10-31'])
11 months ago
# df.to_excel(fr'./行业电量预测29-31/{city}.xlsx')
print(time.time()-t1)
11 months ago
print(result_dict)
11 months ago
# 反归一化
# pred = pred * (max_value - min_value) + min_value
# df = df * (max_value - min_value) + min_value
11 months ago
11 months ago
# # 打印指标
# print(abs(pred - df[-3:]).mean() / df[-3:].mean())
# result_eight = pd.DataFrame({'pred': np.round(pred,1),'real': df[-3:]})
# target = (result_eight['pred'].sum() - result_eight['real'].sum()) / df[-31:].sum()
# result_eight['loss_rate'] = round(target, 5)
# result_eight['level'] = level
# list_app.append(result_eight)
# print(target)
# print(result_eight)
# final_df = pd.concat(list_app,ignore_index=True)
# final_df.to_csv('市行业电量.csv',encoding='gbk')
# print(final_df)