def call_conv1d(self, other_args: List[str]): """Process conv1d command""" try: ns_parser = pred_helper.parse_args( prog="conv1d", description="""1D CNN.""", other_args=other_args, ) if ns_parser: neural_networks_view.display_conv1d( dataset=self.coin, data=self.data[self.target], n_input_days=ns_parser.n_inputs, n_predict_days=ns_parser.n_days, learning_rate=ns_parser.lr, epochs=ns_parser.n_epochs, batch_size=ns_parser.n_batch_size, test_size=ns_parser.valid_split, n_loops=ns_parser.n_loops, no_shuffle=ns_parser.no_shuffle, time_res=self.resolution, ) except Exception as e: console.print(e, "\n") finally: pred_helper.restore_env()
def call_lstm(self, other_args: List[str]): """Process lstm command""" try: ns_parser = pred_helper.parse_args( prog="lstm", description="""Long-Short Term Memory. """, other_args=other_args, ) if ns_parser: neural_networks_view.display_lstm( dataset=self.ticker, data=self.df[self.target], n_input_days=ns_parser.n_inputs, n_predict_days=ns_parser.n_days, learning_rate=ns_parser.lr, epochs=ns_parser.n_epochs, batch_size=ns_parser.n_batch_size, test_size=ns_parser.valid_split, n_loops=ns_parser.n_loops, no_shuffle=ns_parser.no_shuffle, ) except Exception as e: logger.exception(str(e)) console.print(e, "\n") finally: pred_helper.restore_env()
def call_conv1d(self, other_args: List[str]): """Process conv1d command""" try: ns_parser = pred_helper.parse_args( prog="conv1d", description="""1D CNN.""", other_args=other_args, ) if ns_parser: if ns_parser.n_inputs > len(self.data): console.print( f"[red]Data only contains {len(self.data)} samples and the model is trying " f"to use {ns_parser.n_inputs} inputs. Either use less inputs or load with" f" an earlier start date[/red]\n") return neural_networks_view.display_conv1d( dataset=self.current_id, data=self.data, n_input_days=ns_parser.n_inputs, n_predict_days=ns_parser.n_days, learning_rate=ns_parser.lr, epochs=ns_parser.n_epochs, batch_size=ns_parser.n_batch_size, test_size=ns_parser.valid_split, n_loops=ns_parser.n_loops, no_shuffle=ns_parser.no_shuffle, time_res=self.resolution, ) except Exception as e: console.print(e, "\n") finally: pred_helper.restore_env()
def call_rnn(self, other_args: List[str]): """Process rnn command""" try: ns_parser = pred_helper.parse_args( prog="rnn", description="""Recurrent Neural Network. """, other_args=other_args, ) if ns_parser: neural_networks_view.display_rnn( dataset=self.ticker, data=self.stock[self.target], n_input_days=ns_parser.n_inputs, n_predict_days=ns_parser.n_days, learning_rate=ns_parser.lr, epochs=ns_parser.n_epochs, batch_size=ns_parser.n_batch_size, test_size=ns_parser.valid_split, n_loops=ns_parser.n_loops, no_shuffle=ns_parser.no_shuffle, ) except Exception as e: console.print(e, "\n") finally: pred_helper.restore_env()
def call_mlp(self, other_args: List[str]): """Process mlp command""" try: ns_parser = pred_helper.parse_args( prog="mlp", description="""Multi-Layered-Perceptron. """, other_args=other_args, ) if not ns_parser: return neural_networks_view.display_mlp( dataset=self.ticker, data=self.stock[self.target], n_input_days=ns_parser.n_inputs, n_predict_days=ns_parser.n_days, learning_rate=ns_parser.lr, epochs=ns_parser.n_epochs, batch_size=ns_parser.n_batch_size, test_size=ns_parser.valid_split, n_loops=ns_parser.n_loops, no_shuffle=ns_parser.no_shuffle, ) except Exception as e: print(e, "\n") finally: pred_helper.restore_env()
def conv1d(other_args: List[str], s_ticker: str, df_stock: pd.DataFrame): """ Train a 1D Convolutional Neural Net (1D CNN) Parameters ---------- other_args:List[str] Argparse arguments s_ticker: str Stock ticker df_stock: pd.DataFrame Dataframe of stock prices """ try: ns_parser = parse_args( prog="conv1d", description="""1D CNN.""", other_args=other_args, ) if not ns_parser: return ( X_train, X_valid, y_train, y_valid, _, _, _, y_dates_valid, forecast_data_input, dates_forecast_input, scaler, is_error, ) = prepare_scale_train_valid_test(df_stock["Adj Close"], ns_parser) if is_error: return print( f"Training on {X_train.shape[0]} sequences of length {X_train.shape[1]}. Using {X_valid.shape[0]} sequences " f" of length {X_valid.shape[1]} for validation. Model will run {ns_parser.n_loops} loops" ) future_dates = get_next_stock_market_days(dates_forecast_input[-1], n_next_days=ns_parser.n_days) preds = np.zeros( (ns_parser.n_loops, X_valid.shape[0], ns_parser.n_days)) forecast_data = np.zeros((ns_parser.n_loops, ns_parser.n_days)) for i in range(ns_parser.n_loops): # Build Neural Network model model = build_neural_network_model( cfg_nn_models.Convolutional, ns_parser.n_inputs, ns_parser.n_days, ) model.compile( optimizer=optimizers[cfg_nn_models.Optimizer]( learning_rate=ns_parser.lr), loss=cfg_nn_models.Loss, ) model.fit( X_train.reshape(X_train.shape[0], X_train.shape[1], 1), y_train, epochs=ns_parser.n_epochs, verbose=True, batch_size=ns_parser.n_batch_size, validation_data=( X_valid.reshape(X_valid.shape[0], X_valid.shape[1], 1), y_valid, ), callbacks=[es], ) preds[i] = model.predict( X_valid.reshape(X_valid.shape[0], X_valid.shape[1], 1)).reshape(X_valid.shape[0], ns_parser.n_days) forecast_data[i] = forecast(forecast_data_input, future_dates, model, scaler).values.flat forecast_data_df = pd.DataFrame(forecast_data.T, index=future_dates) 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["Adj Close"].values[-1]) else: print_pretty_prediction(forecast_data_df[0], df_stock["Adj Close"].values[-1]) plot_data_predictions( df_stock, np.median(preds, axis=0), y_valid, y_dates_valid, scaler, f"Conv1D Model on {s_ticker}", forecast_data_df, ns_parser.n_loops, ) print("") except Exception as e: print(e) traceback.print_exc() print("") finally: restore_env()