# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# +
import numpy as np

import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler

# Custom classes
from loader import StockPriceLoader
from preprocessor import Preprocessor
from training_engine import TrainingEngine
from reporting import Reporting

# +
#ecb46fc18506ca6879fd4f7b9ce53f1b4261df9a3b778ad4

stock_history_reader = StockPriceLoader()
preprocessor = Preprocessor()
training_engine = TrainingEngine()
reporting = Reporting()

# +
symbols = ['jpm']
look_back = 2
epochs = 2
no_of_features = 1

# fix random seed for reproducibility
np.random.seed(43)

# +
historical_data = stock_history_reader.load_single_stock('jpm')
historical_data = historical_data[:1000]

print historical_data.head()

close = historical_data["Close"]

plt.figure(figsize=(15, 10))
plt.plot(close)
plt.show()

# +
dataset = close.values
dataset.astype('float32')

print "Before normalized"
print dataset[:3]

# normalize the dataset
scaler = MinMaxScaler(feature_range=(0,1))
dataset = dataset.reshape(-1, 1)
dataset = scaler.fit_transform(dataset)

print "After reshaped and normalized"
print dataset[:3]

# +
# split into train and test sets
train, test = preprocessor.split_data(dataset)

print "Splitting data into {} training data and {} test data".format(len(train), len(test))

# +
# convert to dataset matrix
print "Original Training Data"
print train[:4]
trainX, trainY = preprocessor.convert_dataset(train, look_back)
testX, testY = preprocessor.convert_dataset(test, look_back)
print "TrainX[0]"
print trainX[:1]
print "TrainY[0]"
print trainY[:1]
print "TrainX[1]"
print trainX[1:2]
print "TrainY[1]"
print trainY[1:2]

# reshape input to be [samples, time steps, features]
trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1], no_of_features))
testX = np.reshape(testX, (testX.shape[0], testX.shape[1], no_of_features))

print trainX.shape[0]
# -

# create and fit the LSTM network
model = training_engine.create_model(trainX, trainY, epochs, no_of_features, look_back)

# +
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)

# invert predictions
inversedTrainPredict = scaler.inverse_transform(trainPredict.reshape(-1, 1))
inversedTrainY = scaler.inverse_transform(trainY.reshape(-1, 1))
inversedTestPredict = scaler.inverse_transform(testPredict.reshape(-1, 1))
inversedTestY = scaler.inverse_transform(testY.reshape(-1, 1))

print inversedTrainY[:5]
print inversedTrainPredict[:5]

# +
#reporting.print_results(close.values, inversedTrainPredict[:,0], inversedTrainY[:,0], inversedTestPredict[:,0], inversedTestY[:,0], look_back)

start=0
end=-1
reporting.print_results(close.values, inversedTrainPredict[:,0], inversedTrainY[:,0], inversedTestPredict[:,0], inversedTestY[:,0], \
                        look_back, start, end)

StarcoderdataJupyterScriptsDedupFiltered