Harbola DataScience

Answer, Read and Post Coding related Questions!

How To Predict Stock Prices using Machine Learning

The stock market is constantly changing and follows no or little patterns. Is is non linear. Because of various aspects such as politics, global economic conditions, brand awareness, a company’s financial performance, and so on.

Accurate stock price forecast is exceedingly tricky but can be done to some extent using Deep learning Neural Networks. It is hard or nearly impossible to do with Machine learning algorithms.

Therefore to train deep learning neural net we require a large number of datapoints.

Financial analysts, researchers, and data scientists continue to investigate analytics strategies for detecting stock market patterns this spawned the idea of algorithmic trading, which employs automatic, pre-programmed trading.

What precisely is a stock exchange?

A stock market is a market where you may buy and sell shares in publicly traded corporations. In publicly listed companies, the stocks, also known as equities, indicate ownership in the corporation. The stock exchange acts as a middleman, facilitating the buying and selling of shares. 

Stock Price Prediction

Stock Price Prediction using Machine Learning assists you in determining the future worth of a company’s stock and other financial assets traded on an exchange. The whole point of forecasting stock prices is to make a lot of money. It is difficult to predict how the stock market will perform. Other aspects, such as physical and psychological characteristics, reasonable and illogical conduct, and so on, have a role in the forecast. All of these elements work together to make stock values dynamic and volatile. As a result, forecasting stock prices with great accuracy is extremely challenging. 

You skip next section of LSTM if you are a beginner or casual googler.

Understanding Long Short Term Memory Network

In this case, you will create your model to forecast Google stock prices using a Long Short Term Memory Network (LSTM).

LTSMs are Recurrent Neural Networks that can be used to learn long-term dependencies. It is used for time-series data processing and prediction.


The image at the top shows that LSTMs have a chain-like structure. RNNs, in general, features a single neural network layer. LSTMs, on the other hand, contains four interacting layers that communicate quite well.

LSTMs operates in three stages.

  • At LSTM, the initial step is to select which information can be excluded from the cell in that time step. A sigmoid function is being used to make the decision. It computes the function using the initial state (ht-1) and the current input xt.
  • The second layer has two functions. The sigmoid function is the first, while the tanh function is the second. The sigmoid function determines which values are allowed to pass (0 or 1). The tanh function assigns weightage to the items supplied, determining their relevance from -1 to 1.
  • The third phase is to determine what the ultimate product will be. To begin, run a sigmoid layer to decide which elements of the cell state make it to the output, the cell state must then be sent via the tanh function to push the values between -1 and 1 and multiplied the sigmoid gate output.

With this fundamental knowledge of LSTM, you may proceed to the hands-on demonstration section of this lesson on stock price prediction using machine learning.

Google Stock Price Prediction Using LSTM

We will use prebuilt libraries that will make our job easier and we can write efficient code quickly. These libraries have functions that are efficiently written. It is hard to beat this code.

Some of the python libraries that we will be using for forecasting the stocks project are :

  • numpy for numerical computation and calculations
  • pandas for working with dataframe i.e. data is aligned in a tabular form in rows and columns.
  • matplotlib to plot graph
  • sklearn (scikit-learn) the most useful library for machine learning in Python. The sklearn library contains a lot of efficient tools for machine learning and statistical modeling including classification, regression, clustering and dimensionality reduction.
  • keras – powerful and easy-to-use free open source Python library for developing and evaluating deep learning models.Contains efficient numerical computation libraries Theano and TensorFlow and allows you to define and train neural network models in just a few lines of code.

Before starting writing code you need to install these libraries on your system to avoid errors. If you unable to run. You can use google colab too for the same. Just need internet connection.

How Machine learning model works basic intuition for commoner

You have the data. Data meaning the information required to build the model. Here we need the predict the price. In machine learning we need to first train the model and then test its accuracy, how well it is predicting. This is done comparing the predicted data with actual data. We seperate few rows before training to test the accuracy. Denoted X_test, Y_test (test – data for testing purpose). X_train, Y_train (train – data for training purpose)

Second thing is features and labels. Feature are data that is independent. Labels are dependent on features. We try to predict labels. Here label is price. Generally in coding written as X_train, X_test (X for features) Y_train, Y_test(Y for labels, which we need to predict)

Then we apply artificial neural networks, cnn, rnn on our train data (X_train, Y_train)

In coding part you will be more clear.

Implementation of Stock Prediction Project in Python

Here I have used jupyter notebook as text editor to write code as it helps to run code in blocks. You can download it using pip from command prompt.

1. Import the Libraries.

2. Load the Training Dataset.

In this dataset. There are five columns in total.


Understanding the data, the Open column shows the price at which a stock started trading when the market opened on a specific day. The Close column denotes the price of a single stock when the stock exchange closed for the day. Within a particular time period, the High column represents the highest price at which a stock traded. The Low column shows the lowest price during the time. Volume refers to the overall quantity of trade activity during a certain time period. 

3. Train Your Model Using the Open Stock Price Column


4. Dataset Normalization

Normalization is a scaling technique in Machine Learning applied during data preparation to change the values of numeric columns in the dataset to use a common scale.

It transform datapoints into number between 0 to 1. This is relative. Sklearn has a function MinMaxScale to perform this efficiently.


5. Creating features and label, X train and y train.


We will pass these features (X_train) and labels (y_train) to neural network.


6. Reshape the Data.


7. Creating the Model Importing Critical Libraries and Adding Layers to LSTM.


8. Model Fitting.


Passing our data in fit function which will return a regressor object. Which have predict method which we will use in further part to predict price on new data.

9. Obtaining the Actual Stock Prices for January 2017.


New dataset “Google_stock_Price_test.csv” to test the accuracy. We will predict the price of these stock and will compare it with actual price.

10. Preparing the Model’s Input


Doing the same thing which we did earlier Normalisation, here we will not pass y_test(label, here prices) , we will predict it.

11. Predicting the Values for Jan 2017 Stock Prices.


Using inverse_transform because we converted our data to data between 0 and 1. So again reversing it. Think of it as in maths we multiply something and then divide it with same so that it don’t have any change in its value. Same thing here.

12. Plotting the Actual and Predicted Prices for Google Stocks.


As seen above, the model can accurately forecast the trend of real stock prices. The model’s accuracy can be improved training with more data and increasing the number of LSTM layers. Which need more computation power and GPU. Can be done in google colab 🙂


The stock market has a significant impact on our daily life. It plays a vital role in a country’s GDP growth. You learned the fundamentals of the stock market and how to anticipate stock prices using machine learning in this session.

Chitranshu Harbola

Self taught programmer, Web Developer and an aspiring Machine learning engineer cum Data Science student

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top