<jupyter_start><jupyter_text>Neural Network Model (LSTM) on Bitcoin Historical Data  *TEAM B*  Importing essential libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Reading the pickle file<jupyter_code>#df = pd.read_pickle("./df.pkl")    # reading 1 yr cleaned data pickle file
df = pd.read_pickle('./dfe.pkl')    # reading 5 yr cleaned data pickle file
df.head()
df.tail()<jupyter_output><empty_output><jupyter_text>Scaling the data<jupyter_code>from sklearn import preprocessing
from sklearn.preprocessing import MinMaxScaler
min_max_scaler = preprocessing.MinMaxScaler(feature_range=(0, 1))
ds = min_max_scaler.fit_transform(df['Price'].values.reshape(-1, 1))
train_size = int(len(ds) * 0.7)
test_size = len(ds)-train_size
train = ds[:train_size]
test = ds[train_size:]
print("Train size: ", len(train))
print("Test size: ", len(test))<jupyter_output>Train size:  1535
Test size:  658<jupyter_text>Train / Test distribution<jupyter_code>look_back = 15

dataX, dataY = [], []
for i in range(look_back, train.shape[0]):
    dataX.append(train[i-look_back:i])
    dataY.append(train[i,0])
x_train = np.array(dataX)
y_train = np.array(dataY)
dataX, dataY = [], []
for i in range(look_back, test.shape[0]):
    dataX.append(test[i-look_back:i])
    dataY.append(test[i,0])
x_test = np.array(dataX)
y_test = np.array(dataY)
print("X-Train shape:", x_train.shape)
print("Y-Train shape:", y_train.shape)
print("X-Test shape:", x_test.shape)
print("Y-Test shape:", y_test.shape)<jupyter_output>X-Train shape: (1520, 15, 1)
Y-Train shape: (1520,)
X-Test shape: (643, 15, 1)
Y-Test shape: (643,)<jupyter_text>Creating the model<jupyter_code>from keras import Sequential
from keras.layers import Dense, Dropout, LSTM
from tensorflow.keras.optimizers import Adam
model = Sequential()
model.add(LSTM(150, activation='tanh', input_shape=(x_train.shape[1], 1)))
model.add(Dense(1))

adam = Adam(
    learning_rate=0.00001,
    beta_1=0.001,
    beta_2=0.01,
    epsilon=1e-07,
    amsgrad=False,
)

model.compile(loss='mean_squared_error', optimizer='adam')
np.random.seed(7)
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm (LSTM)                  (None, 150)               91200     
_________________________________________________________________
dense (Dense)                (None, 1)                 151       
=================================================================
Total params: 91,351
Trainable params: 91,351
Non-trainable params: 0
_________________________________________________________________<jupyter_text>Training the Model<jupyter_code>history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=50, batch_size=16, verbose=1)
loss = history.history['loss']
val_loss = history.history['val_loss']<jupyter_output><empty_output><jupyter_text>Predicting<jupyter_code>trainPredict = model.predict(x_train)
testPredict = model.predict(x_test)
print("Train shape:", trainPredict.shape)
print("Test shape:", testPredict.shape)<jupyter_output>Train shape: (1520, 1)
Test shape: (643, 1)<jupyter_text>Inverting predictions<jupyter_code>trainPredict = min_max_scaler.inverse_transform(trainPredict)
trainY = min_max_scaler.inverse_transform([y_train])
trainY = trainY.reshape(y_train.shape)

testPredict = min_max_scaler.inverse_transform(testPredict)
testY = min_max_scaler.inverse_transform([y_test])
testY = testY.reshape(y_test.shape)<jupyter_output><empty_output><jupyter_text>Root mean squared error<jupyter_code>from sklearn.metrics import mean_squared_error
from math import sqrt
import math

trainScore = math.sqrt(mean_squared_error(trainY, trainPredict))
print('Train Score: %.2f RMSE' % (trainScore))

testScore = math.sqrt(mean_squared_error(testY, testPredict))
print('Test Score: %.2f RMSE' % (testScore))<jupyter_output>Train Score: 350.53 RMSE
Test Score: 1968.28 RMSE<jupyter_text>Shifting train and test predictions for plotting<jupyter_code>trainPredictPlot = np.empty_like(ds)
trainPredictPlot[:, :] = np.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict

testPredictPlot = np.empty_like(ds)
testPredictPlot[:, :] = np.nan
testPredictPlot[len(trainPredict)+(look_back*2):len(ds), :] = testPredict<jupyter_output><empty_output><jupyter_text>Plotting baseline and predictions<jupyter_code>plt.figure(figsize=(15,7))
plt.plot(min_max_scaler.inverse_transform(ds))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.legend(["Recorded Price Value", "Test Price Predict", "Train Price Predict" ], loc=2)
plt.ylabel('Price Value')
plt.xlabel('Day')
plt.show()<jupyter_output><empty_output><jupyter_text>R2 and Variance Score<jupyter_code>from sklearn.metrics import r2_score
print("R squared value on Train data:", r2_score(trainY,trainPredict))
print("R squared value on Test data:", r2_score(testY,testPredict))
from sklearn.metrics import explained_variance_score
print("Variance value on Train data:", explained_variance_score(trainY,trainPredict))
print("Variance value on Test data:", explained_variance_score(testY,testPredict))<jupyter_output>Variance value on Train data: 0.9938876176891959
Variance value on Test data: 0.9918812082524059<jupyter_text>Predicting for next n days and Visualization<jupyter_code>x_input = test[len(test)-look_back:].reshape(1,-1)
temp_input=list(x_input)
temp_input=temp_input[0].tolist()
from numpy import array

lst_output=[]
i=0
n=15   # next number of days for which we are predicting
while(i<n):
    x_input.shape
    
    if(len(temp_input)>look_back):
        x_input=np.array(temp_input[1:])
        x_input=x_input.reshape(1,-1)
        x_input = x_input.reshape((1, look_back, 1))
        yhat = model.predict(x_input, verbose=0)
        temp_input.extend(yhat[0].tolist())
        temp_input=temp_input[1:]
        lst_output.extend(yhat.tolist())
        i=i+1
    else:
        x_input = x_input.reshape((1, look_back,1))
        yhat = model.predict(x_input, verbose=0)
        temp_input.extend(yhat[0].tolist())
        lst_output.extend(yhat.tolist())
        i=i+1
    

print(lst_output)
day_new=np.arange(1, look_back+1)
day_pred=np.arange(look_back+1, look_back+n+1)
plt.plot(day_new, min_max_scaler.inverse_transform(ds[len(ds)-look_back:]))
plt.plot(day_pred, min_max_scaler.inverse_transform(lst_output))<jupyter_output><empty_output><jupyter_text>* Blue curve shows the data for last 15 days of the given data and orange curve show the predicted data for next 15 days based on the blue curve data<jupyter_code>plt.figure(figsize=(15,7))
df0=ds.tolist()
df0.extend(lst_output)
plt.plot(df0[2000:])<jupyter_output><empty_output><jupyter_text>* Predicted data of the prevous step is appended to the original data (here shown from day 2000 onwards)<jupyter_code>plt.figure(figsize=(15,7))
df0=min_max_scaler.inverse_transform(df0).tolist()
plt.plot(df0)<jupyter_output><empty_output><jupyter_text>* Predicted data appended to the original data, complete data shown here<jupyter_code># serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13896713615023473, 'fraction_numerical': 0.028034875922199866, 'mean_word_length': 6.463463463463463, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '402973', 'license': 'Apache-2.0', 'n_tokens_mistral': 2803, 'n_tokens_neox': 2587, 'n_words': 501, 'path': 'LSTM(updated).ipynb', 'repo_name': 'uttej2001/Using-Bitcoin-Pricing-Data-to-Create-a-Profitable-Algorithmic-Trading-Strategy', 'size': '7455'}