def estimate_price(self, stock_name: str, year: str, month: str, day: str) -> float: """ Estimates price of the stock sent as parameter. Based on the date which is sent as parameter, it takes the month from the date and filters all the stock_name stocks which have a date in the specified month and does the average of the mid range price (average between high and low price) :param stock_name: stock name for which we try to predict :param year: :param month: :param day: :return: returns the prediction """ reader = Reader() self._dateset = reader.load_data("./dow_jones_index.csv") self._dateset = reader.clean_data(self._dateset) estimator = Estimator() estimator.fit(self._dateset) stock_prediction = estimator.predict(stock_name, year, month, day) return stock_prediction
def main(): args = parse_args() # Setup logging log_format = '%(asctime)s %(levelname)s %(message)s' logging.basicConfig(level=logging.INFO, format=log_format) logging.info('Initializing') if args.show_config: logging.info('Command line config: %s' % args) # Read the data train_data = np.load(os.path.join(args.input_dir, 'train_data.npy')) valid_data = np.load(os.path.join(args.input_dir, 'valid_data.npy')) if args.n_train is not None and args.n_train > 0: train_data = train_data[:args.n_train] if args.n_valid is not None and args.n_valid > 0: valid_data = valid_data[:args.n_valid] logging.info('Loaded training data: %s' % (train_data.shape, )) logging.info('Loaded validation data: %s' % (valid_data.shape, )) # Inputs are the hits from [0, N-1). # Targets are the hits from [1, N) without the radius feature. torchutils.set_cuda(args.cuda) train_input = torchutils.np_to_torch(train_data[:, :-1]) train_target = torchutils.np_to_torch(train_data[:, 1:, :2]) valid_input = torchutils.np_to_torch(valid_data[:, :-1]) valid_target = torchutils.np_to_torch(valid_data[:, 1:, :2]) model = None train_losses, valid_losses = [], [] # If we're continuing a pre-trained model, load it up now if args.continue_dir is not None and args.continue_dir != '': logging.info('Loading model to continue training from %s' % args.continue_dir) model_file = os.path.join(args.continue_dir, 'model') model = torch.load(model_file) losses_file = os.path.join(args.continue_dir, 'losses.npz') losses_data = np.load(losses_file) train_losses = list(losses_data['train_losses']) valid_losses = list(losses_data['valid_losses']) # Configure model type and loss function if args.model == 'regression': model_type = HitPredictor loss_func = nn.MSELoss() else: model_type = HitGausPredictor loss_func = gaus_llh_loss # Construct the model if not done already if model is None: model = model_type(hidden_dim=args.hidden_dim) # Construct the estimator estimator = Estimator(model, loss_func=loss_func, cuda=args.cuda, train_losses=train_losses, valid_losses=valid_losses) # Train the model estimator.fit(train_input, train_target, valid_input=valid_input, valid_target=valid_target, batch_size=args.batch_size, n_epochs=args.n_epochs) # Save outputs if args.output_dir is not None: logging.info('Writing outputs to %s' % args.output_dir) make_path = lambda s: os.path.join(args.output_dir, s) # Serialize the model torch.save(estimator.model, make_path('model')) # Save the losses for plotting np.savez(os.path.join(args.output_dir, 'losses'), train_losses=estimator.train_losses, valid_losses=estimator.valid_losses) # Drop to IPython interactive shell if args.interactive: import IPython IPython.embed() logging.info('All done!')
estimator = Estimator(model=model, device=config.DEVICE, exp_dir=config.EXPERIMENT_DIR) train_xs, train_ys = dataset.train_dataset.get_all_data(transforms=[LongTensor, Variable]) valid_xs, valid_ys = dataset.valid_dataset.get_all_data(transforms=[LongTensor, Variable]) epoch = 0 # with tqdm(total=100) as pbar: # pbar.update(10) with trange(epoch, config.EPOCH_SIZE) as t: for epoch in t: # Fit the model training_loss = estimator.fit(dataloader=train_dataloader) # Predict validation set valid_prediction, valid_loss = estimator.validate(xs=valid_xs, ys=valid_ys) valid_prediction = valid_prediction.to('cpu').data.numpy() valid_loss = valid_loss.item() # Log loss estimator.writer.add_scalar('training_loss', training_loss, epoch) estimator.writer.add_scalar('validation_loss', valid_loss, epoch) train_prediction, train_loss = estimator.validate(xs=train_xs, ys=train_ys) train_prediction = train_prediction.to('cpu').data.numpy() # train_prediction_df = pd.DataFrame(dict(y=train_ys.data.numpy().flatten(), yhat=train_prediction.flatten())) #