mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-12-01 13:44:16 +00:00
add back print pretty predictions and add n_loops logic for plotting
This commit is contained in:
@@ -24,16 +24,18 @@ from gamestonk_terminal.prediction_techniques.pred_helper import (
|
||||
plot_data_predictions,
|
||||
parse_args,
|
||||
restore_env,
|
||||
print_pretty_prediction,
|
||||
)
|
||||
from gamestonk_terminal import config_neural_network_models as cfg_nn_models
|
||||
|
||||
es = EarlyStopping(monitor="val_loss", patience=10)
|
||||
|
||||
|
||||
def build_neural_network_model(
|
||||
Recurrent_Neural_Network: List[Any], n_inputs: int, n_days: int
|
||||
):
|
||||
"""
|
||||
Builds neural netfrom config_neural_network_models.py
|
||||
Builds neural net from config_neural_network_models.py
|
||||
Parameters
|
||||
----------
|
||||
Recurrent_Neural_Network: List[Any]
|
||||
@@ -171,7 +173,10 @@ def mlp(other_args: List[str], s_ticker: str, df_stock: pd.DataFrame):
|
||||
epochs=ns_parser.n_epochs,
|
||||
verbose=True,
|
||||
batch_size=ns_parser.n_batch_size,
|
||||
validation_data=(X_valid, y_valid),
|
||||
validation_data=(
|
||||
X_valid.reshape(X_valid.shape[0], X_valid.shape[1], 1),
|
||||
y_valid,
|
||||
),
|
||||
callbacks=[es],
|
||||
)
|
||||
|
||||
@@ -183,7 +188,15 @@ def mlp(other_args: List[str], s_ticker: str, df_stock: pd.DataFrame):
|
||||
).values.flat
|
||||
|
||||
forecast_data_df = pd.DataFrame(forecast_data.T, index=future_dates)
|
||||
print(forecast_data_df)
|
||||
if ns_parser.n_loops > 1:
|
||||
forecast_data_df["Median"] = forecast_data_df.median(axis=1)
|
||||
print_pretty_prediction(
|
||||
forecast_data_df["Median"], df_stock["5. adjusted close"].values[-1]
|
||||
)
|
||||
else:
|
||||
print_pretty_prediction(
|
||||
forecast_data_df[0], df_stock["5. adjusted close"].values[-1]
|
||||
)
|
||||
plot_data_predictions(
|
||||
df_stock,
|
||||
np.median(preds, axis=0),
|
||||
@@ -267,7 +280,10 @@ def rnn(other_args: List[str], s_ticker: str, df_stock: pd.DataFrame):
|
||||
epochs=ns_parser.n_epochs,
|
||||
verbose=True,
|
||||
batch_size=ns_parser.n_batch_size,
|
||||
validation_data=(X_valid, y_valid),
|
||||
validation_data=(
|
||||
X_valid.reshape(X_valid.shape[0], X_valid.shape[1], 1),
|
||||
y_valid,
|
||||
),
|
||||
callbacks=[es],
|
||||
)
|
||||
|
||||
@@ -279,7 +295,15 @@ def rnn(other_args: List[str], s_ticker: str, df_stock: pd.DataFrame):
|
||||
).values.flat
|
||||
|
||||
forecast_data_df = pd.DataFrame(forecast_data.T, index=future_dates)
|
||||
print(forecast_data_df)
|
||||
if ns_parser.n_loops > 1:
|
||||
forecast_data_df["Median"] = forecast_data_df.median(axis=1)
|
||||
print_pretty_prediction(
|
||||
forecast_data_df["Median"], df_stock["5. adjusted close"].values[-1]
|
||||
)
|
||||
else:
|
||||
print_pretty_prediction(
|
||||
forecast_data_df[0], df_stock["5. adjusted close"].values[-1]
|
||||
)
|
||||
plot_data_predictions(
|
||||
df_stock,
|
||||
np.median(preds, axis=0),
|
||||
@@ -361,7 +385,10 @@ def lstm(other_args: List[str], s_ticker: str, df_stock: pd.DataFrame):
|
||||
epochs=ns_parser.n_epochs,
|
||||
verbose=True,
|
||||
batch_size=ns_parser.n_batch_size,
|
||||
validation_data=(X_valid, y_valid),
|
||||
validation_data=(
|
||||
X_valid.reshape(X_valid.shape[0], X_valid.shape[1], 1),
|
||||
y_valid,
|
||||
),
|
||||
callbacks=[es],
|
||||
)
|
||||
|
||||
@@ -374,7 +401,15 @@ def lstm(other_args: List[str], s_ticker: str, df_stock: pd.DataFrame):
|
||||
).values.flat
|
||||
|
||||
forecast_data_df = pd.DataFrame(forecast_data.T, index=future_dates)
|
||||
print(forecast_data_df)
|
||||
if ns_parser.n_loops > 1:
|
||||
forecast_data_df["Median"] = forecast_data_df.median(axis=1)
|
||||
print_pretty_prediction(
|
||||
forecast_data_df["Median"], df_stock["5. adjusted close"].values[-1]
|
||||
)
|
||||
else:
|
||||
print_pretty_prediction(
|
||||
forecast_data_df[0], df_stock["5. adjusted close"].values[-1]
|
||||
)
|
||||
plot_data_predictions(
|
||||
df_stock,
|
||||
np.median(preds, axis=0),
|
||||
@@ -457,8 +492,11 @@ def conv1d(other_args: List[str], s_ticker: str, df_stock: pd.DataFrame):
|
||||
epochs=ns_parser.n_epochs,
|
||||
verbose=True,
|
||||
batch_size=ns_parser.n_batch_size,
|
||||
validation_data=(X_valid, y_valid),
|
||||
callbacks = [es]
|
||||
validation_data=(
|
||||
X_valid.reshape(X_valid.shape[0], X_valid.shape[1], 1),
|
||||
y_valid,
|
||||
),
|
||||
callbacks=[es],
|
||||
)
|
||||
|
||||
preds[i] = model.predict(
|
||||
@@ -469,7 +507,15 @@ def conv1d(other_args: List[str], s_ticker: str, df_stock: pd.DataFrame):
|
||||
).values.flat
|
||||
|
||||
forecast_data_df = pd.DataFrame(forecast_data.T, index=future_dates)
|
||||
print(forecast_data_df)
|
||||
if ns_parser.n_loops > 1:
|
||||
forecast_data_df["Median"] = forecast_data_df.median(axis=1)
|
||||
print_pretty_prediction(
|
||||
forecast_data_df["Median"], df_stock["5. adjusted close"].values[-1]
|
||||
)
|
||||
else:
|
||||
print_pretty_prediction(
|
||||
forecast_data_df[0], df_stock["5. adjusted close"].values[-1]
|
||||
)
|
||||
plot_data_predictions(
|
||||
df_stock,
|
||||
np.median(preds, axis=0),
|
||||
|
||||
@@ -27,8 +27,11 @@ from gamestonk_terminal.helper_funcs import (
|
||||
from gamestonk_terminal import feature_flags as gtff
|
||||
from gamestonk_terminal.config_plot import PLOT_DPI
|
||||
|
||||
|
||||
def get_backtesting_data():
|
||||
pass
|
||||
|
||||
|
||||
register_matplotlib_converters()
|
||||
|
||||
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
||||
@@ -303,8 +306,6 @@ def prepare_scale_train_valid_test(
|
||||
|
||||
if ns_parser.s_end_date:
|
||||
df_stock = df_stock[df_stock.index <= ns_parser.s_end_date]
|
||||
print(df_stock.shape)
|
||||
print(n_input_days + n_predict_days)
|
||||
if n_input_days + n_predict_days > df_stock.shape[0]:
|
||||
print("Cannot train enough input days to predict with loaded dataframe\n")
|
||||
return (
|
||||
@@ -324,9 +325,10 @@ def prepare_scale_train_valid_test(
|
||||
|
||||
test_data = df_stock.iloc[-n_input_days:]
|
||||
train_data = df_stock.iloc[:-n_input_days]
|
||||
dates = train_data.index
|
||||
|
||||
dates = df_stock.index
|
||||
dates_test = test_data.index
|
||||
train_data = scaler.fit_transform(train_data.values.reshape(-1, 1))
|
||||
train_data = scaler.fit_transform(df_stock.values.reshape(-1, 1))
|
||||
test_data = scaler.transform(test_data.values.reshape(-1, 1))
|
||||
prices = train_data
|
||||
|
||||
@@ -382,7 +384,9 @@ def prepare_scale_train_valid_test(
|
||||
)
|
||||
|
||||
|
||||
def forecast(input_values:np.ndarray, future_dates:List, model, scaler)-> pd.DataFrame:
|
||||
def forecast(
|
||||
input_values: np.ndarray, future_dates: List, model, scaler
|
||||
) -> pd.DataFrame:
|
||||
"""
|
||||
Forecast the stock movement over future days and rescale
|
||||
Parameters
|
||||
@@ -457,7 +461,7 @@ def plot_data_predictions(
|
||||
scaler.inverse_transform(preds[-1].reshape(-1, 1)),
|
||||
"r",
|
||||
lw=2,
|
||||
label="Predicions",
|
||||
label="Predictions",
|
||||
)
|
||||
plt.fill_between(
|
||||
y_dates_valid[-1],
|
||||
@@ -484,14 +488,18 @@ def plot_data_predictions(
|
||||
)
|
||||
if n_loops == 1:
|
||||
plt.plot(
|
||||
forecast_data.index, forecast_data.values, "-ok", ms=5, label="Forecast"
|
||||
forecast_data.index,
|
||||
forecast_data.values,
|
||||
"-ok",
|
||||
ms=3,
|
||||
label="Forecast",
|
||||
)
|
||||
else:
|
||||
plt.plot(
|
||||
forecast_data.index,
|
||||
forecast_data.median(axis=1).values,
|
||||
"-ok",
|
||||
ms=5,
|
||||
ms=3,
|
||||
label="Forecast",
|
||||
)
|
||||
plt.fill_between(
|
||||
|
||||
Reference in New Issue
Block a user