avatarJermaine Matthew

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2979

Abstract

edium.com/v2/resize:fit:800/0*_OEB7f6aYqTezFvl"><figcaption></figcaption></figure><p id="8555">There are no Nan values associated with the ‘Close’ feature. In the next step, we will plot the close and check the stock’s growth based on the time.</p><figure id="3927"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*jWS5h15ewegre0xM"><figcaption></figcaption></figure><h1 id="b710">Processing of data</h1><p id="b455">Due to the limited number of features we are going to use in this tutorial, a new dataframe will be created, dataV1, and only contain the Close feature.</p><figure id="1a60"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*kfe34Vn7I4nYcbl7"><figcaption></figcaption></figure><p id="150a">To normalize our data, import MinMaxScaler from sklearn library. It does this by transforming a feature by scaling it to a given range in this case just the ‘Close’ feature.</p><figure id="6e9a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*A5hrPVQ64AaEDiLW"><figcaption></figcaption></figure><p id="99e5">The feature length should be declared</p><figure id="7216"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*8UBDh51Wubtv043B"><figcaption></figcaption></figure><p id="a2c5">Our data has to be divided into X and Y variables before we can build the model. To do so, we will write a function called Create_Features_and_Targets.</p><figure id="defc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*SbtiNosnZ5ideCj8"><figcaption></figcaption></figure><p id="d3ef">As LSTM takes 3 dimensional data as input, data should be made 3 dimensional.</p><figure id="2109"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*8XLkBH7Ow8vort1C"><figcaption></figcaption></figure><figure id="ab3b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*RJDBdxN2TcPstN99"><figcaption></figcaption></figure><h1 id="8767">Model creation</h1><p id="ad3e">Create the model by importing the required libraries</p><figure id="ce2b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*tRt5C0Zp_lcO4YVo"><figcaption></figcaption></figure><p id="b1e8">Adam will be used as an optimizer for the LSTM model we will create which consists of two LSTM layers, two dropout layers, and one dense layer.</p><h1 id="5443">How does LSTM work?</h1><p id="c8c9"><i>LSTMs are recurrent neural networks (RNNs). In simple terms, LSTMs work by allowing the network to remember the context of the model while forgetting the irrelevant information.</i></p><figure id="4e31"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*5_uZZoC6tdyfEPgS"><figcaption></figcaption></figure><figure id="d1d5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*bZnhz78_vrlG4PyI"><figcaption></figcaption></figure><p id="afe5">The data has already been prepared and the model has been developed. Using the model.fit function, we can now train the model. Our model will be t

Options

rained using a batch_size of 12 under 100 epochs.</p><figure id="58cc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*OXOYjdi5ogfJ4aLM"><figcaption></figcaption></figure><p id="399e">It’s time to test the model now that we have successfully trained it. For testing the model, the whole data set will be used, and the predicted data will be plotted against the actual data.</p><h1 id="6988">Predicting and testing</h1><p id="5635">To run the tests, you will need to collect data from two datasets. The ‘Close’ feature should be used to create two variables, one for plotting real data, y_real, and one for making predictions, x_test. Our goal is to create the x_test variable by getting all the values from the ‘Close’ feature, scaling the data with Scaler.transform and transforming the shape to a 3D shape. From the ‘Close’ feature, we can get all values starting from the 101st value to create the y_real variable.</p><figure id="3a62"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*hmwenpG7K4uY0cpg"><figcaption></figcaption></figure><h1 id="7754">Making predictions</h1><p id="9f62">Now let’s plot the data after running the prediction test</p><figure id="2ed8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*wOFrO32-MEttplAN"><figcaption></figcaption></figure><p id="ab26">Comparison of predicted and actual stock prices</p><figure id="23bd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*pxgpL7dehNwNvkhZ"><figcaption></figcaption></figure><figure id="748c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*UqTfKCied_zxDfif"><figcaption></figcaption></figure><h1 id="2194">Predictions in real time</h1><p id="cb78">Here’s a real-time prediction we can make. The objective of this exercise is to create a function that predicts the stock price on a given date.</p><figure id="6dab"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*10pypBd2p-az4om9"><figcaption></figcaption></figure><figure id="da5e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*4wwwYtldFiQrWbLT"><figcaption></figcaption></figure><p id="2596">Taking a look at our model’s prediction for the given date of 2022–09–20, let’s see if it’s any closer to the actual price on that date. Here is the code that does just that. If the date provided already exists in the dataset, this gives the correct result.</p><figure id="2b53"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*UCGqTpOMJ2bVbHLR"><figcaption></figcaption></figure><figure id="2851"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*2gacdo1KgCjxUEtU"><figcaption></figcaption></figure><p id="0352">The practical guide has come to an end. Despite the fact that we can make this better, I do not recommend using it for real-time trading at this time.</p><p id="1b28">Thank you for taking the time to read this tutorial. I hope you enjoyed it. I appreciate your time.</p></article></body>

Forecasting the stock market using LSTM; will it rise tomorrow.

Using a machine learning model, this tutorial will predict a stock’s future value in real time with high accuracy on a stock exchange. To predict if the stock price will rise or fall tomorrow, we will use Google stock data as a time series. The data will be obtained from the Yfinance API.

Prepare the project by importing the required libraries

Using the Yfinance API, download Google stock data

In addition to checking the dataset details such as the data types of the features, we also need to check if there are any null values.

Six features are included in the dataset. Here are some of these features:

  • Open. At the beginning of trading on a particular day, the stock’s price was
  • High. During a trading day, the stock’s highest value
  • Low. The lowest price at which a stock trades on a given day
  • Close. During a trading day, this is the last price at which a stock traded
  • Adj Close. Including any corporate actions, this is the stock’s closing price.
  • Volume. During a particular period, the number of shares traded in a stock

On the New York Stock Exchange (NYSE), for instance, trading takes place from Monday through Friday from 9:30 a.m. through 4:00 p.m. ET. The opening price (open) is the price at which the stock starts trading, which is 9:30 a.m., and the closing price (close) is the price when the trading ends, which is 4:00 p.m. As an example, the New York Stock Exchange (NYSE) is open from 9:30 am to 4:00 pm Eastern time from Monday to Friday. In this case, the opening price (open) is what the stock is worth when it starts trading at 9:30 am and the closing price (close) is what it is worth when it ends trading at 4:00 pm.

In the next step, we will check for non-numeric values in the ‘Close’ feature and change them to Nan. Check for non-numeric values in the dataset as well.

There are no Nan values associated with the ‘Close’ feature. In the next step, we will plot the close and check the stock’s growth based on the time.

Processing of data

Due to the limited number of features we are going to use in this tutorial, a new dataframe will be created, dataV1, and only contain the Close feature.

To normalize our data, import MinMaxScaler from sklearn library. It does this by transforming a feature by scaling it to a given range in this case just the ‘Close’ feature.

The feature length should be declared

Our data has to be divided into X and Y variables before we can build the model. To do so, we will write a function called Create_Features_and_Targets.

As LSTM takes 3 dimensional data as input, data should be made 3 dimensional.

Model creation

Create the model by importing the required libraries

Adam will be used as an optimizer for the LSTM model we will create which consists of two LSTM layers, two dropout layers, and one dense layer.

How does LSTM work?

LSTMs are recurrent neural networks (RNNs). In simple terms, LSTMs work by allowing the network to remember the context of the model while forgetting the irrelevant information.

The data has already been prepared and the model has been developed. Using the model.fit function, we can now train the model. Our model will be trained using a batch_size of 12 under 100 epochs.

It’s time to test the model now that we have successfully trained it. For testing the model, the whole data set will be used, and the predicted data will be plotted against the actual data.

Predicting and testing

To run the tests, you will need to collect data from two datasets. The ‘Close’ feature should be used to create two variables, one for plotting real data, y_real, and one for making predictions, x_test. Our goal is to create the x_test variable by getting all the values from the ‘Close’ feature, scaling the data with Scaler.transform and transforming the shape to a 3D shape. From the ‘Close’ feature, we can get all values starting from the 101st value to create the y_real variable.

Making predictions

Now let’s plot the data after running the prediction test

Comparison of predicted and actual stock prices

Predictions in real time

Here’s a real-time prediction we can make. The objective of this exercise is to create a function that predicts the stock price on a given date.

Taking a look at our model’s prediction for the given date of 2022–09–20, let’s see if it’s any closer to the actual price on that date. Here is the code that does just that. If the date provided already exists in the dataset, this gives the correct result.

The practical guide has come to an end. Despite the fact that we can make this better, I do not recommend using it for real-time trading at this time.

Thank you for taking the time to read this tutorial. I hope you enjoyed it. I appreciate your time.

Machine Learning
Artificial Intelligence
Deep Learning
Stock Market
Finance
Recommended from ReadMedium