RNN: Forecasting Stock Prices with LSTM

 

Hey everyone! It was one the nights that I try to create sth with Python ML and I thought that why wouldn’t I share couple of experiences here.

I run into some datas about stock prices of a company in Kaggle and try to forecast it’s next stock prices. I have used LSTM which is a powerful method for forecasting as many of you know.

LSTM method is a good example of RNN (Reinforces Neural Networks). It’s main idea is basically quite good combination of long-term memory and short-term memory. It has some procedure with tanh and sigmoid functions that I won’t go further here. So let me show you the basic apply to you.

First we should understand the data. We have basic stock market info about this company all the way 1986 to 2022. We will be dealing with only close prices of that day for now.

Stock Price info 1986 to 2022
Stock Price Info 1986 to 2022

If you familiar with some machine learning algorithm like classification or regression, we would try to predict the target value (which is stock price in this case) according to values that effects that target value. RNN is has a slight difference which is we are predicting the feature target value from past target values. So there is reinforced learning from experiences. Our model has to remember some past predicted values also. So this past target values is also inputs of our models for next predictions. This is really interesting actually if we think how human beings are getting knowledge from their past experiences.

We are using MinMaxScaler for scaling the data in order to deal with more relevant values.

0–1 is the range of our data values

And in order to pass the past target values to model as next input values we are creating a structure with 60 batch size

every data has it’s past 60 data for input

We should reshape our inputs (which is x_train) since LSTM models wants output shape too.

Let’s create model structure, we are using a Sequential model and add two LSTM layers. First, return_sequences parameter is set to True because it has another LSTM layer after it.

Let’s measure the success of model, we have 3.11 which is not bad

So let’s have a final look on graph

Traders would have goosebumps after this graph right? :)

Thanks for reading.

Cemil

Yorumlar

Bu blogdaki popüler yayınlar

FİLİSTİN ÇÖZÜMSÜZLÜĞÜNE FARKLI BİR BAKIŞ

MAHÇUP