How to predict the future using Python 🐍?

How to predict the future using Python 🐍?

Hi fellas! I am Yash Makan, a black magician 🦹‍♂️ and you know what, I can predict anything using Python.

Hi fellas! I am Yash Makan, a black magician 🦹‍♂️ and you know what I can predict anything. Yup! I can predict the future of covid, google stock price, bitcoin price and your business profit. To avail of my services simply go to blackmagicfool.com. It would be a pleasure working with you.

https://media.giphy.com/media/09bVX2WzBhZK8KwhqP/giphy.gif

Different Models of Predictive Analysis

  1. ARIMA - Auto-Regressive Integrated Moving Average
  2. LSTM - Long Short Term Memory
  3. Auto-Regressive model
  4. Exponential Smoothing

All of these models have one thing in common that they require data to be trained in and then we have to tweak the parameters accordingly to see whichever yield the best results. But today we are not going to use these models, I am going to show you a third party library created by Facebook known as fbpropet.

But why fbprophet?

I know you must be thinking that why do we are using a third party Facebook library if we already have these models. The problem with these models is that you have to tweak the parameters to see which way yields the best result whereas fbprophet lets all of this work on its own. It is a simple yet powerful library in which you simply has to pass the historical data and it can predict the future.

Installation

To install fbprophet you must have anaconda installed or you can use google colab and the installation works flawlessly. I personally will be using google colab

First, you have to install pystan that helps run fbprophet easily. You can install pystan using

!pip install pystan

Then you can install fbpropeht using the following command

!pip install fbprophet
or
conda install -c conda-forge fbprophet

Then, we will be using yfinance to fetch the data of the stock price

!pip install yfinance

At last, we have to install pandas and plotly to work with historical data and plot it

!pip install plotly==4.4.1
!pip install pandas==1.3.4

Implementation in Python

So, as an example, let’s predict the future 3 years of the reliance share price using python.

  1. Importing libraries First, we have to import the necessary libraries that we have just installed

    Import libraries

  2. (Optional) Ignore any warnings to make the output cleaner

    warning

    (Optional) To show plotly plots in google colab

    Plotly in google colab

  3. Download the historical data from the internet. I am using yahoo finance API to install the historical data but you can fetch data from anywhere. Also, I have assigned the prediction time to 3 years

    Fetching historical data

    The data is from 1996 where this stock IPO till current date and it looks something like this

    Historical data output

  4. Format the historical data for the fbprophet library.

    Here as you can see I created a new dataframe and assigned the date column as ds and close values as y. This is very important as the column names must be ds and y as fbprophet will be looking for these columns. df[’ds’] —> column of date df[’y’] —> the historical values that you have to feed to the model

    Formatting the historical data

  5. Training the model Training the model basically means to feed the historical data and the model trains it feeding data one by one and reducing the loss. Loss is basically a number that represents how bad the model is predictions on the given data. The lesser the loss number, the better your model would be.

    Training the model

  6. Making future predictions Here we will use our previously trained model and predict the future possible outcomes

    Making future predictions

    The forecast looks something like this

    Future 3 years forecast output

  7. Plotting the forecast Actually, we have successfully predicted the future but for the analysis purpose, it is a great practice to visualize using graphs. Here I will be using a scatter plot and it’ll look something like this

    Plotting the forecast table in scatter plot graph

  8. What does this forecast represent?

    • On the y axis, you can see the share price and on the x-axis, you can see the dates.
    • The black line is the actual price
    • The predicted range be the green and blue lines. This means that the share price will be between the green and blue line
    • The red line shows the trend and in this example, the trend is clearly showing up in the next 3 years

Get The Source Code For This Blog

Conclusion

So there you have it, it's that easy to predict future values using python. Now let me clear one thing that this blog is not a financial advice blog and if anyone from India reading this blog thinks, “Oh! this is a great opportunity to invest in reliance India”, then I would like to say hold your horses and do proper technical analysis before investing in any stock either in Indian stock market or any other. Also if you want to predict future values of anything else other than stocks then you can do that as well. You simply need the historical data. The more data you have, the better be the prediction. With all that being said, I would like to conclude my today’s blog. You can follow me here or on twitter to get the latest updates whenever I post new cool tech material. Also if you are willing to donate, then I will highly appreciate that and you can do that using this link. If you have any doubts, just comment down below and I will surely help you out. Alrighty! It’s time to say b-bye!

https://media4.giphy.com/media/TfFDiw5gBuEns4ykJl/giphy.gif?cid=790b7611431abc7a85c76da6c399a5727f24e501f9a5cae9&rid=giphy.gif&ct=g

Contact Me

Website: yashmakan.co.in

Twitter: @Yash_Makan

Contribute

buymeacoffee: @Here