首页 > 解决方案 > 如果测量值在 t+1 时可用,那么如何在 python 中跳过神经网络中的预测值

问题描述

在这里,我有一个包含三个输入 X1、X2、X3 的数据集。所以在这里我编写了预测 X1 未来值的代码。

这里我有一个问题,我在预测 X1 的未来值 (t+1)。所以有时我在那个时期有真正的价值观。我想告诉如果在运行神经网络时实际值可用,则跳过 (t+1) 处的预测值,然后取实际值并预测 X1 的下一个未来值。

我不知道如何清除这个。在这里,我为了理解目的写了更多描述:

这个解释与我的 csv 文件无关,为了便于理解,我写了这个:

time(t)     X1     X2   X3     predicted at(t+1)(every 60 min)
 0          63     0    0       60
60          63     0    2       90
120         104    11   0       100
180          -     10   8       93

如您所见,我在上午 7:00:00 的第一个预测值为 60,但在上午 7:00:00,我得到了真正的价值。所以我想skip my first prediction value then run the neural network with real value of X1 and predict the next value of X1.

在 120 分钟时,我得到了实际值并跳过了预测值并以 X1 的实际值运行神经网络。

但是180分钟我没有测量实际值。因此,然后添加预测值以 X1 的预测值运行神经网络并预测 X1 的下一个值。

这里我的 x_train_n = 80%

和 X_test_n = 20%

我编写了运行神经网络模型的代码。但我不知道如何编写这段代码。

请帮我解决这个代码?

我的代码:

data=data.values
scaler_x = preprocessing.MinMaxScaler(feature_range =(0, 1))
x = np.array(x).reshape ((len(x),4 ))
x = scaler_x.fit_transform(x)
scaler_y = preprocessing.MinMaxScaler(feature_range =(0, 1))
y = np.array(y).reshape ((len(y), 1))
y = scaler_y.fit_transform(y)
print("row",len(y))
train_end = 80
x_train_n=x[0: train_end ,]
x_test_n=x[train_end +1: ,]
y_train_n=y[0: train_end]
y_test_n=y[train_end +1:] 
x_train_n=x_train_n.reshape(x_train_n.shape +(1,))
x_test_n=x_test_n.reshape(x_test_n.shape + (1,))

n_time_steps = 3
model = Sequential()
model.add(LSTM(4, return_sequences=True,input_shape=(num_time_steps,x_train_n.shape[2])))  # returns a sequence of vectors of dimension 32
model.add(LSTM(8, return_sequences=True))  # returns a sequence of vectors of dimension 32
model.add(LSTM(8))  # return a single vector of dimension 32
model.add(Dense(1))


 batchsize = 1
 model.compile(loss="mean_squared_error",optimizer="adam")

 #model.compile(loss='mean_squared_error', optimizer='adadelta',metrics=['accuracy'])
 history = model.fit(x_train_n,y_train_n, batch_size = batchsize,  nb_epoch=30,validation_data=(x_test_n, y_test_n),shuffle =True)


 pred1=model.predict(x_test_n)
 model.reset_states()
 pred1 = scaler_y.inverse_transform(np.array(pred1).reshape ((len(pred1), 1)))
 real_test = scaler_y.inverse_transform(np.array(y_test_n).reshape ((len(y_test_n), 1))).astype(int)
 real = scaler_y.inverse_transform(np.array(y_train_n).reshape ((len(y_train_n), 1))).astype(int)
 pred1 = pred1[:,0]
 real_test = real_test[:,0]

我试过一次,但它给了我一个错误:

xr = iter(range(0, len(x_train_n[x_train_n.columns[0]])))
for i in xr:
   print(i)
   if i == pred1.all():
    next(xr)

错误是: 错误图片

我的 CSV 文件

标签: python-3.xmachine-learningtimeneural-network

解决方案


推荐阅读