From 51f4e9a695c433d5114c50003ef09cc321c8de89 Mon Sep 17 00:00:00 2001 From: Jamie Date: Thu, 6 May 2021 18:50:24 -0400 Subject: [PATCH] merge main + small changes --- .../prediction_techniques/knn_view.py | 13 +++++++-- .../neural_networks_helper.py | 12 --------- .../prediction_techniques/pred_helper.py | 27 +++++++++++++++---- 3 files changed, 33 insertions(+), 19 deletions(-) delete mode 100644 gamestonk_terminal/prediction_techniques/neural_networks_helper.py diff --git a/gamestonk_terminal/prediction_techniques/knn_view.py b/gamestonk_terminal/prediction_techniques/knn_view.py index 950c57b46d2..9b56b6eca4f 100644 --- a/gamestonk_terminal/prediction_techniques/knn_view.py +++ b/gamestonk_terminal/prediction_techniques/knn_view.py @@ -63,7 +63,7 @@ def k_nearest_neighbors(other_args: List[str], s_ticker: str, df_stock: pd.DataF dest="n_inputs", type=check_positive, default=40, - help="number of days to use for prediction.", + help="number of days to use as input for prediction.", ) parser.add_argument( "-d", @@ -99,7 +99,16 @@ def k_nearest_neighbors(other_args: List[str], s_ticker: str, df_stock: pd.DataF type=valid_date, dest="s_end_date", default=None, - help="The end date (format YYYY-MM-DD) to select - Backtesting", + help="The end date (format YYYY-MM-DD) to select for testing", + ) + + parser.add_argument( + "-t", + "--test_size", + default = .2, + dest="test_size", + type = float, + help = "Percentage of data to validate in sample" ) try: diff --git a/gamestonk_terminal/prediction_techniques/neural_networks_helper.py b/gamestonk_terminal/prediction_techniques/neural_networks_helper.py deleted file mode 100644 index 125105080ea..00000000000 --- a/gamestonk_terminal/prediction_techniques/neural_networks_helper.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Helper functions""" -__docformat__ = "numpy" -from typing import List, Tuple -from datetime import datetime -import pandas as pd -import numpy as np -from matplotlib import pyplot as plt -from gamestonk_terminal.helper_funcs import ( - get_next_stock_market_days, - patch_pandas_text_adjustment, - plot_autoscale, -) diff --git a/gamestonk_terminal/prediction_techniques/pred_helper.py b/gamestonk_terminal/prediction_techniques/pred_helper.py index 71168ecb188..7c14d9a449a 100644 --- a/gamestonk_terminal/prediction_techniques/pred_helper.py +++ b/gamestonk_terminal/prediction_techniques/pred_helper.py @@ -26,10 +26,12 @@ from gamestonk_terminal.config_plot import PLOT_DPI register_matplotlib_converters() -def prepare_test_train_valid( +def prepare_train_valid_test( df_stock, parsed_end_date, n_input_days, n_predict_days, test_size ): + df_test = df_stock[df_stock.index >= parsed_end_date] + df_stock = df_stock[df_stock.index < parsed_end_date] dates = df_stock.index prices = df_stock["5. adjusted close"].values @@ -38,7 +40,12 @@ def prepare_test_train_valid( next_n_day_prices = [] next_n_day_dates = [] + test_days = [] + test_inputs = [] + test_next_n_days = [] + for idx in range(len(prices) - n_input_days - n_predict_days): + input_prices.append(prices[idx : idx + n_input_days]) input_dates.append(dates[idx : idx + n_input_days]) next_n_day_prices.append( @@ -55,13 +62,13 @@ def prepare_test_train_valid( ( X_train, - X_test, + X_valid, y_train, - y_test, + y_valid, X_dates_train, - X_dates_test, + X_dates_valid, y_dates_train, - y_dates_test, + y_dates_valid, ) = train_test_split( input_prices, next_n_day_prices, @@ -69,6 +76,16 @@ def prepare_test_train_valid( next_n_day_dates, test_size=test_size, ) + return ( + X_train, + X_valid, + y_train, + y_valid, + X_dates_train, + X_dates_valid, + y_dates_train, + y_dates_valid, + ) def get_backtesting_data(