最近写了个EMD-LSTM的代码,记录并分享一下,跟大家一起学习~
EMD——经验模态分解介绍
EMD其实就是一种信号分解的方法,其能将非平稳非线性数据转化为平稳现象数据,对于挖掘数据中隐藏的时序关系具有较大的辅助作用,EMD的计算步骤如下:
1、由时序数据x ( t ) x(t)x (t )的局部极大值、局部极小值确定数据的上包络线U s ( t ) U_s(t)U s (t )和下包络线V s ( t ) V_s(t)V s (t ),求出均值包络线m ( 1 ) m_(1)m (1 ),如式(1)。
\qquad m 1 = U s ( t ) + V s ( t ) 2 ( 1 ) m_1 = \dfrac{U_s(t) + V_s(t)}{2}\quad(1)m 1 =2 U s (t )+V s (t )(1 )
2、将x ( t ) x(t)x (t )减去m ( 1 ) m_(1)m (1 )得到c ( 1 ) c_(1)c (1 ),c ( 1 ) c_(1)c (1 )为第一个固有模态函数(Inherent Model Function, IMF),即python命令里的i m f imf i m f,如公式(2)所示。
\qquad c 1 = x ( t ) − m 1 ( t ) ( 2 ) c_1=x_(t)-m_1(t)\quad(2)c 1 =x (t )−m 1 (t )(2 )
3、残差r ( 1 ) ( t ) r_(1)(t)r (1 )(t )为x ( t ) x_(t)x (t )和m ( 1 ) ( t ) m_(1)(t)m (1 )(t )的差值,见公式(3)。
\qquad r 1 ( t ) = x ( t ) − c 1 ( t ) ( 3 ) r_1(t)=x_(t)-c_1(t)\quad(3)r 1 (t )=x (t )−c 1 (t )(3 )
4、最后将残差r ( 1 ) ( t ) r_(1)(t)r (1 )(t )重复上述步骤,得到符合条件的一系列i m f imf i m f分量c ( i ) c_(i)c (i )和一个残差r ( n ) r_(n)r (n ),见公式(4)。
\qquad ∑ i = 1 n c i + r n ( 4 ) \sum\limits_{i=1}^nc_i+r_n\quad(4)i =1 ∑n c i +r n (4 )
LSTM-EMD流程图
整体框图如上,至于那个预测分量重构是怎么回事我也没搞明白,我就简单的把他们求和求均值了,明白的大佬可以指点一下正确的应该怎么做。
; 代码
这个是数据链接:pollution.csv
import matplotlib.pyplot as plt
from math import sqrt
from numpy import concatenate
from pandas import read_table
from pandas import DataFrame
from pandas import concat
from sklearn.preprocessing import MinMaxScaler, LabelEncoder
from sklearn.metrics import mean_squared_error
from PyEMD import EMD
from pyhht.visualization import plot_imfs
from keras.models import Sequential
from keras.layers import LSTM, Dense
from keras.regularizers import l2
import numpy as np
import pandas as pd
seed = 42
np.random.seed(seed)
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
n_vars = 1 if type(data) is list else data.shape[1]
df = DataFrame(data)
cols, names = list(), list()
for i in range(n_in, 0, -1):
cols.append(df.shift(i))
names += [('var%d(t-%d)' % (j + 1, i)) for j in range(n_vars)]
for i in range(0, n_out):
cols.append(df.shift(-i))
if i == 0:
names += [('var%d(t)' % (j + 1)) for j in range(n_vars)]
else:
names += [('var%d(t+%d)' % (j + 1, i)) for j in range(n_vars)]
agg = concat(cols, axis=1)
agg.columns = names
if dropnan:
agg.dropna(inplace=True)
return agg
dataset = pd.read_csv('./data/pollution.csv', header=0, index_col=0)
values = dataset.values
encoder = LabelEncoder()
values[:, 4] = encoder.fit_transform(values[:, 4])
values = values.astype('float32')
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values)
emd = EMD()
'''
进行emd分解
'''
pollution = values[:, 0]
imfs = emd(pollution)
plot_imfs(pollution, np.array(imfs))
imfsValues = []
for imf in imfs:
values[:, 0] = imf
imfsValues.append(values.copy())
inv_yHats = []
inv_ys = []
for imf in imfsValues:
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(imf)
n_hours = 4
n_features = 8
reframed = series_to_supervised(scaled, n_hours, 1)
values = reframed.values
n_train_hours = 365 * 24 * 4
train = values[:n_train_hours, :]
test = values[n_train_hours:, :]
n_obs = n_hours * n_features
train_x, train_y = train[:, :n_obs], train[:, -n_features]
test_x, test_y = test[:, :n_obs], test[:, -n_features]
train_X = train_x.reshape((train_x.shape[0], n_hours, n_features))
test_X = test_x.reshape((test_x.shape[0], n_hours, n_features))
model = Sequential()
model.add(
LSTM(20, input_shape=(train_X.shape[1], train_X.shape[2]), return_sequences=True, kernel_regularizer=l2(0.005),
recurrent_regularizer=l2(0.005)))
model.add(LSTM(20, kernel_regularizer=l2(0.005), recurrent_regularizer=l2(0.005)))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
history = model.fit(train_X, train_y, epochs=500, batch_size=2 ** 10, validation_data=(test_X, test_y))
yHat = model.predict(test_X)
y = model.predict(train_X)
test_X = test_X.reshape((test_X.shape[0], n_hours * n_features))
'''
这里注意的是保持拼接后的数组 列数 需要与之前的保持一致
'''
inv_yHat = concatenate((yHat, test_X[:, -7:]), axis=1)
inv_yHat = scaler.inverse_transform(inv_yHat)
inv_yHat = inv_yHat[:, 0]
inv_yHats.append(inv_yHat)
test_y = test_y.reshape((len(test_y), 1))
inv_y = concatenate((test_y, test_X[:, -7:]), axis=1)
inv_y = scaler.inverse_transform(inv_y)
inv_y = inv_y[:, 0]
inv_ys.append(inv_y)
inv_yHats = np.array(inv_yHats)
inv_yHats = np.sum(inv_yHats, axis=0)
inv_ys = np.array(inv_ys)
inv_ys = np.sum(inv_ys, axis=0)
rmse = sqrt(mean_squared_error(inv_yHats, inv_ys))
print('Test RMSE: %.3f' % rmse)
inv_y = inv_ys[-24 * 3:]
inv_yHat = inv_yHats[-24 * 3:]
plt.plot(inv_yHat, label='forecast')
plt.plot(inv_y, label='observation')
plt.ylabel('pm2.5')
plt.legend()
plt.show()
rmse = sqrt(mean_squared_error(inv_y, inv_yHat))
print('Test RMSE: %.3f' % rmse)
最后附上参考链接:
1、EMD-LSTM预测负荷量
2、基于EMD-LSTM空气质量预测
3、将数据转换成监督数据
附:做完之后发现一个问题,在用EMD信号分解之后再用LSTM预测的过程在最开始的时候是用到了信号的所有信息,有点像是泄露了测试集信息的感觉,要是使用部分数据做EMD再预测又会出现每次EMD分解的信号数量不一样的情况,导致训练好的模型无法使用,不知道万能的网友有没有啥解决之道,可以一起交流学习一下~
2022.4.14更,今天又跑了一下代码,可能是很久没跑过了,发现上面有很多包没导入,可能会有点问题,又更新了一下代码
Original: https://blog.csdn.net/qq_38603174/article/details/116169949
Author: qq_38603174
Title: EMD+LSTM单变量预测
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/698774/
转载文章受原作者版权保护。转载请注明原作者出处!