How to predict the future using Python 🐍?
Tags: python, beginners, fbprophet, programming
Published: 2022-01-14 09:17:08

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 www.blackmagicfool.com. It would be a pleasure working with you.

![https://media.giphy.com/media/09bVX2WzBhZK8KwhqP/giphy.gif](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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/790ljahy0yo3pn5weomv.png)
    
2. (Optional) Ignore any warnings to make the output cleaner
    
    ![warning](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plq29wcakw9es9uuho8r.png)
    
    (Optional) To show plotly plots in google colab
    
    ![Plotly in google colab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8b4jza5wuq7jxmi57gm.png)
    
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4yoeilalet91he1rad1.png)
    
    The data is from 1996 where this stock IPO till current date and it looks something like this
    
    ![Historical data output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4nyv5890lhht7bcs4onr.png)
    
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhgvq4oke7rn1ct6uxab.png)
    
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kylx6t7supfc3u2ixa9e.png)
    
6. **Making future predictions**
Here we will use our previously trained model and predict the future possible outcomes
    
    ![Making future predictions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16b345n3wblo3x2p9x09.png)
    
    The forecast looks something like this
    
    ![Future 3 years forecast output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4rglc5zztzywm87uqla.png)
    
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zok0e4vgld5skazu302h.png)
    
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](https://colab.research.google.com/drive/11ueJlrR7KhuOlMb-oBamoEOogQcmBObk)

## 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](https://dev.to/yash_makan) or on [twitter](https://twitter.com/Yash_Makan) 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](https://www.buymeacoffee.com/yashmakan). 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](https://media4.giphy.com/media/TfFDiw5gBuEns4ykJl/giphy.gif?cid=790b7611431abc7a85c76da6c399a5727f24e501f9a5cae9&rid=giphy.gif&ct=g)

## Contact Me

Website: [yashmakan.co.in](http://yashmakan.co.in)

Twitter: [@Yash_Makan](https://twitter.com/Yash_Makan)

## Contribute

buymeacoffee: [@Here](https://www.buymeacoffee.com/yashmakan) 

## Other Interesting Articles

- [21 must-read books for kids, beginner, advanced & interview with PDF 🐍](https://dev.to/yash_makan/21-must-read-books-for-kids-begineer-advanced-interview-with-pdf-56c7)
- [Improve your workflow using Automation in Python](https://dev.to/yash_makan/everything-about-automation-in-python-pc-mobile-web-raspberry-pi-4cgh)
- [4 ways to create modern GUI in python in the easiest way possible](https://dev.to/yash_makan/4-ways-to-create-modern-gui-in-python-in-easiest-way-possible-5e0e)
- [4 Free hosting platforms for python web app with step-by-step process](https://dev.to/yash_makan/4-best-python-web-app-hosting-services-for-freewith-complete-process-57nb)
- [18 Python one-liners that will speed up your coding process.](https://dev.to/yash_makan/18-python-one-liners-that-will-speed-up-your-coding-process-21md)
- [AI bot that could answer your questions](https://dev.to/yash_makan/ai-bot-that-could-answer-any-questions-asked-gmn)
- [Generate your own favourite TV episode using python](https://dev.to/yash_makan/generate-your-own-favourite-tv-episode-using-python-1pb3)