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

时间:2019-12-23 06:47:19

标签: python pandas regression nan prediction

假定我有四个输入,我想预测下一个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值。

1 个答案:

答案 0 :(得分:1)

您可以遍历每一行,获得预测并填充nan。像下面这样

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