1.小波去噪的实现
使用Pywavelets包进行小波去噪
实现代码如下:
#小波去噪处理
training_set_scaled=training_set_scaled.reshape(-1)
w = pywt.Wavelet('db8') # 选用Daubechies8小波
maxlev = pywt.dwt_max_level(len(training_set_scaled), w.dec_len)
threshold = 0.05 # Threshold for filtering
coeffs = pywt.wavedec(training_set_scaled, 'db8', level=maxlev) # 将信号进行小波分解
print(coeffs[0].shape)
print(len(coeffs))
for i in range(1, len(coeffs)):
coeffs[i] = pywt.threshold(coeffs[i], threshold*max(coeffs[i])) # 将噪声滤波
training_set_scaled = pywt.waverec(coeffs, 'db8') # 将信号进行小波重构
plt.plot(training_set_scaled,"b--")
training_set_scaled=np.array(training_set_scaled)
training_set_scaled=training_set_scaled.reshape(-1,1)
print(training_set_scaled.shape)
2.对去噪后的训练集进行重新训练
代码同之前的基本lstm模型的训练代码
3.对归一化后的数据进行还原并画图
maxv=max(training_set)
minv=min(training_set)
print(maxv,minv)
pred=np.array(pred)
for i in range(pred.shape[0]):
pred[i]=pred[i].detach().numpy()*(maxv-minv)+minv
np.savetxt(tc+'_'+sd+'_'+ed+'_pred.txt', pred, fmt="%.2f")
plt.figure(figsize=(8, 4))
plt.plot(pred,"b-")
plt.figure()
plt.plot(rmse,"r-")
plt.plot(mae,"b--")
plt.figure()
plt.plot(pred_t,"g-")
plt.plot(testing_set_scaled[11:],"r-")
结果:(蓝色为原始数据,红色为去噪后的数据,绿色为预测数据)
完整代码:
#基本lstm+wavelet优化
#对原始数据使用小波进行降噪处理
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tushare as ts
import datetime
import torch
from torch.utils.data import Dataset, DataLoader