<jupyter_start><jupyter_text><jupyter_code>!pip install alpha_vantage
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from alpha_vantage.cryptocurrencies import CryptoCurrencies
cc = CryptoCurrencies(key='<KEY>', output_format='pandas')
crypto_data, meta_data = cc.get_digital_currency_daily(symbol='BTC', market='CNY')
crypto_data['4b. close (USD)'].plot()
plt.tight_layout()
plt.title('Daily close value for bitcoin (BTC)')
plt.grid()
plt.show()
crypto_data

crypto_df = crypto_data["4b. close (USD)"]
crypto_df
crypto_df.describe()
bitcoin_ordered_df = crypto_df.loc[::-1]
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM, Flatten
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from keras.layers import ConvLSTM2D
from keras.callbacks import EarlyStopping
import datetime
from keras.preprocessing.sequence import TimeseriesGenerator
import numpy
dataset=bitcoin_ordered_df.values
#dataset.reshape(-1, 1)
dataset=dataset.astype('float32')
dataset = np.reshape(dataset,(dataset.size,1))
scaler=MinMaxScaler(feature_range=(0,1))
dataset=scaler.fit_transform(dataset)<jupyter_output><empty_output><jupyter_text>The train and test data will contain two-thirds and one-third of the dataset respectively.<jupyter_code>train_size=int(len(dataset)*0.66)
test_size=len(dataset)-train_size
train,test = dataset[0:train_size],dataset[train_size:] #Splitting the dataset into test and train
print("Train Size:",train_size)
print("Test Size",test_size)<jupyter_output>Train Size: 660
Test Size 340<jupyter_text>The **to_sequences** method creates a dataset where X is the closing value of the cryptocurrency at a given time **(t, t-1, t-2...)** and Y is the closing value at the next time **(t + 1)**. Where **seq_size** is the number of previous time steps to use as input variables to predict the next time period.<jupyter_code>def to_sequences(dataset, seq_size=1):
    x = []
    y = []

    for i in range(len(dataset)-seq_size-1):
        window = dataset[i:(i+seq_size), 0]
        x.append(window)
        y.append(dataset[i+seq_size, 0])
        
    return np.array(x),np.array(y)
seq_size = 10  # Number of time steps to look back 
#Larger sequences (look further back) may improve forecasting.

trainX, trainY = to_sequences(train, seq_size)
testX, testY = to_sequences(test, seq_size)

print("Shape of training set: {}".format(trainX.shape))
print("Shape of test set: {}".format(testX.shape))
trainX.shape[0]
#Convolutional univariate LSTM
#The shape of input data must be: [samples, timesteps, rows, columns, features]

trainX = trainX.reshape((trainX.shape[0], 1, 1, 1, seq_size))
testX = testX.reshape((testX.shape[0], 1, 1, 1, seq_size))

model = Sequential()
model.add(ConvLSTM2D(filters=64, kernel_size=(1,1), activation='relu', input_shape=(1, 1, 1, seq_size)))
model.add(Flatten())
model.add(Dense(32))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error')
model.summary()

model.fit(trainX, trainY, validation_data=(testX, testY),
          verbose=2, epochs=100)
#Making predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
testPredict<jupyter_output><empty_output><jupyter_text>We must invert the prediction back to prescaled values in order to compare them with the original input values. Since we used **MinMaxScaler** to **normalize** the original values, we can now use **scaler.inverse_transform** to **reverse the tranformation**(normalization).<jupyter_code>trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))

testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))<jupyter_output>Train Score: 324.73 RMSE
Test Score: 6099.08 RMSE<jupyter_text>plt.figure()plt.plot(history.history['loss'])plt.plot(history.history['val_loss'])plt.title('Model loss')plt.ylabel('Loss')plt.xlabel('Epoch')plt.legend(['Train', 'Test'], loc='upper left')<jupyter_code>#Shift train predictions for plotting
#We must shift the predictions so that they align on the x-axis with the original dataset. 
trainPredictPlot = np.empty_like(dataset)
trainPredictPlot[:, :] = np.nan
trainPredictPlot[seq_size:len(trainPredict)+seq_size, :] = trainPredict

# Shift test predictions for plotting
testPredictPlot = np.empty_like(dataset)
testPredictPlot[:, :] = np.nan
testPredictPlot[len(trainPredict)+(seq_size*2)+1:len(dataset)-1, :] = testPredict
dates_ind=[]
for i in range(999,0,-100):
  dates_ind.append(i)
dates_ind.append(0)

dates=[]

for i in dates_ind:
  dates.append(crypto_data.iloc[i].name.date())
dates


# plot baseline and predictions
fig, ax = plt.subplots(figsize=(20, 10))

plt.plot(scaler.inverse_transform(dataset), label="Actual Closing Value")
plt.plot(trainPredictPlot, label="Predicted Values on Train Data")
plt.plot(testPredictPlot, label="Predicted Values on Test Data")
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)

plt.ylabel('Prices in USD')
  
plt.xticks([0,100, 200, 300, 400, 500, 600, 700, 800, 900, 999],dates)
plt.show()
scaler.inverse_transform(dataset[-20:])
x_input = dataset[-50:]
x_pred, y_pred = to_sequences(x_input,10)
x_pred = x_pred.reshape(x_pred.shape[0],1,1,1,10)
x_pred
future_predict = model.predict(x_pred)
future_predict = scaler.inverse_transform(future_predict)
future_predict


# plot baseline and predictions
fig, ax = plt.subplots(figsize=(20, 10))

plt.plot(scaler.inverse_transform(dataset), label="Actual Closing Value")
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
plt.plot(future_predict, label = "Future Predicted Values")
plt.ylabel('Prices in USD')
  
plt.xticks([0,100, 200, 300, 400, 500, 600, 700, 800, 900, 999],dates)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13246116107931316, 'fraction_numerical': 0.028454619787408014, 'mean_word_length': 6.881443298969073, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '474848', 'license': 'Apache-2.0', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2061, 'n_words': 505, 'path': 'TGIFHacks_125_Cryptocurrency_Forecasting.ipynb', 'repo_name': 'tejasr112002/ConvLab-2', 'size': '6126'}