Stock Price Prediction Using Artificial Recurrent Neural Network — Part 2

Kumarpal Nagar
Mindful Engineering
5 min readOct 14, 2021
Image Credit: Intel® AI Developer

In the previous part of this, we understand AI, ML, Unsupervised Learning, Supervised Learning, and some steps of Machine Learning Workflow. So now in this part, we will understand the next steps Choose The Model, Train Model, etc. of Machine Learning Workflow.

Step 3: Choose The Model

There are various models of Machine Learning which are used for different purposes like handling different types of data like text, images, etc.

Here we have data is in the form of time series, so to deal with this type of data we are using the LSTM (Long Short-TimeMemory) model.

Let’s create the LSTM model with the Sequential model API.

Figure — M

We can create a model by calling Sequential() API.

To add a layer, simply create a layer using Keras layer API and then pass the layer through add() function as specified below:

To add the first LSTM layer we create a layer with 50 cells, activation function ‘Relu’, input shape according to the input data size, and return sequences True if the next layer of the model is also the LSTM layer and then we add this layer to the model using add() method.

Now add a Dropout layer ( to Prevent Neural Networks from Overfitting ) with a 20%(0.2) dropout rate.

After that, we add two more layers of LSTM as LSTM Layer-Dropout Layer-LSTM Layer.

At last, we add an output layer or Dense layer with 1 cell.

Once the model is created, you can Configure the model for training with losses and metrics using the compile() method.

Here we set parameters the mean squared error as a loss function, the “Adam Optimization Algorithm” as the optimizer, and the mean squared error as metrics.

We can print a string summary of the network using the summary() method as shown in figure-N.

Figure — N

Step 4: Train Model

In this step, we use the data(training data) for training to teach our model. Trains the model for a fixed number of epochs using the fit() method.

Here we are training the model with 100 epochs, batch size 64, and shuffle is False because our data is in a time-series format so we don’t want to shuffle training data at training time.

Figure — O

Step 5: Evaluation

After the model is trained it needs to test on the test dataset that’s why we split data at the second step of ML. In most models, we use the evaluate() function to check the model’s proficiency.

But in our case, we are not using this function. We compute the Mean Squared Error and R2 Score of the test dataset.

Figure — P

As shown in figure-O, the value of MSE is 0.0006995175556468366, which is nearest to 0, and the value of R2 is 0.9857629449949173 which is nearest to 1. This means this model is good.

Let’s draw a graph of the main dataset vs the training dataset and test dataset. First, we predict the values of the training dataset and test dataset. After that, we invert it into the normal form (invert normalization process) using the invert_transform() method.

Figure — Q

Now we reshape the predicted training dataset and test dataset and then create an array of both datasets as shown in figure Q.

Figure — R

Now we can create a graph of the Main dataset, Train the dataset, and Test the dataset using the following python commands:

Figure — S

This is the output of the datasets graph.

Figure — T

Step 6: Hyperparameter Tuning

After the complete evaluation process, we proceed to the sixth step of ML in which we will try to improve that model (if the values of evaluation or value of errors are not good). In this process, we change hyperparameter values like batch size, learning rate, etc to improve the performance of the model.

Step 7: Prediction

The final step of the ML workflow is prediction. Our Stock Prediction model can predict the next-day price of the stock. To predict the next-day value of the stock we need the last 100 days’ data from the dataset.

Now we reshape the 100-day data and put it into the predict() method which will give us the predicted value of the next-day stock price.

Once achieved, we transform it into a normal value( through an inverted normalization process).

Figure — U

As shown in figure-U, the next-day value of the stock should be 124.88(USD).

For the code, you can refer to this repo on GitHub.

That’s it!

That’s it for this time, guys, If you have any queries or questions then you can comment here.

Happy Learning.

--

--

Kumarpal Nagar
Mindful Engineering

AI Developer, Python Full Stack Developer, Certified Ethical Hacker