import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential, load_model from keras.layers import LSTM, Dense, Dropout import os """### Loading in Dataset""" df = pd.read_csv('/content/Data.csv') df.head() """### Preprocessing and Feature Extraction""" df = df['Close'].values df = df.reshape(-1, 1) print(df.shape) df[:5] dataset_train = np.array(df[:int(df.shape[0]*0.8)]) dataset_test = np.array(df[int(df.shape[0]*0.8)-100:]) print(dataset_train.shape) print(dataset_test.shape) scaler = MinMaxScaler(feature_range=(0,1)) dataset_train = scaler.fit_transform(dataset_train) dataset_train[:5] dataset_test = scaler.transform(dataset_test) dataset_test[:5] def create_dataset(df): x = [] y = [] for i in range(100, df.shape[0]): x.append(df[i-100:i, 0]) y.append(df[i, 0]) x = np.array(x) y = np.array(y) return x,y x_train, y_train = create_dataset(dataset_train) x_train[:1] x_test, y_test = create_dataset(dataset_test) x_test[:1] # Reshape features for LSTM Layer x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1)) x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1)) """### Building Model""" model = Sequential() model.add(LSTM(units=96, return_sequences=True, input_shape=(x_train.shape[1], 1))) model.add(Dropout(0.2)) model.add(LSTM(units=96, return_sequences=True)) model.add(Dropout(0.2)) model.add(LSTM(units=96, return_sequences=True)) model.add(Dropout(0.2)) model.add(LSTM(units=96)) model.add(Dropout(0.2)) model.add(Dense(units=1)) model.compile(loss='mean_squared_error', optimizer='adam') if(not os.path.exists('stock_prediction.h5')): model.fit(x_train, y_train, epochs=1, batch_size=32) model.save('stock_prediction.h5') from google.colab import drive drive.mount('/content/drive') model = load_model('stock_prediction.h5') """### Visualizing Results""" predictions = model.predict(x_test) predictions = scaler.inverse_transform(predictions) fig, ax = plt.subplots(figsize=(8,4)) plt.plot(df, color='red', label="True Price") ax.plot(range(len(y_train)+100,len(y_train)+100+len(predictions)),predictions, color='blue', label='Predicted Testing Price') plt.legend() y_test_scaled = scaler.inverse_transform(y_test.reshape(-1, 1)) model = Sequential() model.add(LSTM(x_train.shape[1], return_sequences=True, input_shape=[x_train.shape[1], 1])) model.add(LSTM(x_train.shape[1], return_sequences=False)) model.add(Dense(1)) model.compile(optimizer='adam', loss = 'mean_squared_error') model.fit(x_train, y_train, batch_size = 1, epochs=30) fig, ax = plt.subplots(figsize=(8,4)) ax.plot(y_test_scaled, color='red', label='True Testing Price') plt.plot(predictions, color='blue', label='Predicted Testing Price') plt.legend() x = x_test[-1] num_timesteps = 1 preds = [] for i in range(num_timesteps): data = np.expand_dims(x, axis=0) prediction = model.predict(data) prediction = scaler.inverse_transform(prediction) preds.append(prediction[0][0]) x = np.delete(x, 0, axis=0) # delete first row x = np.vstack([x, prediction]) # add prediction print(preds)