-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodeling.py
608 lines (353 loc) · 11.1 KB
/
modeling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import lightgbm as lgb
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import datetime
import xgboost as xgb
import torch
import torch.nn as nn
from torch.utils.data import TensorDataset, DataLoader
import torch.nn.functional as F
# In[107]:
data = pd.read_csv('windelectricity.csv')
# In[108]:
data
# In[109]:
dfhs = data.copy()
dfs = data.copy()
# # 构造数据
# In[110]:
dfhs['OTHS'] = dfhs['OT'].shift(-16)
dfs['OTS'] = dfs['OT'].shift(-288)
# In[111]:
dfhs=dfhs.dropna()
dfs=dfs.dropna()
# In[112]:
dfhs
# # 数据归一化
# In[113]:
def unit1(x):
x_min = np.min(x,axis=0)
x_max = np.max(x,axis=0)
x = (2*x-(x_max-x_min))/(x_max-x_min)
return x
def unit2(x):
return np.sin(x)
# In[114]:
def reunit1(x,x_min,x_max):
return 0.5*(x*(x_max-x_min)+(x_max-x_min))
def reunit2(x):
return np.arcsin(x)
# In[115]:
col1 = dfhs.iloc[:,1:5].columns.tolist() + dfhs.iloc[:, -5:].columns.tolist()
print(col1)
col2=dfhs.iloc[:,5:9].columns.tolist()
print(col2)
# In[116]:
dfhs[col2] = dfhs[col2].apply(unit2)
dfhs[col1] = dfhs[col1].apply(unit1)
# In[117]:
col3 = dfs.iloc[:,1:5].columns.tolist() + dfs.iloc[:, -5:].columns.tolist()
print(col3)
col4=dfs.iloc[:,5:9].columns.tolist()
print(col4)
# In[118]:
dfs[col4] = dfs[col4].apply(unit2)
dfs[col3] = dfs[col3].apply(unit1)
# In[119]:
dfs
# In[120]:
xhs = dfhs.iloc[:,1:-2]
yhs = dfhs['OTHS']
xs = dfs.iloc[:,1:-2]
ys = dfs['OTS']
# In[121]:
xhs
# In[122]:
xs
# # 特征选择
# In[174]:
#计算特征重要性
lgb_data = lgb.Dataset(xs, label=ys)
params = {
"objective": "regression", # 选择适合你问题类型的目标函数
"metric": "rmse", # 选择适合你问题类型的评估指标
}
num_round = 500 # 树的数量,可以根据需要调整
model_ = lgb.train(params, lgb_data, num_round)
feature_importance = model_.feature_importance(importance_type="split")
# In[175]:
feature_names = xs.select_dtypes(exclude=['object']).columns # 特征名称
feature_importance_df = pd.DataFrame({"Feature": feature_names, "Importance": feature_importance})
feature_importance_df=feature_importance_df.sort_values(by="Importance", ascending=False)
# In[176]:
feature_importance_df
# In[183]:
xs = xs.drop(['30m_ws','50m_ws'],axis=1)
xhs = xhs.drop(['30m_ws','50m_ws'],axis=1)
# # Lightgbm模型搭建
# In[123]:
train_size=int(len(dfs)*0.7)
train_xs=xs[:train_size]
test_xs=xs[train_size:]
train_ys=ys[:train_size]
test_ys=ys[train_size:]
my_models = lgb.LGBMRegressor(objective='regression', num_leaves=25, learning_rate=0.0001, n_estimators=900,
verbosity=2)
my_models.fit(train_xs, train_ys)
pred_ys = my_models.predict(test_xs)
RMSEs = np.sqrt(mean_squared_error(test_ys,pred_ys))
print("rmse=:",RMSEs)
# In[255]:
train_size=int(len(dfhs)*0.7)
train_xhs=xhs[:train_size]
test_xhs=xhs[train_size:]
train_yhs=yhs[:train_size]
test_yhs=yhs[train_size:]
my_modelhs = lgb.LGBMRegressor(objective='regression', num_leaves=2, learning_rate=0.0009, n_estimators=2750,
verbosity=3)
my_modelhs.fit(train_xhs, train_yhs)
pred_yhs = my_modelhs.predict(test_xhs)
RMSEhs = np.sqrt(mean_squared_error(test_yhs,pred_yhs))
print("rmse=:",RMSEhs)
# # LSTM 模型搭建
# In[2]:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Using device:", device)
# 设定随机种子,以便结果可复现
torch.manual_seed(42)
# In[3]:
df = pd.read_csv('windelectricity.csv')
# In[4]:
df
# In[5]:
col01 = df.iloc[:,1:5].columns.tolist() + df.iloc[:, -5:].columns.tolist()
print(col01)
col02=df.iloc[:,5:9].columns.tolist()
print(col02)
# In[10]:
df[col02] = df[col02].apply(unit2)
df[col01] = df[col01].apply(unit1)
# In[11]:
df0 = df.drop('date',axis=1)
# In[12]:
df0 = df0.reset_index(drop=True)
# In[13]:
df0
# In[14]:
def split_data(data,pred_len,time_step=96):
dataX=[]
datay=[]
for i in range(len(data)-time_step-pred_len+1):
dataX.append(data[i:i+time_step])
datay.append(data[i+time_step:i+time_step+pred_len])
dataX=np.array(dataX)
datay=np.array(datay)
return dataX,datay
# In[15]:
X,y = split_data(df0,16)
print(f"dataX.shape:{X.shape},datay.shape:{y.shape}")
# In[16]:
#划分训练集和测试集的函数
def train_test_split(dataX,datay,shuffle=True,percentage=0.8):
"""
将训练数据X和标签y以numpy.array数组的形式传入
划分的比例定为训练集:测试集=8:2
"""
if shuffle:
random_num=[index for index in range(len(dataX))]
np.random.shuffle(random_num)
dataX=dataX[random_num]
datay=datay[random_num]
split_num=int(len(dataX)*percentage)
train_X=dataX[:split_num]
train_y=datay[:split_num]
test_X=dataX[split_num:]
test_y=datay[split_num:]
return train_X,train_y,test_X,test_y
# In[17]:
train_X,train_y,test_X,test_y=train_test_split(X,y,shuffle=False,percentage=0.8)
print(f"train_X.shape:{train_X.shape},test_X.shape:{test_X.shape}")
# In[18]:
X_train,y_train=train_X,train_y
# In[47]:
# 定义CNN+LSTM模型类
class CNN_LSTM(nn.Module):
def __init__(self, conv_input,conv_output,input_size, hidden_size, num_layers, output_size):
super(CNN_LSTM,self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.conv_output = conv_output
self.conv=nn.Conv1d(conv_input,conv_output,1)
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
x=self.conv(x)
h0 = torch.zeros(self.num_layers,x.size(0), self.hidden_size) # 初始化隐藏状态h0
c0 = torch.zeros(self.num_layers,x.size(0), self.hidden_size) # 初始化记忆状态c0
#print(f"x.shape:{x.shape},h0.shape:{h0.shape},c0.shape:{c0.shape}")
out, _ = self.lstm(x, (h0, c0))# LSTM前向传播
out = self.fc(out) #[b,s,d]
return out
# In[65]:
test_X1=torch.Tensor(test_X)
test_y1=torch.Tensor(test_y)
# 定义输入、隐藏状态和输出维度
input_size = 12 # 输入特征维度
conv_input=96
conv_output=16
hidden_size = 64 # LSTM隐藏状态维度
num_layers = 10 # LSTM层数
output_size = 12 # 输出维度(预测目标维度)
# 创建CNN_LSTM模型实例
model =CNN_LSTM(conv_input,conv_output,input_size, hidden_size, num_layers, output_size)
#训练周期为500次
num_epochs=500
batch_size=64#一次训练的数量
#优化器
optimizer=torch.optim.Adam(model.parameters(),lr=0.05,betas=(0.5,0.999))
#损失函数
criterion=nn.MSELoss()
train_losses=[]
test_losses=[]
print(f"start")
for epoch in range(num_epochs):
random_num=[i for i in range(len(train_X))]
np.random.shuffle(random_num)
train_X=train_X[random_num]
train_y=train_y[random_num]
train_X1=torch.Tensor(train_X[:batch_size])
train_y1=torch.Tensor(train_y[:batch_size])
#训练
model.train()
#将梯度清空
optimizer.zero_grad()
#将数据放进去训练
output=model(train_X1)
#计算每次的损失函数
train_loss=criterion(output,train_y1)
#反向传播
train_loss.backward()
#优化器进行优化(梯度下降,降低误差)
optimizer.step()
if epoch%10==0:
model.eval()
with torch.no_grad():
output=model(test_X1)
test_loss=criterion(output,test_y1)
train_losses.append(train_loss)
test_losses.append(test_loss)
print(f"epoch:{epoch},train_loss:{np.sqrt(train_loss.detach().numpy())},test_loss:{np.sqrt(test_loss.detach().numpy())}")
# # 短期预测
# In[84]:
X_,y_ = split_data(df0,288)
print(f"dataX.shape:{X_.shape},datay.shape:{y_.shape}")
# In[85]:
train_X_,train_y_,test_X_,test_y_=train_test_split(X_,y_,shuffle=False,percentage=0.8)
print(f"train_X_.shape:{train_X_.shape},test_X_.shape:{test_X_.shape}")
# In[86]:
X_train,y_train=train_X_,train_y_
# In[88]:
train_y1.shape
# In[91]:
test_X1=torch.Tensor(test_X_)
test_y1=torch.Tensor(test_y_)
# 定义输入、隐藏状态和输出维度
input_size = 12 # 输入特征维度
conv_input=96
conv_output=288
hidden_size = 64 # LSTM隐藏状态维度
num_layers = 10 # LSTM层数
output_size = 12 # 输出维度(预测目标维度)
# 创建CNN_LSTM模型实例
model =CNN_LSTM(conv_input,conv_output,input_size, hidden_size, num_layers, output_size)
#训练周期为500次
num_epochs=500
batch_size=64#一次训练的数量
#优化器
optimizer=torch.optim.Adam(model.parameters(),lr=0.05,betas=(0.5,0.999))
#损失函数
criterion=nn.MSELoss()
train_losses=[]
test_losses=[]
print(f"start")
for epoch in range(num_epochs):
random_num=[i for i in range(len(train_X_))]
np.random.shuffle(random_num)
train_X_=train_X_[random_num]
train_y_=train_y_[random_num]
train_X1=torch.Tensor(train_X_[:batch_size])
train_y1=torch.Tensor(train_y_[:batch_size])
print(train_y1.shape)
#训练
model.train()
#将梯度清空
optimizer.zero_grad()
#将数据放进去训练
output=model(train_X1)
print(output.shape)
#计算每次的损失函数
train_loss=criterion(output,train_y1)
#反向传播
train_loss.backward()
#优化器进行优化(梯度下降,降低误差)
optimizer.step()
if epoch%10==0:
model.eval()
with torch.no_grad():
output=model(test_X1)
test_loss=criterion(output,test_y1)
train_losses.append(train_loss)
test_losses.append(test_loss)
print(f"epoch:{epoch},train_loss:{np.sqrt(train_loss.detach().numpy())},test_loss:{np.sqrt(test_loss.detach().numpy())}")
# In[6]:
import os
os.getcwd()
# In[11]:
true288 = np.load('result\\ITransFormer_pl288\\true.npy')
pred288 = np.load('result\\ITransFormer_pl288\\pred.npy')
# In[10]:
true288.shape
# In[12]:
pred288.shape
# In[13]:
import matplotlib.pyplot as plt
# 假设你已经有了真实值和预测值的列表或者numpy数组
true_values = true288[0,:,-1]
pred_values = pred288[0,:,-1]
# 创建一个新的图像
plt.figure()
# 绘制真实值,我们用蓝色线条表示
plt.plot(true_values, label='True', color='blue')
# 绘制预测值,我们用红色线条表示
plt.plot(pred_values, label='Pred', color='red')
# 添加图例
plt.legend()
# 显示图像
plt.show()
# In[14]:
true16 = np.load('result\\ITransFormer_pl16\\true.npy')
pred16 = np.load('result\\ITransFormer_pl16\\pred.npy')
# In[15]:
# 假设你已经有了真实值和预测值的列表或者numpy数组
true_values = true16[0,:,-1]
pred_values = pred16[0,:,-1]
# 创建一个新的图像
plt.figure()
# 绘制真实值,我们用蓝色线条表示
plt.plot(true_values, label='True', color='blue')
# 绘制预测值,我们用红色线条表示
plt.plot(pred_values, label='Pred', color='red')
# 添加图例
plt.legend()
# 显示图像
plt.show()
# In[16]:
true16.shape
# In[ ]: