首页 > 解决方案 > timeseries_dataset_from_array 返回未来的样本而不是以前的样本

问题描述

我的 ML 项目中有一个数据框,它最初采用以下格式。

        Feature 1 | Feature 2| Feature 3 | Feature 4 | etc.
Time 1
Time 2
Time 3
Etc.

我正在尝试将此数据框更改为 3d,其中此数据框中的每个值在屏幕中都有另一个维度,包含相同特征的相同值,但在前 192 个时间步长。

在这里,我尝试使用内置函数 keras.preprocessing.timeseries_dataset_from_array(),但它返回的结果与我想要实现的相反。

我希望它会回来

          Feature 1 | Feature 2| Feature 3 | Feature 4 | etc.
Time 192| [1-192]   | [1-192]  | [1-192]   |           |
Time 193|           |          |           |           |
Time 194|           |          |           |           |
Time End|           |          |           |           |

在这里,它改为返回:

        Feature 1 | Feature 2| Feature 3 | Feature 4 | etc.
Time 1| [192-1]   | [192-1]  | [192-1]   |           |
Time 2|           |          |           |           |
Time 3|           |          |           |           |
Time End-192|     |          |           |           |

基本上每个样本都包含未来的 192 个值,而不是数据集之前的 192 个值。因此,它提前结束了 192 个样本,并且过早地开始了 192 个样本。

我的代码如下:

#Past is defined as 192
#x_val is the 2-d dataframe
#y_val is one of the columns in the dataframe.

dataset_historic_train = keras.preprocessing.timeseries_dataset_from_array(
    x_val,
    y_val,
    sequence_length=past,   
    batch_size=len(x_val),
)

其中 x_val 是从样本的第一次到最后一次索引的整个二维数据帧,y_val 是我的目标特征,在这种情况下是特征 1。

标签: pythondataframemachine-learningtime-seriestf.keras

解决方案


推荐阅读