畅游人工智能之海--Keras教程之时间序列数据预处理

畅游人工智能之海--Keras教程之时间序列数据预处理

前言

keras提供的数据预处理总共有三种类型,他们分别用于处理图片数据、时间序列数据、文本数据,这一次要解析的就是处理时间序列数据预处理,这些api将原始数据按照灵活的格式读入,输出指定格式的可直接输入到模型中的数据。用于处理时间序列数据的\(API\)总共有三个,他们分别是:\(timeseries\_dataset\_from\_array function\), \(pad\_sequences function\), \(TimeseriesGenerator class\)

timeseries_dataset_from_array function

1
2
3
4
5
6
7
8
9
10
11
12
tf.keras.preprocessing.timeseries_dataset_from_array(
data,
targets,
sequence_length,
sequence_stride=1,
sampling_rate=1,
batch_size=128,
shuffle=False,
seed=None,
start_index=None,
end_index=None,
)

这个函数将原始数据转换成时间序列数据,所谓的一组时间序列数据就是,数据中的每一条数据都包含了相同时间长度的以当前时间点为结束的数据。

参数解释

data:输入数据

targets:说白了就是训练集的标签

sequence_length:生成的单个序列的长度

sequence_stride:生成的相邻的两个序列的第一个数据点的在原始数据中的位置间隔

sampling_rate:生成的单个序列的中的每个数据点的在原始数据中的位置间隔

shuffle:是否将生成的序列数据打乱顺序

seed:随机种子,只有使用shuffle时才用

start_index:开始下标,另一个参数对应的结束下标

举个例子

考虑输入数据 [0, 1, ... 99],使用如下参数调用该函数 sequence_length=10, sampling_rate=2, sequence_stride=3, shuffle=False,函数的返回值是:

1
2
3
4
5
First sequence:  [0  2  4  6  8 10 12 14 16 18]
Second sequence: [3 5 7 9 11 13 15 17 19 21]
Third sequence: [6 8 10 12 14 16 18 20 22 24]
...
Last sequence: [78 80 82 84 86 88 90 92 94 96]

pad_sequences

1
2
3
tf.keras.preprocessing.sequence.pad_sequences(
sequences, maxlen=None, dtype="int32", padding="pre", truncating="pre", value=0.0
)

这个函数用于将不同的数据补充成相同长度的数据,使用的是padding(补充数据)和truncation(截断)方法。第一个参数就是输入数据,其他的参数就不解释了,直接上例子。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 注意在实际使用这些程序时,一定要将 tf. 换成tensorflow.
>>> sequence = [[1], [2, 3], [4, 5, 6]]
>>> tf.keras.preprocessing.sequence.pad_sequences(sequence)
array([[0, 0, 1],
[0, 2, 3],
[4, 5, 6]], dtype=int32)
# value 的作用
>>> tf.keras.preprocessing.sequence.pad_sequences(sequence, value=-1)
array([[-1, -1, 1],
[-1, 2, 3],
[ 4, 5, 6]], dtype=int32)
# padding 的作用
>>> tf.keras.preprocessing.sequence.pad_sequences(sequence, padding='post')
array([[1, 0, 0],
[2, 3, 0],
[4, 5, 6]], dtype=int32)
# maxlen 的作用
>>> tf.keras.preprocessing.sequence.pad_sequences(sequence, maxlen=2)
array([[0, 1],
[2, 3],
[5, 6]], dtype=int32)
# truncation 的作用
>>> tf.keras.preprocessing.sequence.pad_sequences(sequence, maxlen=2,truncation='post')
array([[0, 1],
[2, 3],
[4, 5]], dtype=int32)

TimeseriesGenerator

1
2
3
4
5
6
7
8
9
10
11
12
tf.keras.preprocessing.sequence.TimeseriesGenerator(
data,
targets,
length,
sampling_rate=1,
stride=1,
start_index=0,
end_index=None,
shuffle=False,
reverse=False,
batch_size=128,
)

与第一个函数作用相同,不再赘述,关于用法见下面这个例子。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from keras.preprocessing.sequence import TimeseriesGenerator
import numpy as np
data = np.array([[i] for i in range(50)])
targets = np.array([[i] for i in range(50)])
data_gen = TimeseriesGenerator(data, targets,
length=10, sampling_rate=2,
batch_size=2)
assert len(data_gen) == 20
batch_0 = data_gen[0]
x, y = batch_0
assert np.array_equal(x,
np.array([[[0], [2], [4], [6], [8]],
[[1], [3], [5], [7], [9]]]))
assert np.array_equal(y,
np.array([[10], [11]]))