在Keras中使用LSTM进行多变量时间序列预测(基于未来数据)

2024-03-29 14:24:32 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我一直在用Keras来预测一个多元时间序列。该数据集是一个污染数据集。第一列是我想要预测的,其余7列是特性。数据集可在以下位置找到: https://github.com/sagarmk/Forecasting-on-Air-pollution-with-RNN-LSTM/blob/master/pollution.csv

所以我想做的是在没有“污染”列的测试集上执行以下代码。假设有新的特征数据,但没有污染数据。所以

  1. 如何在没有测试数据的情况下训练模型?(model.fit())
  2. 如果没有未来的污染数据,我如何预测新的污染数据?(model.predict())

为了简单起见,数据集可以在开始时首先拆分为培训和测试数据集,其中“污染”列从测试数据集中删除

下面是一个简单的代码。 在这里,我只需导入并处理数据集

import numpy as np
import pandas as pd
import matplotlib as plt
import seaborn as sns
import plotly.express as px

# Import data
dataset = pd.read_csv("pollution.csv")
dataset = dataset.drop(['date'], axis = 1)
label, layer = pd.factorize(dataset['wnd_dir'])
dataset['wnd_dir'] = pd.DataFrame(label)
dataset=dataset.fillna(dataset.mean())
dataset.head()

之后,我规范化数据集

from sklearn.preprocessing import MinMaxScaler

values = dataset.values
scaler = MinMaxScaler()
scaled = scaler.fit_transform(values)
scaled[0]

然后将规范化数据转换为监督形式

def to_supervised(dataset,dropNa = True,lag = 1):
    df = pd.DataFrame(dataset)
    column = []
    column.append(df)
    for i in range(1,lag+1):
        column.append(df.shift(-i))
    df = pd.concat(column,axis=1)
    df.dropna(inplace = True)
    features = dataset.shape[1]
    df = df.values
    supervised_data = df[:,:features*lag]
    supervised_data = np.column_stack( [supervised_data, df[:,features*lag]])
    return supervised_data

timeSteps = 2

supervised = to_supervised(scaled,lag=timeSteps)
pd.DataFrame(supervised).head()

现在,数据集被拆分和转换,以便LSTM网络能够处理它

features = dataset.shape[1]
train_hours = round(dataset.shape[0]*0.7)
X = supervised[:,:features*timeSteps]
y = supervised[:,features*timeSteps]

x_train = X[:train_hours,:]
x_test = X[train_hours:,:]
y_train = y[:train_hours]
y_test = y[train_hours:]

print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)
#convert data to fit for lstm
#dimensions = (sample, timeSteps here it is 1, features )

x_train = x_train.reshape(x_train.shape[0], timeSteps, features)
x_test = x_test.reshape(x_test.shape[0], timeSteps, features)

print(x_train.shape,x_test.shape)

这里的模型是经过训练的

#define the model
from keras.models import Sequential
from keras.layers import Dense,LSTM

model = Sequential()
model.add( LSTM( 50, input_shape = ( timeSteps,x_train.shape[2]) ) )
model.add( Dense(1) )

model.compile( loss = "mae", optimizer = "adam")

history =  model.fit( x_train,y_train, validation_data = (x_test,y_test), epochs = 50 , batch_size = 72, verbose = 0, shuffle = False)
plt.pyplot.plot(history.history['loss'], label='train')
plt.pyplot.plot(history.history['val_loss'], label='test')
plt.pyplot.legend()
#plt.pyplot.yticks([])
#plt.pyplot.xticks([])
plt.pyplot.title("loss during training")
plt.pyplot.show()

最后,我绘制了训练数据和测试数据

y_pred = model.predict(x_test)
x_test = x_test.reshape(x_test.shape[0],x_test.shape[2]*x_test.shape[1])

inv_new = np.concatenate( (y_pred, x_test[:,-7:] ) , axis =1)
inv_new = scaler.inverse_transform(inv_new)
final_pred = inv_new[:,0]

plt.pyplot.figure(figsize=(20,10))
plt.pyplot.plot(dataset['pollution'])
plt.pyplot.plot([None for i in dataset['pollution']] + [x for x in final_pred])
plt.pyplot.show()

Tags: 数据testimportdfdatamodeltrainplt
2条回答

I.e. when the "test" dataset only consists of 8 feature columns and no column for the price?

看起来你在问一个特征工程问题。您的真实数据集在不同的列中有nan值,这使得预测失败,对吗

在这种情况下,您可以采用通用解决方案:

用列车组中相应列的中值/平均值填充nan值

例如,您可以通过每个产品最近14天(聚合长度)价格的中位数/平均值填充未来价格

更新

在进行未来预测时,可能有很多功能只具有历史记录(没有计划)。 在传统的机器学习中,如果你想根据所有的特征预测一个目标,你需要先预测这些特征的未来

但是通过LSTM,您可以在一个地方进行预测,检查time_series#multi-output_models

用神经网络预测结果应该像下面的代码行一样简单。传递与训练数据格式相同的新数据

prediction = model.predict(features)

你有什么可以提供的代码吗?你遇到了什么问题

相关问题 更多 >