假设我有四个输入,并且我想预测第一个输入值的下一个 2 小时值当我尝试预测值时,第一个输入列包含 NaN。
我试图跳过 NaN 值,我试图将早期的预测值转移到该输入列中。但它对我不起作用。
[ 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 值移动早期的预测值。
最佳答案
您可以迭代每一行,获取预测并填充 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
关于python - 如何使用 python 使用回归模型将预测值转换为 NaN 输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59451189/