如何使用python使用回归模型将预测值转换为NaN输入值



假设我有四个输入,我想预测第一个输入值的下一个 2 小时值当我尝试预测该值时,NaN 包含第一个输入列。

我试图跳过 NaN 值,我正在尝试将较早的 pred 值转移到该输入列中。但这对我不起作用。

[ 120   30  40  50 
110   20  10  20
NaN   12  30  30
120   50  60  70
NaN   10  28  40]  inputs to the model

我期望的输出 训练模型时

[ 120   30  40  50 = pred1 
110   20  10  20 = pred2
pred2 12  30  30 = pred3
120   50  60  70 = pred4
pred4 10  28  40 = pred5 ]

现在这里当训练模型NaN值被删除时,早期的预测值应该必须移动到那个NaN值位置。 我为此编写了代码,但它对我不起作用。这是我的代码:

model.reset_states()
pred= model.predict(x_test_n) 
pred_count=pred[0]
forecasts=[]
next_pred=[]
for col in range(len(x_test_n)-1):
print('Prediction %s: ' % str(pred))
next_pred_res = np.reshape(next_pred, (next_pred.shape[1], 1, next_pred.shape[0]))
# make predictions
forecastPredict = model.predict(next_pred_res, batch_size=1)
forecastPredictInv = scaler.inverse_transform(forecastPredict)
forecasts.append(forecastPredictInv)
next_pred = next_pred[1:]
next_pred = np.concatenate([next_pred, forecastPredict])
pred_count += 1

谁能帮我解决这个错误?我只想用 NaN 值移动早期的预测值。

您可以遍历每一行,获取预测并填充 nans。 如下所示,即

prev_preds = 0
preds = []
# For each row of the dataframe get the predictions. 
for _,row in df.iterrows(): 
# Fill the missing values with previous prediction, initially it will be zero.  
row = row.fillna(prev_preds)
# Now get the prediction and store it in an array
preds.append(model.predict([row.values]))
# Update the previous prediction to new prediction by accessing last element of the predictions array. 
prev_preds = preds[-1]
# Assign the predictions to a new column in dataframe
df['predictions'] = preds

最新更新