LSTM是一种

，适合于处理和预测时间序列中间隔和延迟相对较长的重要事件。在自然语言处理、语言识别等一系列的应用上都取得了很好的效果。

《Long Short Term Memory Networks with Python》是澳大利亚机器学习专家Jason Brownlee的着作，里面详细介绍了LSTM模型的原理和使用。

## 3.1.1 归一化序列数据

```from pandas import Series
from sklearn.preprocessing import MinMaxScaler
# define contrived series
data = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0]
series = Series(data)
print(series)
# prepare data for normalization
values = series.values
values = values.reshape((len(values), 1))
# train the normalization
scaler = MinMaxScaler(feature_range=(0, 1))
scaler = scaler.fit(values)
print('Min: %f, Max: %f' % (scaler.data_min_, scaler.data_max_))
# normalize the dataset and print
normalized = scaler.transform(values)
print(normalized)
# inverse transform and print
inversed = scaler.inverse_transform(normalized)
print(inversed)```

```0     10.0
1     20.0
2     30.0
3     40.0
4     50.0
5     60.0
6     70.0
7     80.0
8     90.0
9    100.0
dtype: float64
Min: 10.000000, Max: 100.000000
[[ 0.        ]
[ 0.11111111]
[ 0.22222222]
[ 0.33333333]
[ 0.44444444]
[ 0.55555556]
[ 0.66666667]
[ 0.77777778]
[ 0.88888889]
[ 1.        ]]
[[  10.]
[  20.]
[  30.]
[  40.]
[  50.]
[  60.]
[  70.]
[  80.]
[  90.]
[ 100.]]```

## 3.1.2 标准化序列数据

```from pandas import Series
from sklearn.preprocessing import StandardScaler
from math import sqrt
# define contrived series
data = [1.0, 5.5, 9.0, 2.6, 8.8, 3.0, 4.1, 7.9, 6.3]
series = Series(data)
print(series) # prepare data for normalization
values = series.values
values = values.reshape((len(values), 1))
# train the normalization
scaler = StandardScaler()
scaler = scaler.fit(values)
print('Mean: %f, StandardDeviation: %f' % (scaler.mean_, sqrt(scaler.var_)))
# normalize the dataset and print
standardized = scaler.transform(values)
print(standardized)
# inverse transform and print
inversed = scaler.inverse_transform(standardized)
print(inversed)```

```0    1.0
1    5.5
2    9.0
3    2.6
4    8.8
5    3.0
6    4.1
7    7.9
8    6.3
dtype: float64
Mean: 5.355556, StandardDeviation: 2.712568
[[-1.60569456]
[ 0.05325007]
[ 1.34354035]
[-1.01584758]
[ 1.26980948]
[-0.86838584]
[-0.46286604]
[ 0.93802055]
[ 0.34817357]]
[[ 1. ]
[ 5.5]
[ 9. ]
[ 2.6]
[ 8.8]
[ 3. ]
[ 4.1]
[ 7.9]
[ 6.3]]```

## 3.2.1 怎幺样把类别数据转化为数值数据

```red, green, blue
1,   0,     0
0,   1,     0
0,   0,     1```

## 3.2.2 用scikit-learn进行one hot编码

`cold, cold, warm, cold, hot, hot, warm, cold, warm, hot`

```from numpy import array
from numpy import argmax
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
# define example
data = ['cold', 'cold', 'warm', 'cold', 'hot', 'hot', 'warm', 'cold', 'warm', 'hot']
values = array(data)
print(values)
# integer encode
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(values)
print(integer_encoded)
# binary encode
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
print(onehot_encoded)
# invert first example
inverted = label_encoder.inverse_transform([argmax(onehot_encoded[0, :])])
print(inverted)```

```['cold' 'cold' 'warm' 'cold' 'hot' 'hot' 'warm' 'cold' 'warm' 'hot']
[0 0 2 0 1 1 2 0 2 1]
[[ 1.  0.  0.]
[ 1.  0.  0.]
[ 0.  0.  1.]
[ 1.  0.  0.]
[ 0.  1.  0.]
[ 0.  1.  0.]
[ 0.  0.  1.]
[ 1.  0.  0.]
[ 0.  0.  1.]
[ 0.  1.  0.]]
['cold']```

## 3.3.1 序列插补

Keras深度学习库中的插补函数 `sequences()`

```from keras.preprocessing.sequence import pad_sequences
# define sequences
sequences = [ [1, 2, 3, 4], [1, 2, 3], [1] ]

```[[1 2 3 4]
[0 1 2 3]
[0 0 0 1]]```

```from keras.preprocessing.sequence import pad_sequences
# define sequences
sequences = [ [1, 2, 3, 4], [1, 2, 3], [1] ]

```[[1 2 3 4]
[1 2 3 0]
[1 0 0 0]]```

## 3.3.2 序列截断

#### 前序截断

```from keras.preprocessing.sequence import pad_sequences
# define sequences
sequences = [ [1, 2, 3, 4], [1, 2, 3], [1] ]
# truncate sequence
print(truncated)```

```[[3 4]
[2 3]
[0 1]]```

#### 后序截断

```from keras.preprocessing.sequence import pad_sequences
# define sequences
sequences = [ [1, 2, 3, 4], [1, 2, 3], [1] ]
# truncate sequence
print(truncated)```

```[[1 2]
[1 2]
[0 1]]```

```X, y
1, 2
2, 3
3, 4
4, 5
5, 6
6, 7
7, 8
8, 9```

## 3.4.2 Pandas shift()函数

`shift()`

```from pandas import DataFrame
# define the sequence
df = DataFrame()
df['t'] = [x for x in range(10)]
print(df)```

```  t
0  0
1  1
2  2
3  3
4  4
5  5
6  6
7  7
8  8
9  9```

```from pandas import DataFrame
# define the sequence
df = DataFrame()
df['t'] = [x for x in range(10)]
# shift forward
df['t-1'] = df['t'].shift(1)
print(df)```

```  t  t-1
0  0  NaN
1  1  0.0
2  2  1.0
3  3  2.0
4  4  3.0
5  5  4.0
6  6  5.0
7  7  6.0
8  8  7.0
9  9  8.0```

```from pandas import DataFrame
# define the sequence
df = DataFrame()
df['t'] = [x for x in range(10)]
# shift backward
df['t+1'] = df['t'].shift(-1)
print(df)```

```  t  t+1
0  0  1.0
1  1  2.0
2  2  3.0
3  3  4.0
4  4  5.0
5  5  6.0
6  6  7.0
7  7  8.0
8  8  9.0
9  9  NaN```

，其中输入和输出都可以是序列。此外， `shift()`

## 3.5.1 数值缩放的APIs

MinMaxScaler API in scikit-learn.

StandardScaler API in scikit-learn.

Should I normalize/standardize/rescale the data? Neural Nets FAQ.

## 3.5.2 类别编码的APIs

LabelEncoder API in scikit-learn.

OneHotEncoder API in scikit-learn.

NumPy argmax() API.

## 3.5.4监督学习的APIs

shift() function API in Pandas.