【发布时间】:2021-08-26 03:13:40
【问题描述】:
我想传递我的预测模型值,这些值将根据条件从 Pandas DataFrame 中提取,并且模型的结果将放置在 Pandas DataFrame 中。
数据帧
+------------+--------------+------------+--------------+
| Date | Actual Value | Prediction | Model Values |
+------------+--------------+------------+--------------+
| 02/01/2021 | 0.02 | | |
| 03/01/2021 | 0.06 | | |
| 04/01/2021 | 0.02 | | |
| 05/01/2021 | 0.04 | | |
| 06/01/2021 | 0.04 | | |
| 07/01/2021 | 0.08 | | |
| 08/01/2021 | 0.06 | | |
| 09/01/2021 | 0.02 | 0.03 | 0.03 |
| 10/01/2021 | 0.20 | | |
| 11/01/2021 | 0.02 | | |
| 12/01/2021 | 0.02 | | |
| 13/01/2021 | 0.09 | 0.06 | 0.06 |
| 14/01/2021 | 0.06 | | |
| 15/01/2021 | 0.04 | | |
| 16/01/2021 | 0.06 | | |
| 17/01/2021 | 0.03 | 0.04 | 0.04 |
| 18/01/2021 | 0.03 | | |
| 19/01/2021 | 0.06 | | |
| 20/01/2021 | 0.06 | | |
+------------+--------------+------------+--------------+
Actual Value 是特定日期的实际值。
Prediction 是该日期的预测值。 (需要填充)
Model Values 需要传递给模型以获得结果的值。 (棘手的部分)
该模型采用过去 7 天的值并给出第二天的输出。因此,可以做出预测的最短日期是9th Jan。
要对9th Jan 进行预测,df['Actual Value'].iloc[:7,] 将被传递给模型,该模型将输出 numpy (1*1) 数组。然后将该值放在Prediction 列中。 (即 0.03)
我卡住的部分
我想使用 9th Jan 的预测值和过去 6 个值来预测 10th Jan。这意味着df['Actual Value'].iloc[1:7,:] + df['Prediction'].iloc[7,:]。
我想访问这些值并将其传递给模型,这将给出一个结果 numpy (1*1) 数组,该数组需要放在 Date 10th Jan 上的 Prediction 列中。
现在我们有了10th Jan 的值,我们可以根据类似的逻辑使用df['Actual Value'].iloc[2:7,:] + df['Prediction'].iloc[7:9,:] 来预测11th Jan。将这些值传递给模型并获得11th Jan 的结果。
预测12th Jan要遵循的类似过程。
但是对于13th Jan,我们将在过去 7 天内使用Actual Value 来获得Prediction。这部分已经完成。每 4 天 09 日、13 日和 17 日之后总会有一个值。代码如下:
look_back = 7
look_forward = 1
n_days_pred = 4
pred = 0
predictions = []
while pred <= X_test.shape[0]:
predictions.append(model.predict(X_test[pred].reshape(1, look_back, look_forward)).flatten().tolist())
pred = pred + n_days_pred
predictions_flat = [item for sublist in predictions for item in sublist]
start_date = test_df.Date.min() + dt.timedelta(look_back)
predictions_df = pd.DataFrame(index=pd.date_range(start=start_date, periods=len(predictions), freq='4D'))
predictions_df['Prediction'] = predictions_flat
# Join predictions_df with df
df = analysis_df.set_index('Date').join(predictions_df)
上面代码中的df与上面显示的相同。
我被困在如何访问 Actual Value 和 Prediction 列中的值以获取第 10、11、12、14、15、16、18、19 和 20 日的预测。
【问题讨论】:
-
不确定我是否理解这一点,1) 您的输入来自
prediction,除非它不可用,然后您使用actual value,并且 2) 您需要最后 7 天来制作预言。这是对的还是我错过了什么? -
@Cimbali 第 2 点是正确的。第 1 点)每 4 天后将仅使用
actual value,因此对于9th, 13th and 17th,仅使用actual value进行预测(已完成并且已经在df中)以填充Prediction为10th,Prediction的9th和来自3rd to 8th的实际值将用作模型至少需要 7 个值。类似于填充预测11thPrediction的9th & 10th以及Actual Value4th to 8th将被使用。 -
所以在某个时候(我猜是第 9 天 + 7 天,所以从第 16 天开始?)只会使用
prediction值,对吗? -
@Cimbali 从第 13、14 和 15 日填充第 16 日的预测
Prediction将与第 9、10、11 和 12 日的Actual Value一起使用。这个想法是对接下来的 4 天进行预测。所以如果我在 13 号,我需要对 13 号、14 号、15 号和 16 号进行预测。我将使用Actual Value从 6 日到 12 日预测 13 日。为了预测 14 号,我将使用 7 号 - 12 号Actual Value和 13 号的预测值。与预测 15 日类似,我将使用 8th -12thActual Value和 13th 和 14thPredicted值。 -
@Cimbali 不会有任何情况下只使用
Predicted来进行第二天的预测。因为预计最多只有 4 天。因此,在第 4 天,您仍然有 4 个实际值 + 3 个预测值(总计:7)来进行第 4 天的预测。
标签: python pandas dataframe numpy conditional-statements