如何利用python回归模型将预测值转化为输入值

2024-05-14 18:35:20 发布

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

假设我有四个输入,当我试图预测NaN包含第一个输入列的值时,我想预测第一个输入值的下一个2小时值。你知道吗

我试图跳过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值。你知道吗


Tags: testmodelcountnpresnanpredictnext
1条回答
网友
1楼 · 发布于 2024-05-14 18:35:20

您可以遍历每一行,得到预测并填充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

相关问题 更多 >

    热门问题