如何处理 .csv 以进行时间序列分类

How to process .csv for time series classification

我想知道如何处理记录的时间序列数据以将其输入 RNN。

我想用 16 个时间步长的数据和 15 个时间步长的标签让 RNN 对第 16 个时间步长进行分类(如果这有意义的话)。通过对批次使用每三个条目,我可以用每秒合理数量的条目覆盖大约 3 秒的数据。

Here is a smaller .csv of the recorded data.“时间”和“五月天”栏仅供参考,以确保所有内容都正确标记,因此可以删除。
This is what my data looks like after dropping the unrelated columns

Here is what I have tried so far in google colab 不幸的是,这种方法不起作用,调用 model.fit.

时出现“AttributeError: 'tuple' object has no attribute 'shape'”

或者我也试过这个:

data = pd.read_csv("slim.csv", sep=",")
data.drop(['Time', 'Mayday'], axis=1)
dataset = tf.data.Dataset.from_tensor_slices(data)

但从那以后我不确定如何处理数据以获得所需的结果,因为在数据集上调用 tf.keras.preprocessing.timeseries_dataset_from_array() 以错误消息终止

'TensorSliceDataset' object is not subscriptable

你的想法很好。问题是 train_targettest_target 正在返回元组,因为 docs 状态:

Returns a tf.data.Dataset instance. If targets was passed, the dataset yields tuple (batch_of_sequences, batch_of_targets). If not, the dataset yields only batch_of_sequences.

由于您只对本例中的目标感兴趣,您可以运行:

data_set = tf.data.Dataset.zip( (train_input ,train_target.map(lambda x, y: y)))
test_set = tf.data.Dataset.zip( (test_input ,test_target.map(lambda x, y: y)))

但请注意,这仍然无效,因为您的目标具有 (32, 11) 形状,而您的模型输出形状为 (32, 3)。所以你应该问问自己你到底想达到什么目的。

更新 1

尝试:


import tensorflow as tf

data = pd.read_csv("slim.csv", sep=",")
data = data.drop(['Time', 'Mayday'], axis=1)

window_size = 16
dataset = tf.data.Dataset.from_tensor_slices((data.values)).window(window_size, shift=3, drop_remainder=True)
dataset = dataset.flat_map(lambda window: window.batch(window_size)).batch(32)
dataset = dataset.map(lambda x: (x[:, :, :-1], x[:, 15, -1]))

model = tf.keras.models.Sequential([
tf.keras.layers.GRU(input_shape=(None, 8), units= 128),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(units=128, activation='tanh'),
tf.keras.layers.Dense(units=3, activation='softmax')
])

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

model.fit(dataset, epochs=5)