├── README.md └── TimeSeries-CNN ├── 1DCNN.py ├── univariateMultiStep.py ├── multi-variate-multi-step-CNN.py └── multiVariateCNN.py /README.md: -------------------------------------------------------------------------------- 1 | # TimeSeries-CNN 2 | In this project I developed Convolutional Neural Network models for univariate , multivariate , multi-step time series forecasting. 3 | -------------------------------------------------------------------------------- /TimeSeries-CNN/1DCNN.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Feb 28 00:09:54 2020 4 | 5 | @author: SalmanKarim 6 | """ 7 | 8 | # univariate cnn example 9 | from numpy import array 10 | from keras.models import Sequential 11 | from keras.layers import Dense 12 | from keras.layers import Flatten 13 | from keras.layers.convolutional import Conv1D 14 | from keras.layers.convolutional import MaxPooling1D 15 | 16 | # split a univariate sequence into samples 17 | def split_sequence(sequence, n_steps): 18 | X, y = list(), list() 19 | for i in range(len(sequence)): 20 | # find the end of this pattern 21 | end_ix = i + n_steps 22 | # check if we are beyond the sequence 23 | if end_ix > len(sequence)-1: 24 | break 25 | # gather input and output parts of the pattern 26 | seq_x, seq_y = sequence[i:end_ix], sequence[end_ix] 27 | X.append(seq_x) 28 | y.append(seq_y) 29 | return array(X), array(y) 30 | 31 | # define input sequence 32 | raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90] 33 | # choose a number of time steps 34 | n_steps = 3 35 | # split into samples 36 | X, y = split_sequence(raw_seq, n_steps) 37 | # reshape from [samples, timesteps] into [samples, timesteps, features] 38 | n_features = 1 39 | X = X.reshape((X.shape[0], X.shape[1], n_features)) 40 | # define model 41 | model = Sequential() 42 | model.add(Conv1D(filters=64, kernel_size=2, activation='relu', input_shape=(n_steps, n_features))) 43 | model.add(MaxPooling1D(pool_size=2)) 44 | model.add(Flatten()) 45 | model.add(Dense(50, activation='relu')) 46 | model.add(Dense(1)) 47 | model.compile(optimizer='adam', loss='mse') 48 | # fit model 49 | model.fit(X, y, epochs=1000, verbose=0) 50 | # demonstrate prediction 51 | x_input = array([70, 80, 90]) 52 | x_input = x_input.reshape((1, n_steps, n_features)) 53 | yhat = model.predict(x_input, verbose=0) 54 | print(yhat) -------------------------------------------------------------------------------- /TimeSeries-CNN/univariateMultiStep.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Feb 28 00:18:30 2020 4 | 5 | @author: SalmanKarim 6 | """ 7 | 8 | # univariate multi-step vector-output 1d cnn example 9 | from numpy import array 10 | from keras.models import Sequential 11 | from keras.layers import Dense 12 | from keras.layers import Flatten 13 | from keras.layers.convolutional import Conv1D 14 | from keras.layers.convolutional import MaxPooling1D 15 | 16 | # split a univariate sequence into samples 17 | def split_sequence(sequence, n_steps_in, n_steps_out): 18 | X, y = list(), list() 19 | for i in range(len(sequence)): 20 | # find the end of this pattern 21 | end_ix = i + n_steps_in 22 | out_end_ix = end_ix + n_steps_out 23 | # check if we are beyond the sequence 24 | if out_end_ix > len(sequence): 25 | break 26 | # gather input and output parts of the pattern 27 | seq_x, seq_y = sequence[i:end_ix], sequence[end_ix:out_end_ix] 28 | X.append(seq_x) 29 | y.append(seq_y) 30 | return array(X), array(y) 31 | 32 | # define input sequence 33 | raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90] 34 | # choose a number of time steps 35 | n_steps_in, n_steps_out = 3, 2 36 | # split into samples 37 | X, y = split_sequence(raw_seq, n_steps_in, n_steps_out) 38 | # reshape from [samples, timesteps] into [samples, timesteps, features] 39 | n_features = 1 40 | X = X.reshape((X.shape[0], X.shape[1], n_features)) 41 | # define model 42 | model = Sequential() 43 | model.add(Conv1D(filters=64, kernel_size=2, activation='relu', input_shape=(n_steps_in, n_features))) 44 | model.add(MaxPooling1D(pool_size=2)) 45 | model.add(Flatten()) 46 | model.add(Dense(50, activation='relu')) 47 | model.add(Dense(n_steps_out)) 48 | model.compile(optimizer='adam', loss='mse') 49 | # fit model 50 | model.fit(X, y, epochs=2000, verbose=0) 51 | # demonstrate prediction 52 | x_input = array([70, 80, 90]) 53 | x_input = x_input.reshape((1, n_steps_in, n_features)) 54 | yhat = model.predict(x_input, verbose=0) 55 | print(yhat) -------------------------------------------------------------------------------- /TimeSeries-CNN/multi-variate-multi-step-CNN.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Feb 28 00:19:53 2020 4 | 5 | @author: SalmanKarim 6 | """ 7 | 8 | # multivariate multi-step 1d cnn example 9 | from numpy import array 10 | from numpy import hstack 11 | from keras.models import Sequential 12 | from keras.layers import Dense 13 | from keras.layers import Flatten 14 | from keras.layers.convolutional import Conv1D 15 | from keras.layers.convolutional import MaxPooling1D 16 | 17 | # split a multivariate sequence into samples 18 | def split_sequences(sequences, n_steps_in, n_steps_out): 19 | X, y = list(), list() 20 | for i in range(len(sequences)): 21 | # find the end of this pattern 22 | end_ix = i + n_steps_in 23 | out_end_ix = end_ix + n_steps_out-1 24 | # check if we are beyond the dataset 25 | if out_end_ix > len(sequences): 26 | break 27 | # gather input and output parts of the pattern 28 | seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1:out_end_ix, -1] 29 | X.append(seq_x) 30 | y.append(seq_y) 31 | return array(X), array(y) 32 | 33 | # define input sequence 34 | in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) 35 | in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) 36 | out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) 37 | # convert to [rows, columns] structure 38 | in_seq1 = in_seq1.reshape((len(in_seq1), 1)) 39 | in_seq2 = in_seq2.reshape((len(in_seq2), 1)) 40 | out_seq = out_seq.reshape((len(out_seq), 1)) 41 | # horizontally stack columns 42 | dataset = hstack((in_seq1, in_seq2, out_seq)) 43 | # choose a number of time steps 44 | n_steps_in, n_steps_out = 3, 2 45 | # convert into input/output 46 | X, y = split_sequences(dataset, n_steps_in, n_steps_out) 47 | # the dataset knows the number of features, e.g. 2 48 | n_features = X.shape[2] 49 | # define model 50 | model = Sequential() 51 | model.add(Conv1D(filters=64, kernel_size=2, activation='relu', input_shape=(n_steps_in, n_features))) 52 | model.add(MaxPooling1D(pool_size=2)) 53 | model.add(Flatten()) 54 | model.add(Dense(50, activation='relu')) 55 | model.add(Dense(n_steps_out)) 56 | model.compile(optimizer='adam', loss='mse') 57 | # fit model 58 | model.fit(X, y, epochs=2000, verbose=0) 59 | # demonstrate prediction 60 | x_input = array([[70, 75], [80, 85], [90, 95]]) 61 | x_input = x_input.reshape((1, n_steps_in, n_features)) 62 | yhat = model.predict(x_input, verbose=0) 63 | print(yhat) -------------------------------------------------------------------------------- /TimeSeries-CNN/multiVariateCNN.py: -------------------------------------------------------------------------------- 1 | # multivariate multi-headed 1d cnn example 2 | from numpy import array 3 | from numpy import hstack 4 | from keras.models import Model 5 | from keras.layers import Input 6 | from keras.layers import Dense 7 | from keras.layers import Flatten 8 | from keras.layers.convolutional import Conv1D 9 | from keras.layers.convolutional import MaxPooling1D 10 | from keras.layers.merge import concatenate 11 | 12 | # split a multivariate sequence into samples 13 | def split_sequences(sequences, n_steps): 14 | X, y = list(), list() 15 | for i in range(len(sequences)): 16 | # find the end of this pattern 17 | end_ix = i + n_steps 18 | # check if we are beyond the dataset 19 | if end_ix > len(sequences): 20 | break 21 | # gather input and output parts of the pattern 22 | seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1, -1] 23 | X.append(seq_x) 24 | y.append(seq_y) 25 | return array(X), array(y) 26 | 27 | # define input sequence 28 | in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) 29 | in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) 30 | out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) 31 | # convert to [rows, columns] structure 32 | in_seq1 = in_seq1.reshape((len(in_seq1), 1)) 33 | in_seq2 = in_seq2.reshape((len(in_seq2), 1)) 34 | out_seq = out_seq.reshape((len(out_seq), 1)) 35 | # horizontally stack columns 36 | dataset = hstack((in_seq1, in_seq2, out_seq)) 37 | # choose a number of time steps 38 | n_steps = 3 39 | # convert into input/output 40 | X, y = split_sequences(dataset, n_steps) 41 | # one time series per head 42 | n_features = 1 43 | # separate input data 44 | X1 = X[:, :, 0].reshape(X.shape[0], X.shape[1], n_features) 45 | X2 = X[:, :, 1].reshape(X.shape[0], X.shape[1], n_features) 46 | # first input model 47 | visible1 = Input(shape=(n_steps, n_features)) 48 | cnn1 = Conv1D(filters=64, kernel_size=2, activation='relu')(visible1) 49 | cnn1 = MaxPooling1D(pool_size=2)(cnn1) 50 | cnn1 = Flatten()(cnn1) 51 | # second input model 52 | visible2 = Input(shape=(n_steps, n_features)) 53 | cnn2 = Conv1D(filters=64, kernel_size=2, activation='relu')(visible2) 54 | cnn2 = MaxPooling1D(pool_size=2)(cnn2) 55 | cnn2 = Flatten()(cnn2) 56 | # merge input models 57 | merge = concatenate([cnn1, cnn2]) 58 | dense = Dense(50, activation='relu')(merge) 59 | output = Dense(1)(dense) 60 | model = Model(inputs=[visible1, visible2], outputs=output) 61 | model.compile(optimizer='adam', loss='mse') 62 | # fit model 63 | model.fit([X1, X2], y, epochs=1000, verbose=0) 64 | # demonstrate prediction 65 | x_input = array([[80, 85], [90, 95], [100, 105]]) 66 | x1 = x_input[:, 0].reshape((1, n_steps, n_features)) 67 | x2 = x_input[:, 1].reshape((1, n_steps, n_features)) 68 | yhat = model.predict([x1, x2], verbose=0) 69 | print(yhat) --------------------------------------------------------------------------------