def rnn_predict(stock, start, end):
    # get stock data
    try:
        df = get_stock_data(stock, start, end, json=False)
    except:
        # error info
        e = sys.exc_info()
        print(e)
        print("rnn predict fail")
        return e

    # normalize
    scaler = Normalize(df, max=True)
    normalized = scaler.normalize_data(df)

    # get training and testing inputs and outputs
    train_inputs, train_targets, test_inputs, test_targets = train_test_split(
        normalized)

    train_inputs = np.array(train_inputs)
    train_targets = np.array(train_targets)
    test_inputs = np.array(test_inputs)
    test_targets = np.array(test_targets)

    # returns 3d array in format [inputs, timesteps, features]
    train_inputs = to_3d(train_inputs)
    test_inputs = to_3d(test_inputs)

    NN = RNN_V2()
    train_outputs = NN.train(train_inputs, train_targets, epochs=100)
    test_outputs = NN.test(test_inputs)

    # de-normalize
    train_outputs = scaler.denormalize_data(train_outputs)
    train_targets = scaler.denormalize_data(train_targets)
    test_outputs = scaler.denormalize_data(test_outputs)
    test_targets = scaler.denormalize_data(test_targets).T

    # accuracy
    accuracy = 100 - mape(test_targets, test_outputs)

    return df[4:], pd.DataFrame(train_outputs), pd.DataFrame(
        test_outputs), str(round(accuracy, 2))
    def test_normalize(self):
        test = np.arange(1000)

        # normalize
        scaler = Normalize(test)
        normalized = scaler.normalize_data(test)

        min_val = min(normalized)
        max_val = max(normalized)

        # ensure values scaled to range (0, 1)
        self.assertGreaterEqual(min_val, 0.0)
        self.assertLessEqual(max_val, 1.0)

        # denormalize
        denormalized = scaler.denormalize_data(normalized)

        # ensure denormalized values are the same as the original
        for x, y in zip(test, denormalized):
            try:
                self.assertEqual(x, y)
            except AssertionError:
                self.assertAlmostEqual(x, y, 12)
def lstm_predict(stock, start, end):
    # shift start date -4 days for correct test/train i/o

    # get stock data
    try:
        df = get_stock_data(stock, start, end, json=False)
    except:
        # error info
        e = sys.exc_info()
        print(e)
        print("lstm predict fail")
        return e

    stock = df

    scaler = Normalize(df)
    df = scaler.normalize_data(df)

    train_max_index = round((len(df) - 1) * 0.75)

    training_input_1 = [[df[i - 6], df[i - 5]]
                        for i in range(6, train_max_index)]
    training_input_2 = [[df[i - 4], df[i - 3]]
                        for i in range(6, train_max_index)]
    training_input_3 = [[df[i - 2], df[i - 1]]
                        for i in range(6, train_max_index)]
    target = [[i] for i in df[6:train_max_index]]

    training_input_1 = np.array(training_input_1, dtype=float)
    training_input_2 = np.array(training_input_2, dtype=float)
    training_input_3 = np.array(training_input_3, dtype=float)
    target = np.array(target, dtype=float)

    assert len(training_input_1) == len(training_input_2) == len(
        training_input_3) == len(target)

    # create neural network
    NN = LSTM()

    # number of training cycles
    training_cycles = 100

    # train the neural network
    for cycle in range(training_cycles):
        for n in training_input_1:
            output = NN.train(training_input_1, training_input_2,
                              training_input_3, target)

    # de-Normalize
    output = scaler.denormalize_data(output)
    target = scaler.denormalize_data(target)

    # transpose
    output = output.T

    # change data type so it can be plotted
    prices = pd.DataFrame(output)

    # [price 2 days ago, price yesterday] for each day in range
    testing_input_1 = [[df[i - 6], df[i - 5]]
                       for i in range(train_max_index, len(df))]
    testing_input_2 = [[df[i - 4], df[i - 3]]
                       for i in range(train_max_index, len(df))]
    testing_input_3 = [[df[i - 2], df[i - 1]]
                       for i in range(train_max_index, len(df))]
    test_target = [[i] for i in df[train_max_index:len(df)]]

    assert len(testing_input_1) == len(testing_input_2) == len(
        testing_input_3) == len(test_target)

    testing_input_1 = np.array(testing_input_1, dtype=float)
    testing_input_2 = np.array(testing_input_2, dtype=float)
    testing_input_3 = np.array(testing_input_3, dtype=float)
    test_target = np.array(test_target, dtype=float)

    # test the network with unseen data
    test = NN.test(testing_input_1, testing_input_2, testing_input_3)

    # de-Normalize data
    test = scaler.denormalize_data(test)
    test_target = scaler.denormalize_data(test_target)

    # transplose test results
    test = test.T

    # accuracy
    accuracy = 100 - mape(test_target, test)

    return stock[6:], prices, pd.DataFrame(test), str(round(accuracy, 2))