├── README.md ├── ARIMA.py ├── RF.py ├── SVM.py ├── train_CNN.py ├── train_cnn_lstm.py ├── train_cnn_lstm_multi.py ├── train_lstm.py ├── tcn_a.py ├── LSTM.py ├── CNN.py ├── CNN_LSTM.py └── CNN_LSTM_multi_output.py /README.md: -------------------------------------------------------------------------------- 1 | # Time-Series-Forecasting 2 | ## Model 3 | - [ARIMA](https://github.com/danielgy/Time-Series-Forecasting/blob/master/ARIMA.py) 4 | - [SVR](https://github.com/danielgy/Time-Series-Forecasting/blob/master/SVM.py) 5 | - [Random Forest](https://github.com/danielgy/Time-Series-Forecasting/blob/master/RF.py) 6 | - [LSTM](https://github.com/danielgy/Time-Series-Forecasting/blob/master/LSTM.py) 7 | - [CNN](https://github.com/danielgy/Time-Series-Forecasting/blob/master/train_CNN.py) 8 | - [CNN-LSTM](https://github.com/danielgy/Time-Series-Forecasting/blob/master/train_cnn_lstm.py) 9 | - [CNN-LSTM-multi](https://github.com/danielgy/Time-Series-Forecasting/blob/master/train_cnn_lstm_multi.py) 10 | - [Temporal causal dilated CNN](https://github.com/danielgy/Time-Series-Forecasting/blob/master/tcn_a.py) 11 | -------------------------------------------------------------------------------- /ARIMA.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Jun 11 19:05:10 2018 4 | 5 | @author: GY 6 | """ 7 | import pandas as pd 8 | import numpy as np 9 | import matplotlib.pyplot as plt 10 | import statsmodels.api as sm 11 | from pandas.tools.plotting import autocorrelation_plot 12 | from statsmodels.tsa.arima_model import ARIMA 13 | from sklearn.metrics import mean_squared_error 14 | 15 | def data_pre(data): 16 | """ 17 | :param data: 18 | :param seq_len: 19 | :return: 20 | """ 21 | row = round(0.9 * data.shape[0]) 22 | data = data.values 23 | train = data[:int(row), :] 24 | x_train = train[:, :] 25 | y_train = train[:,16:17] 26 | x_test = data[int(row):, :] 27 | y_test = data[int(row):, 16:17] 28 | x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1])) 29 | x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1])) 30 | return [x_train, y_train, x_test, y_test] 31 | 32 | print('> Loading data... ') 33 | df = pd.read_csv('./data/data(1).csv', header=None) 34 | X_train, y_train, X_test, y_test = data_pre(df) 35 | print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) 36 | 37 | history = [x for x in y_train] 38 | predictions = list() 39 | for t in range(len(y_test)): 40 | model = ARIMA(history, order=(1, 0, 0)) 41 | model_fit = model.fit(disp=0) 42 | output = model_fit.forecast() 43 | yhat = output[0] 44 | predictions.append(yhat) 45 | obs = y_test[t] 46 | history.append(obs) 47 | print('t=%d, predicted=%f, expected=%f' % (t, yhat, obs)) 48 | error = mean_squared_error(y_test, predictions) 49 | print('Test MSE: %.3f' % error) 50 | 51 | predictions=np.array(predictions) 52 | y = y_test * 20450.83322 + 4975.270898 53 | pre = predictions * 20450.83322 + 4975.270898 54 | 55 | plt.plot(y, label='True Data') 56 | plt.plot(pre, label='Predict') 57 | plt.legend() 58 | plt.show() 59 | -------------------------------------------------------------------------------- /RF.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Jun 12 10:49:01 2018 4 | 5 | @author: GY 6 | """ 7 | import pandas as pd 8 | import numpy as np 9 | import matplotlib.pyplot as plt 10 | from sklearn.ensemble import RandomForestRegressor 11 | from sklearn.metrics import mean_squared_error 12 | 13 | 14 | def data_pre(data): 15 | """ 16 | :param data: 17 | :param seq_len: 18 | :return: 19 | """ 20 | row = round(0.9 * data.shape[0]) 21 | data = data.values 22 | train = data[:int(row), :] 23 | x_train = train[:, :] 24 | y_train = train[:,16:17] 25 | x_test = data[int(row):, :] 26 | y_test = data[int(row):, 16:17] 27 | x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1])) 28 | x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1])) 29 | return [x_train, y_train, x_test, y_test] 30 | 31 | 32 | if __name__=="__main__": 33 | print('> Loading data... ') 34 | df = pd.read_csv('./data/data(1).csv', header=None) 35 | X_train, y_train, X_test, y_test = data_pre(df) 36 | print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) 37 | 38 | clf = RandomForestRegressor(n_estimators=100, criterion='mse', max_depth=None, 39 | min_samples_split=2, min_samples_leaf=1, 40 | max_features='auto', max_leaf_nodes=None, 41 | bootstrap=True, oob_score=False, n_jobs=1, 42 | random_state=None, verbose=0) 43 | clf.fit(X_train, y_train) 44 | predictions = clf.predict(X_test) 45 | 46 | error = mean_squared_error(y_test, predictions) 47 | print('Random Forest Test MSE: %.3f' % error) 48 | 49 | y = y_test * 20450.83322 + 4975.270898 50 | p = predictions * 20450.83322 + 4975.270898 51 | 52 | plt.plot(y, label='True Data') 53 | plt.plot(p, label='Random Forest Predict') 54 | plt.title('Random Forest Regression') 55 | plt.legend() 56 | plt.show() 57 | -------------------------------------------------------------------------------- /SVM.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Jun 11 20:27:29 2018 4 | 5 | @author: GY 6 | """ 7 | from sklearn import svm 8 | import pandas as pd 9 | import numpy as np 10 | import matplotlib.pyplot as plt 11 | from sklearn.metrics import mean_squared_error 12 | 13 | def data_pre(data): 14 | """ 15 | :param data: 16 | :param seq_len: 17 | :return: 18 | """ 19 | row = round(0.9 * data.shape[0]) 20 | data = data.values 21 | train = data[:int(row), :] 22 | x_train = train[:, :] 23 | y_train = train[:,16] 24 | x_test = data[int(row):, :] 25 | y_test = data[int(row):, 16] 26 | x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1])) 27 | x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1])) 28 | return [x_train, y_train, x_test, y_test] 29 | 30 | def predict_ts(X_train, y_train, X_test, y_test): 31 | 32 | svr_poly = svm.SVR(kernel='poly', C=1e2, degree=2) 33 | svr_rbf = svm.SVR(kernel='rbf', C=1e2, gamma=0.1) 34 | svr_poly.fit(X_train,y_train) 35 | svr_rbf.fit(X_train,y_train) 36 | p1 = svr_rbf.predict(X_test) 37 | p3 = svr_rbf.predict(X_test) 38 | 39 | error = mean_squared_error(y_test, p1) 40 | print('RBF model Test MSE: %.3f' % error) 41 | error2 = mean_squared_error(y_test, p3) 42 | print('Polinomial model Test MSE: %.3f' % error2) 43 | 44 | y_test = y_test * 20450.83322 + 4975.270898 45 | p1 = p1 * 20450.83322 + 4975.270898 46 | p3 = p3 * 20450.83322 + 4975.270898 47 | 48 | plt.plot(y_test, label='True Data') 49 | plt.plot(p1, label='RBF model Predict') 50 | plt.title('Support Vector Regression(RBF)') 51 | plt.legend() 52 | plt.show() 53 | 54 | plt.plot(y_test, label='True Data') 55 | plt.plot(p3, label='Polynomial model Predic') 56 | plt.title('Support Vector Regression(Polynomial)') 57 | plt.legend() 58 | plt.show() 59 | 60 | if __name__=="__main__": 61 | print('> Loading data... ') 62 | df = pd.read_csv('./data/data(1).csv', header=None) 63 | X_train, y_train, X_test, y_test = data_pre(df) 64 | print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) 65 | predict_ts(X_train, y_train, X_test, y_test) 66 | -------------------------------------------------------------------------------- /train_CNN.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu May 31 15:46:25 2018 4 | 5 | @author: GY 6 | """ 7 | import time 8 | import matplotlib.pyplot as plt 9 | import pandas as pd 10 | import yaml 11 | import CNN 12 | 13 | 14 | def plot_results(predicted_data, true_data): 15 | fig = plt.figure(facecolor='white') 16 | ax = fig.add_subplot(111) 17 | ax.plot(true_data, label='True Data') 18 | plt.plot(predicted_data, label='Prediction') 19 | plt.legend() 20 | plt.show() 21 | 22 | 23 | def plot_results_multiple(predicted_data, true_data, prediction_len): 24 | print('Start ploting') 25 | fig = plt.figure(facecolor='white') 26 | ax = fig.add_subplot(111) 27 | ax.plot(true_data, label='True Data') 28 | # Pad the list of predictions to shift it in the graph to it's correct start 29 | for i, data in enumerate(predicted_data): 30 | if i < 10: 31 | continue 32 | padding = [None for p in range(i * prediction_len)] 33 | plt.plot(padding + data, label='Prediction') 34 | plt.legend() 35 | plt.show() 36 | 37 | 38 | # Main Run Thread 39 | if __name__ == '__main__': 40 | epochs = 5 41 | 42 | print('> Loading data... ') 43 | df = pd.read_csv('./data/data(1).csv', header=None) 44 | column = list(range(df.shape[1])) 45 | column.remove(16) 46 | column.append(16) 47 | df = df.ix[:, column] 48 | 49 | X_train, y_train, X_test, y_test = CNN.data_pre(df) 50 | print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) 51 | 52 | global_start_time = time.time() 53 | print('> Data Loaded. Compiling...') 54 | 55 | # build and fit model 56 | model = CNN.build_model() 57 | model = CNN.fit_model(X_train, y_train, model, batch_size=16, nb_epoch=1000, validation_split=0.2) 58 | print('Training duration (s) : ', time.time() - global_start_time) 59 | 60 | # Predict 61 | predicted, score = CNN.predict_point_by_point(X_test, y_test) 62 | print('Test score is: ', score) 63 | 64 | fig = plt.figure(facecolor='white') 65 | 66 | # plot result 67 | ax = fig.add_subplot(1, 1, 1) 68 | ax.plot(y_test, label='True Data') 69 | ax.plot(predicted, label='Predict') 70 | ax.legend() 71 | plt.show() 72 | -------------------------------------------------------------------------------- /train_cnn_lstm.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu May 31 18:57:58 2018 4 | 5 | @author: GY 6 | """ 7 | import time 8 | import matplotlib.pyplot as plt 9 | import pandas as pd 10 | from sklearn.preprocessing import MinMaxScaler 11 | import yaml 12 | import CNN_LSTM 13 | 14 | 15 | def plot_results(predicted_data, true_data): 16 | fig = plt.figure(facecolor='white') 17 | ax = fig.add_subplot(111) 18 | ax.plot(true_data, label='True Data') 19 | plt.plot(predicted_data, label='Prediction') 20 | plt.legend() 21 | plt.show() 22 | 23 | 24 | def plot_results_multiple(predicted_data, true_data, prediction_len): 25 | print('Start ploting') 26 | fig = plt.figure(facecolor='white') 27 | ax = fig.add_subplot(111) 28 | ax.plot(true_data, label='True Data') 29 | # Pad the list of predictions to shift it in the graph to it's correct start 30 | for i, data in enumerate(predicted_data): 31 | if i < 10: 32 | continue 33 | padding = [None for p in range(i * prediction_len)] 34 | plt.plot(padding + data, label='Prediction') 35 | plt.legend() 36 | plt.show() 37 | 38 | 39 | # Main Run Thread 40 | if __name__ == '__main__': 41 | epochs = 5 42 | 43 | print('> Loading data... ') 44 | df = pd.read_csv('./data/data(1).csv', header=None) 45 | column = list(range(df.shape[1])) 46 | column.remove(16) 47 | column.append(16) 48 | df = df.ix[:, column] 49 | values = df.values.astype('float32') 50 | 51 | X_train, y_train, X_test, y_test = CNN_LSTM.data_pre(df) 52 | print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) 53 | 54 | # fit model 55 | global_start_time = time.time() 56 | print('> Data Loaded. Compiling...') 57 | 58 | model = CNN_LSTM.build_model() 59 | model = CNN_LSTM.fit_model(X_train, y_train, model, batch_size=16, nb_epoch=1000, validation_split=0.2) 60 | print('Training duration (s) : ', time.time() - global_start_time) 61 | 62 | # Predict 63 | predicted, score = CNN_LSTM.predict_point_by_point(X_test, y_test) 64 | print('Test score is: ', score) 65 | 66 | y = y_test*20446.87563+4975.270898 67 | pre = predicted*20446.87563+4975.270898 68 | 69 | fig = plt.figure(facecolor='white') 70 | ax = fig.add_subplot(1, 1, 1) 71 | ax.plot(y, label='True Data') 72 | ax.plot(pre, label='Predict') 73 | ax.legend() 74 | plt.show() 75 | -------------------------------------------------------------------------------- /train_cnn_lstm_multi.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Jun 4 23:47:14 2018 4 | 5 | @author: GY 6 | """ 7 | import time 8 | import matplotlib.pyplot as plt 9 | import pandas as pd 10 | import numpy as np 11 | from sklearn.preprocessing import MinMaxScaler 12 | import yaml 13 | import tensorboard 14 | import CNN_LSTM_6 15 | 16 | 17 | def plot_results(predicted_data, true_data): 18 | fig = plt.figure(facecolor='white') 19 | ax = fig.add_subplot(111) 20 | ax.plot(true_data, label='True Data') 21 | plt.plot(predicted_data, label='Prediction') 22 | plt.legend() 23 | plt.show() 24 | 25 | 26 | def plot_results_multiple(predicted_data, true_data, prediction_len): 27 | print('Start ploting') 28 | fig = plt.figure(facecolor='white') 29 | ax = fig.add_subplot(111) 30 | ax.plot(true_data, label='True Data') 31 | # Pad the list of predictions to shift it in the graph to it's correct start 32 | for i, data in enumerate(predicted_data): 33 | if i < 10: 34 | continue 35 | padding = [None for p in range(i * prediction_len)] 36 | plt.plot(padding + data, label='Prediction') 37 | plt.legend() 38 | plt.show() 39 | 40 | 41 | # Main Run Thread 42 | if __name__ == '__main__': 43 | epochs = 5 44 | 45 | print('> Loading data... ') 46 | df = pd.read_csv('./data/data(1).csv', header=None) 47 | values = df.values.astype('float32') 48 | 49 | X_train, y_train, X_test, y_test = CNN_LSTM_6.data_pre(df) 50 | print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) 51 | 52 | # fit model 53 | global_start_time = time.time() 54 | print('> Data Loaded. Compiling...') 55 | 56 | model = CNN_LSTM_6.build_model() 57 | model = CNN_LSTM_6.fit_model(X_train, y_train, model, batch_size=128, nb_epoch=1000, validation_split=0.2) 58 | print('Training duration (s) : ', time.time() - global_start_time) 59 | 60 | # Predict 61 | predicted, score = CNN_LSTM_6.predict_point_by_point(X_test, y_test) 62 | print('Test score is: ', score) 63 | 64 | fig = plt.figure(facecolor='white') 65 | for i in range(6): 66 | ax = fig.add_subplot(2, 3, (i+1)) 67 | ax.plot(np.absolute(y_test[:, i]-predicted[:, i]),label='MAE') 68 | ax.legend() 69 | plt.show() 70 | 71 | fig = plt.figure(facecolor='white') 72 | for i in range(6): 73 | ax = fig.add_subplot(2, 3, (i+1)) 74 | ax.plot(y[:, i], label='True Data') 75 | ax.plot(pre[:, i], label='Predict') 76 | ax.legend() 77 | plt.show() 78 | -------------------------------------------------------------------------------- /train_lstm.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu May 31 18:52:57 2018 4 | @author: GY 5 | """ 6 | 7 | import time 8 | import matplotlib.pyplot as plt 9 | import pandas as pd 10 | from sklearn.preprocessing import MinMaxScaler 11 | import yaml 12 | import lstm 13 | 14 | 15 | def plot_results(predicted_data, true_data): 16 | fig = plt.figure(facecolor='white') 17 | ax = fig.add_subplot(111) 18 | ax.plot(true_data, label='True Data') 19 | plt.plot(predicted_data, label='Prediction') 20 | plt.legend() 21 | plt.show() 22 | 23 | 24 | def plot_results_multiple(predicted_data, true_data, prediction_len): 25 | print('Start ploting') 26 | fig = plt.figure(facecolor='white') 27 | ax = fig.add_subplot(111) 28 | ax.plot(true_data, label='True Data') 29 | # Pad the list of predictions to shift it in the graph to it's correct start 30 | for i, data in enumerate(predicted_data): 31 | if i < 10: 32 | continue 33 | padding = [None for p in range(i * prediction_len)] 34 | plt.plot(padding + data, label='Prediction') 35 | plt.legend() 36 | plt.show() 37 | 38 | 39 | # Main Run Thread 40 | if __name__ == '__main__': 41 | epochs = 5 42 | seq_len = 5 43 | 44 | print('> Loading data... ') 45 | df = pd.read_csv('./data/data(1).csv', header=None) 46 | values = df.values.astype('float32') 47 | # normalize features 48 | scaler = MinMaxScaler(feature_range=(-1, 1)) 49 | scaled = scaler.fit_transform(values) 50 | scaled = pd.DataFrame(scaled) 51 | 52 | X_train, y_train, X_test, y_test = lstm.data_pre(scaled, seq_len) 53 | print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) 54 | 55 | # fit model 56 | global_start_time = time.time() 57 | print('> Data Loaded. Compiling...') 58 | # layers [X_input feature dim, LSTM[1].unit, LSTM[2].unit, output_dim] 59 | model = lstm.build_model(layers=[X_train.shape[-1], 20, 20, X_train.shape[-1]], sequence_length=seq_len) 60 | model = lstm.fit_model(X_train, y_train, model, batch_size=64, nb_epoch=1000, validation_split=0.2) 61 | print('Training duration (s) : ', time.time() - global_start_time) 62 | 63 | # Predict 64 | predicted = lstm.predict_point_by_point(X_test) 65 | predicted = scaler.inverse_transform(predicted) 66 | 67 | y_test = scaler.inverse_transform(y_test) 68 | 69 | fig = plt.figure(facecolor='white') 70 | for i in range(6): 71 | ax = fig.add_subplot(2, 3, (i+1)) 72 | ax.plot(y_test[:, i], label='True Data') 73 | ax.plot(predicted[:, i], label='Predict') 74 | ax.legend() 75 | plt.show() 76 | 77 | fig = plt.figure(facecolor='white') 78 | for i in range(4): 79 | ax = fig.add_subplot(2, 2, (i+1)) 80 | ax.plot(y_test[:, -(i+1)], label='True Data') 81 | ax.plot(predicted[:, -(i+1)], label='Predict') 82 | ax.legend() 83 | plt.show() 84 | -------------------------------------------------------------------------------- /tcn_a.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Jun 11 09:22:30 2018 4 | 5 | @author: GY 6 | """ 7 | import keras 8 | import numpy as np 9 | import pandas as pd 10 | import matplotlib.pyplot as plt 11 | from keras.utils import plot_model 12 | from tcn import tcn 13 | 14 | 15 | def data_pre(data): 16 | """ 17 | :param data: 18 | :param seq_len: 19 | :return: 20 | """ 21 | # sequence_length = sequence_length + 1 22 | row = round(0.9 * data.shape[0]) 23 | data = data.values 24 | train = data[:int(row), :] 25 | x_train = train[:, :] 26 | y_train = train[:,16:17] 27 | x_test = data[int(row):, :] 28 | y_test = data[int(row):, 16:17] 29 | x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1)) 30 | x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1)) 31 | return [x_train, y_train, x_test, y_test] 32 | 33 | 34 | def run_task(): 35 | model, param_str = tcn.dilated_tcn(output_slice_index='last', 36 | num_feat=X_train.shape[2], 37 | num_classes=0, 38 | nb_filters=24, 39 | kernel_size=3, 40 | dilatations=[1, 2, 4, 8], 41 | nb_stacks=8, 42 | max_len=X_train.shape[1], 43 | activation='norm_relu', 44 | use_skip_connections=False, 45 | return_param_str=True, 46 | regression=True) 47 | 48 | print('x_train.shape = {}'.format(X_train.shape)) 49 | print('y_train.shape = {}'.format(y_train.shape)) 50 | plot_model(model, to_file='tcn.png') 51 | model.summary() 52 | 53 | history = model.fit(X_train, y_train, epochs=500, batch_size=128, validation_split=0.2) 54 | 55 | plt.plot() 56 | plt.plot(history.history['loss'], label='Trainig Loss') 57 | plt.plot(history.history['val_loss'], label='Validation Loss') 58 | plt.title('Training') 59 | plt.xlabel('epoch') 60 | plt.ylabel('MSE') 61 | plt.legend() 62 | plt.show() 63 | 64 | predicted = model.predict(X_test) 65 | score = model.evaluate(X_test, y_test, batch_size=128) 66 | print('Test score is: {}!\n'.format(score)) 67 | 68 | fig = plt.figure(facecolor='white') 69 | ax = fig.add_subplot(1, 1, 1) 70 | ax.plot(np.absolute(y_test-predicted), label='MAE') 71 | ax.legend() 72 | plt.show() 73 | 74 | y = y_test * 20450.83322 + 4975.270898 75 | pre = predicted * 20450.83322 + 4975.270898 76 | 77 | fig = plt.figure(facecolor='white') 78 | ax = fig.add_subplot(1, 1, 1) 79 | ax.plot(y, label='True Data') 80 | ax.plot(pre, label='Predict') 81 | ax.legend() 82 | plt.show() 83 | return model, param_str 84 | 85 | 86 | if __name__ == '__main__': 87 | print('> Loading data... ') 88 | df = pd.read_csv('./data/data(1).csv', header=None) 89 | X_train, y_train, X_test, y_test = data_pre(df) 90 | print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) 91 | 92 | model, param_str = run_task() 93 | -------------------------------------------------------------------------------- /LSTM.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Jun 11 19:05:10 2018 4 | @author: GY 5 | """ 6 | import os 7 | import time 8 | import warnings 9 | import numpy as np 10 | from numpy import newaxis 11 | from keras.layers.core import Dense, Activation, Dropout 12 | from keras.layers.recurrent import LSTM 13 | from keras.models import Sequential, model_from_yaml 14 | import yaml 15 | 16 | 17 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Hide messy TensorFlow warnings 18 | warnings.filterwarnings("ignore") # Hide messy Numpy warnings 19 | 20 | 21 | def data_pre(data, sequence_length): 22 | """ 23 | :param data: 24 | :param seq_len: 25 | :return: 26 | """ 27 | result = [] 28 | for index in range(len(data) - sequence_length): 29 | result.append(data.ix[index: index + sequence_length].values) 30 | result = np.array(result) 31 | row = round(0.9 * result.shape[0]) 32 | train = result[:int(row), :, :] 33 | np.random.shuffle(train) 34 | x_train = train[:, :-1, :] 35 | y_train = train[:, -1, :] 36 | x_test = result[int(row):, :-1, :] 37 | y_test = result[int(row):, -1, :] 38 | x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], x_train.shape[2])) 39 | x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], x_test.shape[2])) 40 | return [x_train, y_train, x_test, y_test] 41 | 42 | 43 | def build_model(layers, sequence_length): 44 | model = Sequential() 45 | # print(layers, sequence_length) 46 | model.add(LSTM(units=layers[1], 47 | input_shape=(sequence_length, layers[0]), 48 | return_sequences=True)) 49 | model.add(Dropout(0.2)) 50 | 51 | model.add(LSTM(units=layers[2], 52 | return_sequences=False)) 53 | model.add(Dropout(0.2)) 54 | 55 | model.add(Dense(layers[3], activation='linear')) 56 | 57 | start = time.time() 58 | model.compile(loss="mse", optimizer="rmsprop",metrics=["mse"]) 59 | print("> Compilation Time : ", time.time() - start) 60 | return model 61 | 62 | 63 | def fit_model(X_train, y_train, model, batch_size=128, nb_epoch=10, validation_split=0.2): 64 | model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, validation_split=validation_split) 65 | yaml_string = model.to_yaml() 66 | with open('lstm/lstm.yml', 'w') as outfile: 67 | outfile.write(yaml.dump(yaml_string, default_flow_style=True)) 68 | model.save_weights('lstm/lstm.h5') 69 | return model 70 | 71 | 72 | def predict_point_by_point(data): 73 | # Predict each timestep given the last sequence of true data, in effect only predicting 1 step ahead each time 74 | print('loading model....') 75 | with open('lstm/lstm.yml', 'r') as f: 76 | yaml_string = yaml.load(f) 77 | model = model_from_yaml(yaml_string) 78 | print('loading weights...') 79 | model.load_weights('lstm/lstm.h5') 80 | model.compile(loss='mean_squared_error', optimizer='adam') 81 | predicted = model.predict(data) 82 | 83 | return predicted 84 | 85 | 86 | def predict_sequence_full(model, data, window_size): 87 | # Shift the window by 1 new prediction each time, re-run predictions on new window 88 | curr_frame = data[0] 89 | predicted = [] 90 | for i in range(len(data)): 91 | predicted.append(model.predict(curr_frame[newaxis, :, :])[0, 0]) 92 | curr_frame = curr_frame[1:] 93 | curr_frame = np.insert(curr_frame, [window_size - 1], predicted[-1], axis=0) 94 | return predicted 95 | 96 | 97 | def predict_sequences_multiple(model, data, window_size, prediction_len): 98 | # Predict sequence of 50 steps before shifting prediction run forward by 50 steps 99 | prediction_seqs = [] 100 | for i in range(int(len(data) / prediction_len)): 101 | curr_frame = data[i * prediction_len] 102 | predicted = [] 103 | for j in range(prediction_len): 104 | 105 | predicted.append(model.predict(curr_frame[newaxis, :, :])[0, 0]) 106 | curr_frame = curr_frame[1:] 107 | curr_frame = np.insert(curr_frame, [window_size - 1], predicted[-1], axis=0) 108 | prediction_seqs.append(predicted) 109 | return prediction_seqs 110 | 111 | -------------------------------------------------------------------------------- /CNN.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Jun 11 19:05:10 2018 4 | @author: GY 5 | """ 6 | import os 7 | import time 8 | import warnings 9 | import numpy as np 10 | from numpy import newaxis 11 | from keras.layers.core import Dense, Activation, Dropout, Reshape, Flatten 12 | from keras.layers.convolutional import Conv2D 13 | from keras.layers import GlobalAveragePooling2D,AveragePooling2D 14 | from keras.models import Sequential, model_from_yaml 15 | import yaml 16 | 17 | 18 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Hide messy TensorFlow warnings 19 | warnings.filterwarnings("ignore") # Hide messy Numpy warnings 20 | 21 | 22 | def data_pre(data): 23 | """ 24 | :param data: 25 | :param seq_len: 26 | :return: 27 | """ 28 | row = round(0.9 * data.shape[0]) 29 | data = data.values 30 | train = data[:int(row), :] 31 | x_train = train[:, :-1] 32 | y_train = train[:, -1] 33 | x_test = data[int(row):, :-1] 34 | y_test = data[int(row):, -1] 35 | x_train = np.reshape(x_train, (x_train.shape[0],1,1, x_train.shape[1])) 36 | x_test = np.reshape(x_test, (x_test.shape[0],1,1, x_test.shape[1])) 37 | return [x_train, y_train, x_test, y_test] 38 | 39 | 40 | def build_model(): 41 | model = Sequential() 42 | model.add(Conv2D(nb_filter=32, nb_row=1, nb_col=3, init='glorot_uniform', 43 | activation='relu', weights=None, border_mode='valid', 44 | subsample=(1, 1), dim_ordering='th', W_regularizer=None, 45 | b_regularizer=None, activity_regularizer=None, W_constraint=None, 46 | b_constraint=None, bias=True, input_shape=(1, 1, 18))) 47 | model.add(AveragePooling2D(pool_size=(1, 2), strides=None, border_mode='valid', dim_ordering='th')) 48 | model.add(Conv2D(nb_filter=16, nb_row=1, nb_col=3, init='glorot_uniform', 49 | activation='relu', weights=None, border_mode='valid', 50 | subsample=(1, 1), dim_ordering='th', W_regularizer=None, 51 | b_regularizer=None, activity_regularizer=None, W_constraint=None, 52 | b_constraint=None, bias=True)) 53 | model.add(AveragePooling2D(pool_size=(1, 2), strides=None, border_mode='valid', dim_ordering='th')) 54 | model.add(Conv2D(nb_filter=8, nb_row=1, nb_col=3, init='glorot_uniform', 55 | activation='relu', weights=None, border_mode='valid', 56 | subsample=(1, 1), dim_ordering='th', W_regularizer=None, 57 | b_regularizer=None, activity_regularizer=None, W_constraint=None, 58 | b_constraint=None, bias=True)) 59 | 60 | model.add(Flatten()) 61 | model.add(Dense(1024)) 62 | model.add(Activation('relu')) 63 | model.add(Dropout(0.5)) 64 | model.add(Dense(1)) 65 | model.add(Activation('linear')) 66 | 67 | start = time.time() 68 | model.compile(loss="mse", optimizer="adam") 69 | print("> Compilation Time : ", time.time() - start) 70 | return model 71 | 72 | 73 | def fit_model(X_train, y_train, model, batch_size=128, nb_epoch=10, validation_split=0.2): 74 | model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, validation_split=validation_split) 75 | yaml_string = model.to_yaml() 76 | with open('cnn/cnn.yml', 'w') as outfile: 77 | outfile.write(yaml.dump(yaml_string, default_flow_style=True)) 78 | model.save_weights('cnn/cnn.h5') 79 | return model 80 | 81 | 82 | def predict_point_by_point(data, label): 83 | # Predict each timestep given the last sequence of true data, in effect only predicting 1 step ahead each time 84 | print('loading model....') 85 | with open('cnn/cnn.yml', 'r') as f: 86 | yaml_string = yaml.load(f) 87 | model = model_from_yaml(yaml_string) 88 | print('loading weights...') 89 | model.load_weights('cnn/cnn.h5') 90 | model.compile(loss='mean_squared_error', optimizer='adam') 91 | predicted = model.predict(data) 92 | score = model.evaluate(data, label, batch_size=128) 93 | return predicted, score 94 | 95 | 96 | def predict_sequence_full(model, data, window_size): 97 | # Shift the window by 1 new prediction each time, re-run predictions on new window 98 | curr_frame = data[0] 99 | predicted = [] 100 | for i in range(len(data)): 101 | predicted.append(model.predict(curr_frame[newaxis, :, :])[0, 0]) 102 | curr_frame = curr_frame[1:] 103 | curr_frame = np.insert(curr_frame, [window_size - 1], predicted[-1], axis=0) 104 | return predicted 105 | 106 | 107 | def predict_sequences_multiple(model, data, window_size, prediction_len): 108 | # Predict sequence of 50 steps before shifting prediction run forward by 50 steps 109 | prediction_seqs = [] 110 | for i in range(int(len(data) / prediction_len)): 111 | curr_frame = data[i * prediction_len] 112 | predicted = [] 113 | for j in range(prediction_len): 114 | 115 | predicted.append(model.predict(curr_frame[newaxis, :, :])[0, 0]) 116 | curr_frame = curr_frame[1:] 117 | curr_frame = np.insert(curr_frame, [window_size - 1], predicted[-1], axis=0) 118 | prediction_seqs.append(predicted) 119 | return prediction_seqs 120 | -------------------------------------------------------------------------------- /CNN_LSTM.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu May 31 18:52:57 2018 4 | 5 | @author: GY 6 | """ 7 | 8 | import os 9 | import time 10 | import warnings 11 | import numpy as np 12 | from numpy import newaxis 13 | from keras.layers.core import Dense, Activation, Dropout, Reshape, Flatten, Permute 14 | from keras.layers.convolutional import Conv2D,Conv1D 15 | from keras.layers.recurrent import LSTM 16 | from keras.layers.wrappers import TimeDistributed 17 | from keras.layers import GlobalAveragePooling2D,AveragePooling1D,MaxPooling1D, AveragePooling2D,GlobalAveragePooling1D 18 | from keras.models import Sequential, model_from_yaml 19 | import yaml 20 | 21 | 22 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Hide messy TensorFlow warnings 23 | warnings.filterwarnings("ignore") # Hide messy Numpy warnings 24 | 25 | 26 | def data_pre(data): 27 | """ 28 | :param data: 29 | :param seq_len: 30 | :return: 31 | """ 32 | row = round(0.9 * data.shape[0]) 33 | data = data.values 34 | train = data[:int(row), :] 35 | x_train = train[:, :-1] 36 | y_train = train[:, -1] 37 | x_test = data[int(row):, :-1] 38 | y_test = data[int(row):, -1] 39 | x_train = np.reshape(x_train, (x_train.shape[0], 1, 1, 1, x_train.shape[1])) 40 | x_test = np.reshape(x_test, (x_test.shape[0], 1, 1, 1, x_test.shape[1])) 41 | return [x_train, y_train, x_test, y_test] 42 | 43 | 44 | def build_model(): 45 | model = Sequential() 46 | model.add(TimeDistributed(Conv2D(nb_filter=32, nb_row=1, nb_col=3, init='glorot_uniform', 47 | activation='relu', weights=None, border_mode='valid', 48 | subsample=(1, 1), dim_ordering='th', W_regularizer=None, 49 | b_regularizer=None, activity_regularizer=None, W_constraint=None, 50 | b_constraint=None, bias=True), input_shape=(None, 1, 1, 18))) 51 | 52 | model.add(TimeDistributed(AveragePooling2D(pool_size=(1, 2), strides=None, border_mode='valid', dim_ordering='th'))) 53 | model.add(TimeDistributed(Conv2D(nb_filter=16, nb_row=1, nb_col=3, init='glorot_uniform', 54 | activation='relu', weights=None, border_mode='valid', 55 | subsample=(1, 1), dim_ordering='th', W_regularizer=None, 56 | b_regularizer=None, activity_regularizer=None, W_constraint=None, 57 | b_constraint=None, bias=True))) 58 | model.add(TimeDistributed(AveragePooling2D(pool_size=(1, 2), strides=None, border_mode='valid', dim_ordering='th'))) 59 | model.add(TimeDistributed(Conv2D(nb_filter=8, nb_row=1, nb_col=3, init='glorot_uniform', 60 | activation='relu', weights=None, border_mode='valid', 61 | subsample=(1, 1), dim_ordering='th', W_regularizer=None, 62 | b_regularizer=None, activity_regularizer=None, W_constraint=None, 63 | b_constraint=None, bias=True))) 64 | model.add(TimeDistributed(Flatten())) 65 | model.add(TimeDistributed(Dense(1024))) 66 | model.add(TimeDistributed(Activation('relu'))) 67 | model.add(LSTM(32, return_sequences=True, activation='relu')) 68 | model.add(LSTM(32, return_sequences=True, activation='relu')) 69 | model.add(TimeDistributed(Dropout(0.25))) 70 | model.add(TimeDistributed(Activation('relu'))) 71 | model.add(TimeDistributed(Dense(1))) 72 | model.add(TimeDistributed(Activation('linear'))) 73 | model.add(GlobalAveragePooling1D(name="global_avg")) 74 | model.summary() 75 | start = time.time() 76 | model.compile(loss="mse", optimizer="adam") 77 | print("> Compilation Time : ", time.time() - start) 78 | return model 79 | 80 | 81 | def fit_model(X_train, y_train, model, batch_size=128, nb_epoch=10, validation_split=0.2): 82 | model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, validation_split=validation_split) 83 | yaml_string = model.to_yaml() 84 | with open('cnn_lstm/cnn_lstm.yml', 'w') as outfile: 85 | outfile.write(yaml.dump(yaml_string, default_flow_style=True)) 86 | model.save_weights('cnn_lstm/cnn_lstm.h5') 87 | return model 88 | 89 | 90 | def predict_point_by_point(data, label): 91 | # Predict each timestep given the last sequence of true data, in effect only predicting 1 step ahead each time 92 | print('loading model....') 93 | with open('cnn_lstm/cnn_lstm.yml', 'r') as f: 94 | yaml_string = yaml.load(f) 95 | model = model_from_yaml(yaml_string) 96 | print('loading weights...') 97 | model.load_weights('cnn_lstm/cnn_lstm.h5') 98 | model.compile(loss='mean_squared_error', optimizer='adam') 99 | predicted = model.predict(data) 100 | score = model.evaluate(data, label, batch_size=128) 101 | return predicted, score 102 | 103 | 104 | def predict_sequence_full(model, data, window_size): 105 | # Shift the window by 1 new prediction each time, re-run predictions on new window 106 | curr_frame = data[0] 107 | predicted = [] 108 | for i in range(len(data)): 109 | predicted.append(model.predict(curr_frame[newaxis, :, :])[0, 0]) 110 | curr_frame = curr_frame[1:] 111 | curr_frame = np.insert(curr_frame, [window_size - 1], predicted[-1], axis=0) 112 | return predicted 113 | 114 | 115 | def predict_sequences_multiple(model, data, window_size, prediction_len): 116 | # Predict sequence of 50 steps before shifting prediction run forward by 50 steps 117 | prediction_seqs = [] 118 | for i in range(int(len(data) / prediction_len)): 119 | curr_frame = data[i * prediction_len] 120 | predicted = [] 121 | for j in range(prediction_len): 122 | 123 | predicted.append(model.predict(curr_frame[newaxis, :, :])[0, 0]) 124 | curr_frame = curr_frame[1:] 125 | curr_frame = np.insert(curr_frame, [window_size - 1], predicted[-1], axis=0) 126 | prediction_seqs.append(predicted) 127 | return prediction_seqs 128 | -------------------------------------------------------------------------------- /CNN_LSTM_multi_output.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Jun 4 23:47:44 2018 4 | 5 | @author: GY 6 | """ 7 | 8 | import os 9 | import time 10 | import warnings 11 | import numpy as np 12 | from numpy import newaxis 13 | from keras.layers.core import Dense, Activation, Dropout, Reshape, Flatten, Permute 14 | from keras.layers.convolutional import Conv2D,Conv1D 15 | from keras.layers.recurrent import LSTM 16 | from keras.layers.wrappers import TimeDistributed 17 | from keras.layers import AveragePooling2D, MaxPooling2D, GlobalAveragePooling1D, GlobalMaxPooling1D 18 | from keras.models import Sequential, model_from_yaml 19 | from keras.callbacks import History 20 | from keras.utils import plot_model 21 | import matplotlib.pyplot as plt 22 | import yaml 23 | 24 | 25 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Hide messy TensorFlow warnings 26 | warnings.filterwarnings("ignore") # Hide messy Numpy warnings 27 | 28 | 29 | def data_pre(data): 30 | """ 31 | :param data: 32 | :param seq_len: 33 | :return: 34 | """ 35 | row = round(0.9 * data.shape[0]) 36 | data = data.values 37 | train = data[:int(row), :] 38 | x_train = train[:, :] 39 | y_train = np.concatenate((train[:, 3:7],train[:,16:18]),axis=1) 40 | x_test = data[int(row):, :] 41 | y_test = np.concatenate((data[int(row):, 3:7],data[int(row):, 16:18]),axis=1) 42 | x_train = np.reshape(x_train, (x_train.shape[0], 1, 1, 1, x_train.shape[1])) 43 | x_test = np.reshape(x_test, (x_test.shape[0], 1, 1, 1, x_test.shape[1])) 44 | return [x_train, y_train, x_test, y_test] 45 | 46 | 47 | def build_model(): 48 | model = Sequential() 49 | model.add(TimeDistributed(Conv2D(nb_filter=512, nb_row=1, nb_col=3, init='glorot_uniform', 50 | activation='relu', weights=None, border_mode='valid', 51 | subsample=(1, 1), dim_ordering='th', W_regularizer=None, 52 | b_regularizer=None, activity_regularizer=None, W_constraint=None, 53 | b_constraint=None, bias=True), input_shape=(None, 1, 1, 19))) 54 | 55 | model.add(TimeDistributed(AveragePooling2D(pool_size=(1, 2), strides=None, border_mode='valid', dim_ordering='th'))) 56 | model.add(TimeDistributed(Conv2D(nb_filter=256, nb_row=1, nb_col=3, init='glorot_uniform', 57 | activation='relu', weights=None, border_mode='valid', 58 | subsample=(1, 1), dim_ordering='th', W_regularizer=None, 59 | b_regularizer=None, activity_regularizer=None, W_constraint=None, 60 | b_constraint=None, bias=True))) 61 | model.add(TimeDistributed(AveragePooling2D(pool_size=(1, 2), strides=None, border_mode='valid', dim_ordering='th'))) 62 | model.add(TimeDistributed(Conv2D(nb_filter=128, nb_row=1, nb_col=3, init='glorot_uniform', 63 | activation='relu', weights=None, border_mode='valid', 64 | subsample=(1, 1), dim_ordering='th', W_regularizer=None, 65 | b_regularizer=None, activity_regularizer=None, W_constraint=None, 66 | b_constraint=None, bias=True))) 67 | model.add(TimeDistributed(Flatten())) 68 | model.add(TimeDistributed(Dense(1024))) 69 | model.add(TimeDistributed(Activation('relu'))) 70 | model.add(LSTM(1024, return_sequences=True, activation='relu')) 71 | model.add(LSTM(1024, return_sequences=True, activation='relu')) 72 | model.add(TimeDistributed(Dropout(0.25))) 73 | model.add(TimeDistributed(Activation('relu'))) 74 | model.add(TimeDistributed(Dense(1024))) 75 | model.add(TimeDistributed(Activation('linear'))) 76 | model.add(GlobalAveragePooling1D(name="global_avg")) 77 | model.add(Dense(6)) 78 | model.add(Activation('linear')) 79 | model.summary() 80 | plot_model(model, to_file='model.png') 81 | start = time.time() 82 | model.compile(loss="mse", optimizer="adam") 83 | print("> Compilation Time : ", time.time() - start) 84 | return model 85 | 86 | 87 | def fit_model(X_train, y_train, model, batch_size=128, nb_epoch=10, validation_split=0.2): 88 | history = model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, validation_split=validation_split) 89 | plt.plot() 90 | plt.plot(history.history['loss'], label='Trainig Loss') 91 | plt.plot(history.history['val_loss'], label='Validation Loss') 92 | plt.title('Training') 93 | plt.xlabel('epoch') 94 | plt.ylabel('MSE') 95 | plt.legend() 96 | plt.show() 97 | yaml_string = model.to_yaml() 98 | with open('cnn_lstm/cnn_lstm.yml', 'w') as outfile: 99 | outfile.write(yaml.dump(yaml_string, default_flow_style=True)) 100 | model.save_weights('cnn_lstm/cnn_lstm.h5') 101 | return model 102 | 103 | 104 | def predict_point_by_point(data, label): 105 | # Predict each timestep given the last sequence of true data, in effect only predicting 1 step ahead each time 106 | print('loading model....') 107 | with open('cnn_lstm/cnn_lstm.yml', 'r') as f: 108 | yaml_string = yaml.load(f) 109 | model = model_from_yaml(yaml_string) 110 | print('loading weights...') 111 | model.load_weights('cnn_lstm/cnn_lstm.h5') 112 | model.compile(loss='mean_squared_error', optimizer='adam') 113 | predicted = model.predict(data) 114 | score = model.evaluate(data, label, batch_size=128) 115 | return predicted, score 116 | 117 | 118 | def predict_sequence_full(model, data, window_size): 119 | # Shift the window by 1 new prediction each time, re-run predictions on new window 120 | curr_frame = data[0] 121 | predicted = [] 122 | for i in range(len(data)): 123 | predicted.append(model.predict(curr_frame[newaxis, :, :])[0, 0]) 124 | curr_frame = curr_frame[1:] 125 | curr_frame = np.insert(curr_frame, [window_size - 1], predicted[-1], axis=0) 126 | return predicted 127 | 128 | 129 | def predict_sequences_multiple(model, data, window_size, prediction_len): 130 | # Predict sequence of 50 steps before shifting prediction run forward by 50 steps 131 | prediction_seqs = [] 132 | for i in range(int(len(data) / prediction_len)): 133 | curr_frame = data[i * prediction_len] 134 | predicted = [] 135 | for j in range(prediction_len): 136 | 137 | predicted.append(model.predict(curr_frame[newaxis, :, :])[0, 0]) 138 | curr_frame = curr_frame[1:] 139 | curr_frame = np.insert(curr_frame, [window_size - 1], predicted[-1], axis=0) 140 | prediction_seqs.append(predicted) 141 | return prediction_seqs 142 | --------------------------------------------------------------------------------