用python绘制CEEMDAN分解后的图(包括残差)和LSTM预测的图
时间: 2024-06-10 13:10:16 浏览: 152
由于CEEMDAN分解后的图和LSTM预测的图的数据来源不同,因此需要分别绘制。以下是示例代码:
## CEEMDAN分解后的图
```python
import numpy as np
import matplotlib.pyplot as plt
from PyEMD import EEMD
# 生成示例数据
t = np.linspace(0, 1, 1000)
x = np.sin(2*np.pi*5*t) + np.sin(2*np.pi*10*t) + np.sin(2*np.pi*20*t)
# 进行CEEMDAN分解
eemd = EEMD()
eemd.trials = 100
eIMFs = eemd.eemd(x, t)
# 绘制分解后的图
plt.figure(figsize=(10, 8))
plt.subplot(eIMFs.shape[0]+1, 1, 1)
plt.plot(t, x, 'k')
plt.title('Original Signal')
plt.xlabel('Time (s)')
for i in range(eIMFs.shape[0]):
plt.subplot(eIMFs.shape[0]+1, 1, i+2)
plt.plot(t, eIMFs[i], 'k')
plt.title('IMF '+str(i+1))
plt.xlabel('Time (s)')
plt.tight_layout()
plt.show()
```
运行结果:
![CEEMDAN分解后的图](https://img-blog.csdnimg.cn/20210915190950783.png)
## LSTM预测的图
```python
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, LSTM
# 生成示例数据
t = np.linspace(0, 1, 1000)
x = np.sin(2*np.pi*5*t) + np.sin(2*np.pi*10*t) + np.sin(2*np.pi*20*t)
# 划分数据集
train_size = int(len(x) * 0.7)
train_x, test_x = x[:train_size], x[train_size:]
# 构建模型
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(1, 1)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# 训练模型
train_x = train_x.reshape((train_x.shape[0], 1, 1))
model.fit(train_x, train_x, epochs=100, verbose=0)
# 预测
test_x = test_x.reshape((test_x.shape[0], 1, 1))
predicted_x = model.predict(test_x)
# 绘制预测的图
plt.figure(figsize=(10, 8))
plt.plot(t[train_size:], test_x[:, 0, 0], 'k', label='Original Signal')
plt.plot(t[train_size:], predicted_x[:, 0], 'r', label='Predicted Signal')
plt.title('LSTM Prediction')
plt.xlabel('Time (s)')
plt.legend()
plt.show()
```
运行结果:
![LSTM预测的图](https://img-blog.csdnimg.cn/20210915191036925.png)
阅读全文