Chapter 9: Sequential Modeling with Recurrent Neural Networks
Activity 17: Predict the Trend of Microsoft’s Stock Price Using an LSTM with 50 Units (Neurons)
Solution
- Import the required libraries:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
- Import the dataset:
dataset_training = pd.read_csv(‘MSFT_train.csv’)
dataset_training.head()
The following figure shows the output of the preceding code:
Figure 9.23: The first five rows of the dataset
- We are going to make the prediction using the Open stock price, so we will extract this column first:
training_data = dataset_training.iloc[:, 1:2].values
training_data
The following figure shows the output of the preceding code:
Figure 9.24: The extracted column (the open stock price) from the dataset
- Then, perform feature scaling by normalizing the data:
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0, 1))
training_data_scaled = sc.fit_transform(training_data)
training_data_scaled...