1.第一问

2.第二问

1.第一问

2.第二问

# 一、第一题

## 1.第一问

LSTM模型：

``````class LSTM(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, output_size, batch_size):
super().__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.output_size = output_size
self.num_directions = 1 # 单向LSTM
self.batch_size = batch_size
self.lstm = nn.LSTM(self.input_size, self.hidden_size, self.num_layers, batch_first=True)
self.linear = nn.Linear(self.hidden_size, self.output_size)

def forward(self, input_seq):
h_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(device)
c_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(device)
seq_len = input_seq.shape[1]
# input(batch_size, seq_len, input_size)
input_seq = input_seq.view(self.batch_size, seq_len, 1)
# output(batch_size, seq_len, num_directions * hidden_size)
output, _ = self.lstm(input_seq, (h_0, c_0))
output = output.contiguous().view(self.batch_size * seq_len, self.hidden_size)
pred = self.linear(output) # pred(150, 1)
pred = pred.view(self.batch_size, seq_len, -1)
pred = pred[:, -1, :]
return pred
``````

``````    for i in range(len(data) - 96):
train_seq = []
train_label = []
for j in range(i, i + 96):
train_seq = torch.FloatTensor(train_seq).view(-1)
train_label = torch.FloatTensor(train_label).view(-1)
seq.append((train_seq, train_label))``````

``````x = [i for i in range(1, 97)]
x_smooth = np.linspace(np.min(x), np.max(x), 900)
y_smooth = make_interp_spline(x, y.T[96:192])(x_smooth)
plt.plot(x_smooth, y_smooth, c='green', marker='*', ms=1, alpha=0.75, label='true')

y_smooth = make_interp_spline(x, pred.T[96:192])(x_smooth)
plt.plot(x_smooth, y_smooth, c='red', marker='o', ms=1, alpha=0.75, label='pred')
plt.grid(axis='y')
plt.legend()
plt.show()``````

mape: 0.014883823223180978，即准确率达98%+，可自行调参提高准确率

ARIMA模型：

 数据时间 总有功功率(KM) 2021/9/1 0:00 261372.7135 2021/9/1 0:15 236249.2922 2021/9/1 0:30 232803.4953 2021/9/1 0:45 229891.0916 2021/9/1 1:00 250784.1078 2021/9/1 1:15 226421.0218 2021/9/1 1:30 223678.3659 2021/9/1 1:45 222034.4955 2021/9/1 2:00 244418.4528 ...... ......

mape: 0.07800534689379683，即准确率达92%+，可自行调参提高准确率

## 2.第二问

 数据时间 总有功功率(KM) 2021/9/1 270572.8498 2021/9/2 266392.4357 2021/9/3 263519.7642 2021/9/4 260923.6411 2021/9/5 258540.9461 2021/9/6 256324.3903 2021/9/7 254238.4298 2021/9/8 252256.2427 2021/9/9 250357.4918 ...... ......

......略

# 二、第二题

## 1.第一问

``````"""

"""``````

# 写在后面

我看了下她做的，内容挺丰富的，代码/方法/可视化的图都打包好了，照着她的思路论文也很好写。 这里我只帮她展示了一点点内容，详情请移步闲鱼

THE END