<jupyter_start><jupyter_text>How to do it... 1. Import required libraries<jupyter_code>import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM

from sklearn.preprocessing import MinMaxScaler<jupyter_output><empty_output><jupyter_text>2. Set prediction horizon and network hyperparameters<jupyter_code>prediction_days = 60
nof_units = 4<jupyter_output><empty_output><jupyter_text>3. Encapsulate model creation within a function<jupyter_code>def create_model(nunits):
    
    # Initializing the RNN
    regressor = Sequential()
    
    # Adding the input layer and the LSTM layer
    regressor.add(LSTM(units=nunits, activation='sigmoid', input_shape=(None, 1)))
    
    # Add output layer
    regressor.add(Dense(units = 1))
    
    # Compiling the RNN
    regressor.compile(optimizer='adam', loss='mean_squared_error')
    
    return regressor<jupyter_output><empty_output><jupyter_text>4. Load data<jupyter_code># Import the dataset and encode the date
df = pd.read_csv('bitstampUSD_1-min_data_2012-01-01_to_2021-03-31.csv')
df['date'] = pd.to_datetime(df['Timestamp'], unit='s').dt.date
group = df.groupby('date')
Real_Price = group['Weighted_Price'].mean()<jupyter_output><empty_output><jupyter_text>5. Split data into train and test<jupyter_code>df_train = Real_Price[:len(Real_Price)-prediction_days]
df_test = Real_Price[len(Real_Price)-prediction_days:]<jupyter_output><empty_output><jupyter_text>6. Apply preprocessing to improve convergence<jupyter_code>training_set = df_train.values
training_set = np.reshape(training_set, (len(training_set), 1))

sc = MinMaxScaler()
training_set = sc.fit_transform(training_set)
X_train = training_set[0:len(training_set)-1]
y_train = training_set[1:len(training_set)]
X_train = np.reshape(X_train, (len(X_train), 1, 1))<jupyter_output><empty_output><jupyter_text>7. Fit model<jupyter_code>regressor = create_model(nunits = nof_units)
regressor.fit(X_train, y_train, batch_size=5, epochs=100)<jupyter_output>WARNING:tensorflow:Layer lstm_1 will not use cuDNN kernels since it doesn't meet the criteria. It will use a generic GPU kernel as fallback when running on GPU.
Epoch 1/100
663/663 [==============================] - 1s 1ms/step - loss: 0.0129
Epoch 2/100
663/663 [==============================] - 1s 1ms/step - loss: 0.0078
Epoch 3/100
663/663 [==============================] - 1s 1ms/step - loss: 0.0038
Epoch 4/100
663/663 [==============================] - 1s 1ms/step - loss: 0.0013
Epoch 5/100
663/663 [==============================] - 1s 1ms/step - loss: 3.2707e-04
Epoch 6/100
663/663 [==============================] - 1s 1ms/step - loss: 1.2027e-04
Epoch 7/100
663/663 [==============================] - 1s 1ms/step - loss: 9.9372e-05
Epoch 8/100
663/663 [==============================] - 1s 1ms/step - loss: 9.5801e-05
Epoch 9/100
663/663 [==============================] - 1s 1ms/step - loss: 9.1653e-05
Epoch 10/100
663/663 [==============================] - 1s 1ms/step - loss: 8.614[...]<jupyter_text>8. Using the trained model, we create a time series prediction over the horizon<jupyter_code>test_set = df_test.values
inputs = np.reshape(test_set, (len(test_set), 1))
inputs = sc.transform(inputs)
inputs = np.reshape(inputs, (len(inputs), 1, 1))
predicted_BTC_price = regressor.predict(inputs)
predicted_BTC_price = sc.inverse_transform(predicted_BTC_price)<jupyter_output><empty_output><jupyter_text>9. View prediction results<jupyter_code>plt.figure(figsize=(25,15), dpi=80, facecolor='w', edgecolor='k')
ax = plt.gca()
plt.plot(test_set, color='red', label='Real BTC Price')
plt.plot(predicted_BTC_price, color='blue', label='Predicted BTC Price')
plt.title('BTC Price Prediction', fontsize=40)
df_test = df_test.reset_index()
x = df_test.index
labels = df_test['date']
plt.xticks(x, labels, rotation = 'vertical')
for tick in ax.xaxis.get_major_ticks():
    tick.label1.set_fontsize(18)
for tick in ax.yaxis.get_major_ticks():
    tick.label1.set_fontsize(18)
plt.xlabel('Time', fontsize=40)
plt.ylabel('BTC Price(USD)', fontsize=40)
plt.legend(loc=2, prop={'size': 25})
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1906005221932115, 'fraction_numerical': 0.058390695466413484, 'mean_word_length': 6.191126279863481, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '151202', 'license': 'MIT', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1481, 'n_words': 315, 'path': 'Chapter 9 - Recurrent Neural Networks/Bitcoin price prediction.ipynb', 'repo_name': 'Young-Picasso/TensorFlow2.x_Cookbook', 'size': '4213'}