Improve pred files, and non-blocking plot

This commit is contained in:
didier
2021-03-06 14:58:37 +00:00
parent bf1a2a5c33
commit 76cfe15102
7 changed files with 60 additions and 55 deletions

View File

@@ -4,11 +4,15 @@ import pandas as pd
from pandas.plotting import register_matplotlib_converters
import pmdarima
from statsmodels.tsa.arima.model import ARIMA
from gamestonk_terminal.helper_funcs import check_positive, get_next_stock_market_days
from gamestonk_terminal.helper_funcs import (
check_positive,
get_next_stock_market_days,
parse_known_args_and_warn,
)
register_matplotlib_converters()
# -------------------------------------------------- ARIMA --------------------------------------------------
# pylint: disable=unused-argument
def arima(l_args, s_ticker, s_interval, df_stock):
parser = argparse.ArgumentParser(
@@ -71,11 +75,7 @@ def arima(l_args, s_ticker, s_interval, df_stock):
)
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
ns_parser = parse_known_args_and_warn(parser, l_args)
# Machine Learning model
if ns_parser.s_order:
@@ -115,6 +115,7 @@ def arima(l_args, s_ticker, s_interval, df_stock):
print("")
# Plotting
plt.figure()
plt.plot(df_stock.index, df_stock["5. adjusted close"], lw=2)
if ns_parser.s_order:
plt.title(
@@ -147,6 +148,7 @@ def arima(l_args, s_ticker, s_interval, df_stock):
plt.vlines(
df_stock.index[-1], ymin, ymax, linewidth=1, linestyle="--", color="k"
)
plt.ion()
plt.show()
# Print prediction data

View File

@@ -4,13 +4,16 @@ import matplotlib.pyplot as plt
import pandas as pd
from pandas.plotting import register_matplotlib_converters
from fbprophet import Prophet
from gamestonk_terminal.helper_funcs import check_positive, get_next_stock_market_days
from gamestonk_terminal.helper_funcs import (
check_positive,
get_next_stock_market_days,
parse_known_args_and_warn,
)
register_matplotlib_converters()
warnings.simplefilter("ignore")
# ----------------------------------------------------- FBPROPHET -----------------------------------------------------
# pylint: disable=unused-argument
def fbprophet(l_args, s_ticker, s_interval, df_stock):
parser = argparse.ArgumentParser(
@@ -33,11 +36,7 @@ def fbprophet(l_args, s_ticker, s_interval, df_stock):
)
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
ns_parser = parse_known_args_and_warn(parser, l_args)
df_stock = df_stock.sort_index(ascending=True)
df_stock.reset_index(level=0, inplace=True)
@@ -77,6 +76,7 @@ def fbprophet(l_args, s_ticker, s_interval, df_stock):
df_stock["ds"].values[0], get_next_stock_market_days(l_pred_days[-1], 1)[-1]
)
plt.title(f"Fb Prophet on {s_ticker} - {ns_parser.n_days} days prediction")
plt.ion()
plt.show()
print("")

View File

@@ -4,11 +4,15 @@ import pandas as pd
from pandas.plotting import register_matplotlib_converters
from sklearn import neighbors
from TimeSeriesCrossValidation import splitTrain
from gamestonk_terminal.helper_funcs import check_positive, get_next_stock_market_days
from gamestonk_terminal.helper_funcs import (
check_positive,
get_next_stock_market_days,
parse_known_args_and_warn,
)
register_matplotlib_converters()
# ----------------------------------------------------- kNN -----------------------------------------------------
# pylint: disable=unused-argument
def k_nearest_neighbors(l_args, s_ticker, s_interval, df_stock):
parser = argparse.ArgumentParser(
@@ -58,11 +62,7 @@ def k_nearest_neighbors(l_args, s_ticker, s_interval, df_stock):
)
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
ns_parser = parse_known_args_and_warn(parser, l_args)
# Split training data
stock_x, stock_y = splitTrain.split_train(
@@ -87,6 +87,7 @@ def k_nearest_neighbors(l_args, s_ticker, s_interval, df_stock):
df_pred = pd.Series(l_predictions, index=l_pred_days, name="Price")
# Plotting
plt.figure()
plt.plot(df_stock.index, df_stock["5. adjusted close"], lw=2)
plt.title(
f"{ns_parser.n_neighbors}-Nearest Neighbors on {s_ticker} - {ns_parser.n_days} days prediction"
@@ -114,6 +115,7 @@ def k_nearest_neighbors(l_args, s_ticker, s_interval, df_stock):
plt.vlines(
df_stock.index[-1], ymin, ymax, linewidth=1, linestyle="--", color="k"
)
plt.ion()
plt.show()
# Print prediction data

View File

@@ -18,7 +18,12 @@ from tensorflow.keras.layers import (
# TimeDistributed,
)
from gamestonk_terminal.helper_funcs import check_positive, get_next_stock_market_days
from gamestonk_terminal.helper_funcs import (
check_positive,
get_next_stock_market_days,
parse_known_args_and_warn,
)
from gamestonk_terminal import config_neural_network_models as cfg_nn_models
@@ -77,7 +82,6 @@ def build_neural_network_model(Recurrent_Neural_Network, n_inputs, n_days):
return model
# -------------------------------------------------- MLP --------------------------------------------------
# pylint: disable=unused-argument
def mlp(l_args, s_ticker, s_interval, df_stock):
parser = argparse.ArgumentParser(
@@ -159,11 +163,7 @@ def mlp(l_args, s_ticker, s_interval, df_stock):
)
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
ns_parser = parse_known_args_and_warn(parser, l_args)
# Pre-process data
if ns_parser.s_preprocessing == "standardization":
@@ -227,6 +227,7 @@ def mlp(l_args, s_ticker, s_interval, df_stock):
df_pred = pd.Series(y_pred_test_t[0].tolist(), index=l_pred_days, name="Price")
# Plotting
plt.figure()
plt.plot(df_stock.index, df_stock["5. adjusted close"], lw=3)
plt.title(f"MLP on {s_ticker} - {ns_parser.n_days} days prediction")
plt.xlim(
@@ -258,6 +259,7 @@ def mlp(l_args, s_ticker, s_interval, df_stock):
linestyle="--",
color="k",
)
plt.ion()
plt.show()
# Print prediction data
@@ -271,7 +273,6 @@ def mlp(l_args, s_ticker, s_interval, df_stock):
print("")
# -------------------------------------------------- RNN --------------------------------------------------
# pylint: disable=unused-argument
def rnn(l_args, s_ticker, s_interval, df_stock):
parser = argparse.ArgumentParser(
@@ -353,11 +354,7 @@ def rnn(l_args, s_ticker, s_interval, df_stock):
)
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
ns_parser = parse_known_args_and_warn(parser, l_args)
# Pre-process data
if ns_parser.s_preprocessing == "standardization":
@@ -421,6 +418,7 @@ def rnn(l_args, s_ticker, s_interval, df_stock):
df_pred = pd.Series(y_pred_test_t[0].tolist(), index=l_pred_days, name="Price")
# Plotting
plt.figure()
plt.plot(df_stock.index, df_stock["5. adjusted close"], lw=3)
plt.title(f"RNN on {s_ticker} - {ns_parser.n_days} days prediction")
plt.xlim(
@@ -452,6 +450,7 @@ def rnn(l_args, s_ticker, s_interval, df_stock):
linestyle="--",
color="k",
)
plt.ion()
plt.show()
# Print prediction data
@@ -465,7 +464,6 @@ def rnn(l_args, s_ticker, s_interval, df_stock):
print("")
# -------------------------------------------------- LSTM --------------------------------------------------
# pylint: disable=unused-argument
def lstm(l_args, s_ticker, s_interval, df_stock):
parser = argparse.ArgumentParser(
@@ -547,11 +545,7 @@ def lstm(l_args, s_ticker, s_interval, df_stock):
)
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
ns_parser = parse_known_args_and_warn(parser, l_args)
# Pre-process data
if ns_parser.s_preprocessing == "standardization":
@@ -615,6 +609,7 @@ def lstm(l_args, s_ticker, s_interval, df_stock):
df_pred = pd.Series(y_pred_test_t[0].tolist(), index=l_pred_days, name="Price")
# Plotting
plt.figure()
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(
@@ -646,6 +641,7 @@ def lstm(l_args, s_ticker, s_interval, df_stock):
linestyle="--",
color="k",
)
plt.ion()
plt.show()
# Print prediction data

View File

@@ -6,8 +6,9 @@ from gamestonk_terminal.prediction_techniques import regression
from gamestonk_terminal.prediction_techniques import arima
from gamestonk_terminal.prediction_techniques import fbprophet
from gamestonk_terminal.prediction_techniques import neural_networks
import matplotlib.pyplot as plt
# -----------------------------------------------------------------------------------------------------------------------
def print_prediction(s_ticker, s_start, s_interval):
""" Print help """
@@ -69,6 +70,9 @@ def pred_menu(df_stock, s_ticker, s_start, s_interval):
# Get input command from user
as_input = input("> ")
# Images are non blocking - allows to close them if we type other command
plt.close()
# Parse prediction techniques command of the list of possible commands
try:
(ns_known_args, l_args) = pred_parser.parse_known_args(as_input.split())

View File

@@ -7,7 +7,11 @@ from sklearn import linear_model
from sklearn import pipeline
from sklearn import preprocessing
from gamestonk_terminal.helper_funcs import check_positive, get_next_stock_market_days
from gamestonk_terminal.helper_funcs import (
check_positive,
get_next_stock_market_days,
parse_known_args_and_warn,
)
register_matplotlib_converters()
@@ -16,7 +20,6 @@ LINEAR = 1
QUADRATIC = 2
CUBIC = 3
# -------------------------------------------------- REGRESSION --------------------------------------------------
# pylint: disable=unused-argument
def regression(l_args, s_ticker, s_interval, df_stock, polynomial):
parser = argparse.ArgumentParser(
@@ -69,11 +72,7 @@ def regression(l_args, s_ticker, s_interval, df_stock, polynomial):
)
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
ns_parser = parse_known_args_and_warn(parser, l_args)
# Split training data
stock_x, stock_y = splitTrain.split_train(
@@ -106,6 +105,7 @@ def regression(l_args, s_ticker, s_interval, df_stock, polynomial):
df_pred = pd.Series(l_predictions, index=l_pred_days, name="Price")
# Plotting
plt.figure()
plt.plot(df_stock.index, df_stock["5. adjusted close"], lw=2)
plt.title(
f"Regression (polynomial {polynomial}) on {s_ticker} - {ns_parser.n_days} days prediction"
@@ -133,6 +133,7 @@ def regression(l_args, s_ticker, s_interval, df_stock, polynomial):
plt.vlines(
df_stock.index[-1], ymin, ymax, linewidth=1, linestyle="--", color="k"
)
plt.ion()
plt.show()
# Print prediction data

View File

@@ -4,11 +4,14 @@ import matplotlib.pyplot as plt
import pandas as pd
from pandas.plotting import register_matplotlib_converters
from gamestonk_terminal.helper_funcs import check_positive, get_next_stock_market_days
from gamestonk_terminal.helper_funcs import (
check_positive,
get_next_stock_market_days,
parse_known_args_and_warn,
)
register_matplotlib_converters()
# ----------------------------------------------------- SMA -----------------------------------------------------
# pylint: disable=unused-argument
def simple_moving_average(l_args, s_ticker, s_interval, df_stock):
parser = argparse.ArgumentParser(
@@ -44,11 +47,7 @@ def simple_moving_average(l_args, s_ticker, s_interval, df_stock):
)
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
ns_parser = parse_known_args_and_warn(parser, l_args)
# Prediction data
l_predictions = list()
@@ -97,6 +96,7 @@ def simple_moving_average(l_args, s_ticker, s_interval, df_stock):
plt.vlines(
df_stock.index[-1], ymin, ymax, linewidth=1, linestyle="--", color="k"
)
plt.ion()
plt.show()
# Print prediction data