forked from philipperemy/keras-tcn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_series_forecasting.py
59 lines (46 loc) · 1.43 KB
/
time_series_forecasting.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# https://datamarket.com/data/set/22ox/monthly-milk-production-pounds-per-cow-jan-62-dec-75#!ds=22ox&display=line
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from tcn import TCN
##
# It's a very naive (toy) example to show how to do time series forecasting.
# - There are no training-testing sets here. Everything is training set for simplicity.
# - There is no input/output normalization.
# - The model is simple.
##
milk = pd.read_csv('monthly-milk-production-pounds-p.csv', index_col=0, parse_dates=True)
print(milk.head())
lookback_window = 12 # months.
milk = milk.values # just keep np array here for simplicity.
x, y = [], []
for i in range(lookback_window, len(milk)):
x.append(milk[i - lookback_window:i])
y.append(milk[i])
x = np.array(x)
y = np.array(y)
print(x.shape)
print(y.shape)
# noinspection PyArgumentEqualDefault
model = Sequential([
TCN(input_shape=(lookback_window, 1),
kernel_size=2,
use_skip_connections=False,
use_batch_norm=False,
use_weight_norm=False,
use_layer_norm=False
),
Dense(1, activation='linear')
])
model.summary()
model.compile('adam', 'mae')
print('Train...')
model.fit(x, y, epochs=100, verbose=2)
p = model.predict(x)
plt.plot(p)
plt.plot(y)
plt.title('Monthly Milk Production (in pounds)')
plt.legend(['predicted', 'actual'])
plt.show()