Stock Price prediction using LSTM

Harsh
5 min readJun 19, 2020

--

Stock price predictions are a hot topic of debate among data scientists and machine learners. LSTM has turned about to be a good alternative to traditional machine learning algorithms.

So , the big question — What is LSTM and why are we using it?

LSTM is a short form of Long Short Term Memory. LSTM networks are a type of recurrent neural network capable of learning order dependence in sequence prediction problems. This is a behavior required in complex problem domains like machine translation, speech recognition, and more. LSTMs are a complex area of deep learning.

Unlike standard feedforward neural networks, LSTM has feedback connections. It can not only process single data points, but also entire sequences of data , hence saving you a ton of time.

Recurrent neural networks have following advantages-

  • That the system be able to store information for an arbitrary duration.
  • That the system be resistant to noise (i.e. fluctuations of the inputs that are random or irrelevant to predicting a correct output).
  • That the system parameters be trainable (in reasonable time).

We will discuss LSTM with an example project here. We will make a stock price prediction model and a very accurate one as well.

We’ll start by taking the stock market price of Apple and we will use the data available online.Also for starters , we will take the time duration as 5 years. i used the data from yahoo in this instance , but you can choose any other data source if you want to.

df = web.DataReader("AAPL" , data_source='yahoo' , start='2012-01-01' , end='2017-12-17')

we might find some columns missing but that may be because there was no recording of prices those days. ( most likely because stock market was closed that day ). that being done , we can now plot the graph and see the values. the graph looks something like this. it is based on the closing prices and the corresponding date of the closing price.

we can observe that the price have increased over the time , so that’s a good start as we can confirm that our data is not faulty.

Next thing we need to do is , to scale the data. it’s a good practice to scale the data as It basically helps to normalize the data within a particular range. Sometimes, it also helps in speeding up the calculations in an algorithm. After scaling we will divide the data into training and testing sets. ( Pretty standard , nothing to discuss about it. ) we will scale it from zero to one.

Now , coming to the main part , the LSTM model. first thing we need to do here is reshape the data because LSTM needs 3D input data. LSTM layer is a recurrent layer, hence it expects a 3-dimensional input (batch_size, timesteps, input_dim) . Empirical evidence shows that LSTM can learn up to 100 time steps, so feeding larger sequences won’t make it learn better , and as we know our current data is 2D type.

x_train = np.reshape(x_train,(x_train.shape[0],x_train.shape[1],1))

Now our input data is ready , we can now , write the model now

model = Sequential()
model.add(LSTM(50, return_sequences=True , input_shape=(x_train.shape[1],1)))
model.add(LSTM(50,return_sequences=False))
model.add(Dense(25))
model.add(Dense(1))

Things to notice in the above code is that when we initially return sequence as true , we are saying that we need another layer , but in the next layer we declare the sequence false as we do not need another LSTM layer for our model. we will take batch size and epochs as 1.

All done till here , now we will compile the model using “Adam” optimizer and loss will be measured in MSE.

So , we calculated the root mean square error and it turns out to be amazing. a value as low as this means that error is reduced substantially , and the thing to notice is that there no overfitting.

All set and ready , we will now make the predictions. A little something to remember here is that , our data is scaled and we need to unscale the data. we can do that easily by using “Inverse transform on predictions”

And now we can go ahead and plot the new graph with the following code , which might look like this

The green line depicts the predicted values while the blue lines values are the one we used for training our model. as we can see the results are pretty accurate and predict the correct prices. So we were successful in creating a stock prediction model and now we can use it to predict the future stock prices !!

CONCLUSION — So we saw here how we can predict saw prices using LMST and how successful and advantageous can it be to use Recurrent Neural Network for predictions of something as important as a stock price. we can use it in many other fields , as we will see in upcoming papers.

check the source code for the project on — https://github.com/hahaharsh7/stock-price-predictions/blob/master/prediction.ipynb

follow me on linkedin for updates — https://www.linkedin.com/in/harsh-41493b189/

--

--