<jupyter_start><jupyter_text>Import Data<jupyter_code>! pip install ipython-autotime

%load_ext autotime
import pandas as pd

bgr_df = pd.read_csv('/content/drive/My Drive/Colab Notebooks/Data/bgr.us.txt')
bgr_open_np = bgr_df.iloc[:, 1:2].values
bgr_df.head()<jupyter_output><empty_output><jupyter_text>Select only "Open" columns<jupyter_code>bgr_open_np
%config InlineBackend.figure_format = 'svg'

import seaborn as sns
import matplotlib.pyplot as plt

sns.set(rc={'figure.figsize':(8, 4)})

plt.plot(bgr_open_np)
plt.show()<jupyter_output><empty_output><jupyter_text>Data Preprocessing  Data Scaling<jupyter_code>from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler(feature_range= (0,1))
bgr_open_scaled_np = scaler.fit_transform(bgr_open_np)
bgr_open_scaled_np
bgr_open_scaled_np.shape<jupyter_output><empty_output><jupyter_text>Train / Test Preparation<jupyter_code>import numpy as np
X_train = []
y_train = []

window_size = 60

for _ in range(window_size, len(bgr_open_scaled_np)) :
    X_train.append(bgr_open_scaled_np[_-window_size : _, 0])
    y_train.append(bgr_open_scaled_np[_, 0])

X_train, y_train = np.array(X_train), np.array(y_train)
X_train.shape
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1],1))
X_train.shape
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, shuffle=False, test_size=0.2)<jupyter_output>time: 62.8 ms<jupyter_text>Modeling<jupyter_code>import tensorflow as tf

def build_lstm(input_data, drop_out=0.2, loss: str = 'mse', optimiser: str = 'adam') :
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.LSTM(50, return_sequences=True, input_shape=(input_data.shape[1], input_data.shape[2])))
    model.add(tf.keras.layers.Dropout(drop_out))
    model.add(tf.keras.layers.LSTM(50, return_sequences=True))
    model.add(tf.keras.layers.Dropout(drop_out))
    model.add(tf.keras.layers.LSTM(50, return_sequences=True))
    model.add(tf.keras.layers.Dropout(drop_out))
    model.add(tf.keras.layers.LSTM(50, return_sequences=False))
    model.add(tf.keras.layers.Dropout(drop_out))
    model.add(tf.keras.layers.Dense(1))

    model.compile(optimizer=optimiser, loss=loss)
    return model
model = build_lstm(X_train)
model.summary()
model.fit(X_train, y_train, epochs=100, batch_size=32)
y_pred = model.predict(X_test)<jupyter_output>time: 1.18 s<jupyter_text>Evaluation<jupyter_code>plot_y_test = scaler.inverse_transform(np.array([[_] for _ in y_test]))[:,0]
plot_y_pred = scaler.inverse_transform(y_pred)[:, 0]
plt.plot(plot_y_test, label='Actual')
plt.plot(plot_y_pred, label='Predict')
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13476490188818957, 'fraction_numerical': 0.015179563124768604, 'mean_word_length': 7.114114114114114, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '632254', 'license': 'MIT', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1099, 'n_words': 148, 'path': 'stock_price_lstm_example.ipynb', 'repo_name': 'suakow/stock_price_prediction_lstm_practice', 'size': '2701'}