Add LSTM to prediction techniques

This commit is contained in:
didier
2021-02-13 23:31:22 +00:00
parent 59a374c995
commit be1c324511
2 changed files with 116 additions and 7 deletions

View File

@@ -123,11 +123,13 @@ def mlp(l_args, s_ticker, s_interval, df_stock):
model = build_neural_network_model(MultiLayer_Perceptron, ns_parser.n_inputs, ns_parser.n_days)
model.compile(optimizer=ns_parser.s_optimizer, loss=ns_parser.s_loss)
print(model.summary())
print("")
# Train our model
model.fit(stock_x, stock_y, epochs=ns_parser.n_epochs, verbose=0);
model.fit(stock_x, stock_y, epochs=ns_parser.n_epochs, verbose=1);
print("")
print(model.summary())
print("")
# Prediction
yhat = model.predict(stock_train_data[-ns_parser.n_inputs:].reshape(1, ns_parser.n_inputs), verbose=0)
@@ -220,11 +222,13 @@ def rnn(l_args, s_ticker, s_interval, df_stock):
model = build_neural_network_model(Recurrent_Neural_Network, ns_parser.n_inputs, ns_parser.n_days)
model.compile(optimizer=ns_parser.s_optimizer, loss=ns_parser.s_loss)
print(model.summary())
print("")
# Train our model
model.fit(stock_x, stock_y, epochs=ns_parser.n_epochs, verbose=0);
model.fit(stock_x, stock_y, epochs=ns_parser.n_epochs, verbose=1);
print("")
print(model.summary())
print("")
# Prediction
yhat = model.predict(stock_train_data[-ns_parser.n_inputs:].reshape(1, ns_parser.n_inputs, 1), verbose=0)
@@ -261,3 +265,104 @@ def rnn(l_args, s_ticker, s_interval, df_stock):
except:
print("")
# -------------------------------------------------- LSTM --------------------------------------------------
def lstm(l_args, s_ticker, s_interval, df_stock):
parser = argparse.ArgumentParser(prog='lstm',
description="""Long-Short Term Memory. """)
parser.add_argument('-d', "--days", action="store", dest="n_days", type=check_positive, default=5, help='prediction days')
parser.add_argument('-i', "--input", action="store", dest="n_inputs", type=check_positive, default=40, help='number of days to use for prediction')
parser.add_argument('-e', "--epochs", action="store", dest="n_epochs", type=check_positive, default=200, help='number of training epochs')
parser.add_argument('-f', "--freq", action="store", dest="n_jumps", type=check_positive, default=1, help='how')
parser.add_argument('-p', "--pp", action="store", dest="s_preprocessing", default='normalization', choices=['normalization', 'standardization', 'none'], help='Pre Processing data')
parser.add_argument('-o', "--optimizer", action="store", dest="s_optimizer", default='adam', help='Optimizer technique',
choices=['adam', 'adagrad', 'adadelta', 'adamax', 'ftrl', 'nadam', 'optimizer', 'rmsprop', 'sgd'])
parser.add_argument('-l', "--loss", action="store", dest="s_loss", default='mae',
choices=['mae', 'mape', 'mse', 'msle'], help='Loss function')
try:
(ns_parser, l_unknown_args) = parser.parse_known_args(l_args)
if l_unknown_args:
print(f"The following args couldn't be interpreted: {l_unknown_args}\n")
return
# Pre-process data
if ns_parser.s_preprocessing == 'standardization':
scaler = StandardScaler()
stock_train_data = scaler.fit_transform(np.array(df_stock['5. adjusted close'].values.reshape(-1, 1)))
elif ns_parser.s_preprocessing == 'normalization':
scaler = MinMaxScaler()
stock_train_data = scaler.fit_transform(np.array(df_stock['5. adjusted close'].values.reshape(-1, 1)))
else: # No pre-processing
stock_train_data = np.array(df_stock['5. adjusted close'].values.reshape(-1, 1))
# Split training data for the neural network
stock_x, stock_y = splitTrain.split_train(stock_train_data, ns_parser.n_inputs, ns_parser.n_days, numJumps=ns_parser.n_jumps)
stock_x = np.array(stock_x)
stock_x = np.reshape(stock_x, (stock_x.shape[0], stock_x.shape[1], 1))
stock_y = np.array(stock_y)
stock_y = np.reshape(stock_y, (stock_y.shape[0], stock_y.shape[1], 1))
# Build Neural Network model
Long_Short_Term_Memory \
= [ {'LSTM':
{'units':25, 'activation':'tanh', 'return_sequences':True} },
{'LSTM':
{'units':50, 'activation':'tanh', 'return_sequences':True} },
{'LSTM':
{'units':30, 'activation':'tanh', 'return_sequences':True} },
{'LSTM':
{'units':20, 'activation':'tanh', 'return_sequences':True} },
{'LSTM':
{'units':15, 'activation':'tanh', 'return_sequences':False} },
{'Dense':
{'activation':'linear'} }]
model = build_neural_network_model(Long_Short_Term_Memory, ns_parser.n_inputs, ns_parser.n_days)
model.compile(optimizer=ns_parser.s_optimizer, loss=ns_parser.s_loss)
# Train our model
model.fit(stock_x, stock_y, epochs=ns_parser.n_epochs, verbose=1);
print("")
print(model.summary())
print("")
# Prediction
yhat = model.predict(stock_train_data[-ns_parser.n_inputs:].reshape(1, ns_parser.n_inputs, 1), verbose=0)
# Re-scale the data back
if (ns_parser.s_preprocessing == 'standardization') or (ns_parser.s_preprocessing == 'normalization'):
y_pred_test_t = scaler.inverse_transform(yhat.tolist())
else:
y_pred_test_t = yhat
l_pred_days = get_next_stock_market_days(last_stock_day=df_stock['5. adjusted close'].index[-1], n_next_days=ns_parser.n_days)
df_pred = pd.Series(y_pred_test_t[0].tolist(), index=l_pred_days, name='Price')
# Plotting
plt.plot(df_stock.index, df_stock['5. adjusted close'], lw=3)
plt.title(f"LSTM on {s_ticker} - {ns_parser.n_days} days prediction")
plt.xlim(df_stock.index[0], get_next_stock_market_days(df_pred.index[-1], 1)[-1])
plt.xlabel('Time')
plt.ylabel('Share Price ($)')
plt.grid(b=True, which='major', color='#666666', linestyle='-')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.plot([df_stock.index[-1], df_pred.index[0]], [df_stock['5. adjusted close'].values[-1], df_pred.values[0]], lw=1, c='tab:green', linestyle='--')
plt.plot(df_pred.index, df_pred, lw=2, c='tab:green')
plt.axvspan(df_stock.index[-1], df_pred.index[-1], facecolor='tab:orange', alpha=0.2)
xmin, xmax, ymin, ymax = plt.axis()
plt.vlines(df_stock.index[-1], ymin, ymax, colors='k', linewidth=3, linestyle='--', color='k')
plt.show()
# Print prediction data
df_pred = df_pred.apply(lambda x: f"{x:.2f} $")
print(df_pred.to_string())
print("")
except:
print("")

View File

@@ -42,6 +42,7 @@ def print_prediction(s_ticker, s_start, s_interval):
print(" prophet Facebook's prophet prediction")
print(" mlp MultiLayer Perceptron")
print(" rnn Recurrent Neural Network")
print(" lstm Long-Short Term Memory")
print("")
@@ -53,7 +54,7 @@ def pred_menu(df_stock, s_ticker, s_start, s_interval):
pred_parser.add_argument('cmd', choices=['help', 'q', 'quit',
'sma', 'knn',
'linear', 'quadratic', 'cubic', 'regression',
'arima', 'prophet', 'mlp', 'rnn'])
'arima', 'prophet', 'mlp', 'rnn', 'lstm'])
print_prediction(s_ticker, s_start, s_interval)
@@ -117,6 +118,9 @@ def pred_menu(df_stock, s_ticker, s_start, s_interval):
elif ns_known_args.cmd == 'rnn':
neural_networks.rnn(l_args, s_ticker, s_interval, df_stock)
elif ns_known_args.cmd == 'lstm':
neural_networks.lstm(l_args, s_ticker, s_interval, df_stock)
# ------------------------------------------------------------------------------------------------------------
else:
print("Command not recognized!")