Time-Series Forecasting: Predicting Apple Stock Price Using An LSTM Model

Rohan Kumar
Analytics Vidhya
Published in
3 min readFeb 9, 2021


Time-series & forecasting models

Traditionally most machine learning (ML) models use as input features some observations (samples / examples) but there is no time dimension in the data.

Time-series forecasting models are the models that are capable to predict future values based on previously observed values. Time-series forecasting is widely used for non-stationary data. Non-stationary data are called the data whose statistical properties e.g. the mean and standard deviation are not constant over time but instead, these metrics vary over time.

These non-stationary input data (used as input to these models) are usually called time-series. Some examples of time-series include the temperature values over time, stock price over time, price of a house over time etc. So, the input is a signal (time-series) that is defined by observations taken sequentially in time.

A time series example

The LSTM model

Long short-term memory (LSTM) is an artificial recurrent neural network (RNN) architecture used in the field of deep learning. Unlike standard feedforward neural networks, LSTM has feedback connections. It can not only process single data points (e.g. images), but also entire sequences of data (such as speech or video inputs).

LSTM models are able to store information over a period of time.

Let’s start coding!

We are going to build a multi-layer LSTM recurrent neural network to predict the last value of a sequence of values i.e. the AAPL stock price in this example.

Modules needed: Keras, Tensorflow, Pandas, Scikit-Learn & Numpy

Let’s load the data and inspect them:

We’ll be taking last 20 years of EOD data of AAPL, the more the merrier.

I am using the AAPL dataset, which is already divided into training set and test set, but you can do the divison with a simple command!

training_set = df.iloc[:4780, 1:2].values
test_set = df.iloc[4780:, 1:2].values

Let’s visualise the dataset

First few rows of the dataset

The target value to be predicted is going to be the “Close” stock price value. Hence the following statement.

training_set = dataset_train.iloc[:, 1:2].values

It’s a good idea to normalize the data before model fitting. This will boost the performance.

Building the input variable

As an example for this article I used the model described above to predict closing price of AAPLstock for next trading day given data from last sixty trading day

We have now reshaped the data into the following format (#values, #time-steps, #1 dimensional output).

Now, it’s time to build the model. We will build the LSTM with 100 neurons and 5 hidden layers. Finally, we will assign 1 neuron in the output layer for predicting the normalized stock price. We will use the MSE loss function and the Adam stochastic gradient descent optimizer.

It will go on like this

Getting the test data ready and making predictions

Visualising the results

We can clearly see that our model performed very good. It is able to accuretly follow most of the unexcepted jumps/drops however, for the most recent date stamps, we can see that the model expected (predicted) lower values compared to the real values of the stock price.

Disclaimer There have been attempts to predict stock prices using time series analysis algorithms, though they still cannot be used to place bets in the real market. This is just a tutorial article that does not intent in any way to “direct” people into buying stocks.

Till next time!



Rohan Kumar
Analytics Vidhya

Poet | Story writer | Blogger "I took a walk in the woods and came out taller than the trees."~ Henry David Thorea