def test_btd_7(self): # normalize_rnn_input_options, predict 1 x, y, other_data = build_training_dataset(normalize_rnn_input_options, 1, stock_data=stock_prices) self.assertEqual(x.shape, (3637, 10, 1)) self.assertEqual(y.shape, (3637, 1)) self.assertTrue("normalize_data" in other_data) self.assertTrue("min" in other_data["normalize_data"]) self.assertTrue("max" in other_data["normalize_data"]) data_min = np.array([np.nanmin(prices)]) data_max = np.array([np.nanmax(prices)]) self.assertEqual(other_data["normalize_data"]["min"], data_min.tolist()) self.assertEqual(other_data["normalize_data"]["max"], data_max.tolist()) self.assertEqual(x[0, :, 0].tolist(), ((prices[:10] - data_min) / (data_max - data_min)).tolist()) self.assertEqual(x[1, :, 0].tolist(), ((prices[1:11] - data_min) / (data_max - data_min)).tolist()) self.assertEqual(x[-1, :, 0].tolist(), ((prices[-11:-1] - data_min) / (data_max - data_min)).tolist()) self.assertEqual(y[0][0], prices[10]) self.assertEqual(y[1][0], prices[11]) self.assertEqual(y[-1][0], prices[-1])
def test_btd_4(self): # rnn_input_options, predict 10 x, y, _ = build_training_dataset(rnn_input_options, 10, stock_data=stock_prices) self.assertEqual(x.shape, (3619, 10, 2)) self.assertEqual(y.shape, (3619, 10)) self.assertEqual(x[0, :, 0].tolist(), prices[9:19].tolist()) self.assertEqual(x[0, 0, 1], prices[:10].mean()) self.assertEqual(x[0, 1, 1], prices[1:11].mean()) self.assertEqual(x[0, 9, 1], prices[9:19].mean()) self.assertEqual(x[1, :, 0].tolist(), prices[10:20].tolist()) self.assertEqual(x[1, 0, 1], prices[1:11].mean()) self.assertEqual(x[1, 1, 1], prices[2:12].mean()) self.assertEqual(x[1, 9, 1], prices[10:20].mean()) self.assertEqual(x[-1, :, 0].tolist(), prices[-20:-10].tolist()) self.assertEqual(x[-1, 0, 1], prices[-29:-19].mean()) self.assertEqual(x[-1, 1, 1], prices[-28:-18].mean()) self.assertEqual(x[-1, 9, 1], prices[-20:-10].mean()) self.assertEqual(y[0].tolist(), prices[19:29].tolist()) self.assertEqual(y[-1].tolist(), prices[-10:].tolist())
def test_btd_1(self): # input_options, predict 1 x, y, _ = build_training_dataset(input_options, 1, stock_data=stock_prices) self.assertEqual(x.shape, (3637, 11)) self.assertEqual(y.shape, (3637, 1)) x_start = [ 50.3228, 54.3227, 54.8694, 52.5974, 53.1641, 54.1221, 53.2393, 51.1629, 51.3435, 50.2802 ] x_end = [ 1097.99, 1125.89, 1118.62, 1141.42, 1151.87, 1122.89, 1105.91, 1102.38, 1102.12, 1127.58 ] x_start.append(sum(x_start) / len(x_start)) x_end.append(sum(x_end) / len(x_end)) self.assertEqual(x[0].tolist(), x_start) self.assertEqual(x[-1].tolist(), x_end) self.assertEqual(y[0][0], 50.9122) self.assertEqual(y[-1][0], 1128.63)
def test_btd_5(self): # index_input_options, predict 10 x, y, _ = build_training_dataset(index_input_options, 10, stock_data=stock_prices) self.assertEqual(x.tolist(), [[i] for i in range(1, 11)]) self.assertEqual(y.tolist(), prices[-10:].tolist())
def test_btd_2(self): # input_options, predict 10 x, y, _ = build_training_dataset(input_options, 10, stock_data=stock_prices) self.assertEqual(x.shape, (3628, 11)) self.assertEqual(y.shape, (3628, 10)) x_start = prices[:10].tolist() x_start.append(prices[:10].mean()) x_end = prices[-20:-10].tolist() x_end.append(prices[-20:-10].mean()) self.assertEqual(x[0].tolist(), x_start) self.assertEqual(x[-1].tolist(), x_end) self.assertEqual(y[0].tolist(), prices[10:20].tolist()) self.assertEqual(y[-1].tolist(), prices[-10:].tolist())
def train_models(train_models_data): """Trains models. Args: train_models_data: Train models data. Format: { models: [ { "model": "model type, matches MODEL in a model class", "stockCode": "the predicting stock", "modelOptions": "model options dict", "inputOptions": "input options dict" } ] } Refer to train_models_sample.json. """ if not path.isdir(SAVED_MODELS_DIR): makedirs(SAVED_MODELS_DIR) for train_model_data_idx, train_model_data in enumerate(train_models_data): print("Model {}".format(train_model_data_idx + 1)) # initialize the model if train_model_data["model"] == LinearRegression.MODEL: model = LinearRegression(train_model_data["modelOptions"], train_model_data["inputOptions"], stock_code=train_model_data["stockCode"]) elif train_model_data["model"] == SupportVectorRegression.MODEL: model = SupportVectorRegression( train_model_data["modelOptions"], train_model_data["inputOptions"], stock_code=train_model_data["stockCode"]) elif train_model_data["model"] == LinearIndexRegression.MODEL: model = LinearIndexRegression(train_model_data["modelOptions"], train_model_data["inputOptions"], train_model_data["stock_code"]) elif train_model_data["model"] == SupportVectorIndexRegression.MODEL: model = SupportVectorIndexRegression( train_model_data["modelOptions"], train_model_data["inputOptions"], train_model_data["stock_code"]) elif train_model_data["model"] == DenseNeuralNetwork.MODEL: model = DenseNeuralNetwork( train_model_data["modelOptions"], train_model_data["inputOptions"], stock_code=train_model_data["stockCode"]) # prepare the data x, y, other_data = build_training_dataset( train_model_data["inputOptions"], model.model_options["predict_n"]) if train_model_data["model"] in [ LinearRegression.MODEL, SupportVectorRegression.MODEL, DenseNeuralNetwork.MODEL ]: # get the training set x = x[:-100] y = y[:-100] if "normalize" in train_model_data["inputOptions"]: model.input_options["normalize_data"] = other_data[ "normalize_data"] # train the model model.train(x, y) # save the model model.save(SAVED_MODELS_DIR_MAP[train_model_data["model"]])