Bitcoin Price Prediction with Python

Bitcoin is a decentralized network and digital currency that uses a peer-to-peer system to verify and process transactions. In this article, I will introduce you to a machine learning project on Bitcoin price prediction with Python.

Bitcoin Price Prediction

Recently Bitcoin has received a lot of attention from the media and the public due to its recent price hike. As Bitcoin has been viewed as a financial asset and is traded through many cryptocurrency exchanges like a stock market, many researchers have studied various factors that affect the price of Bitcoin and the patterns behind its fluctuations using various analytical and predictive methods.

Also, Read – 100+ Machine Learning Projects Solved and Explained.

Several predictive methods have been studied and compared for the task bitcoin price prediction using machine learning. In this article, I will be using the Facebook Prophet model for the task of Bitcoin price prediction using machine learning with Python.

The Facebook Prophet Library is an open-source additive regression model made available by Facebook for time-series predictions. While there is a more advanced version of the Prophet like NeuralProphet which is based on neural networks, I will be using the simplified version which uses machine learning techniques for the Bitcoin price prediction task. If you want to know more about how the Facebook Prophet template works, you can learn here.

Bitcoin Price Prediction with Python using Machine Learning

Now in this section, I will take you through a machine learning project on Bitcoin Price Prediction. The first step is to download the data. Now, let’s see how to download the latest data of bitcoin prices:

  1. Visit Yahoo Finance
  2. Type “Bitcoin” in the search bar
  3. You will get a summary of bitcoin prices, then just click on the historical data and then click on download as shown in the image below.
download data from yahoo finance

Now after downloading the data, it’s time to import the necessary Python libraries and the dataset. Before importing the libraries make sure that you have installed the fbprophet model. You can easily install it by using a pip command; pip install fbprophet. Now let’s import the libraries:

import pandas as pd
from fbprophet import Prophet

The Facebook Prophet model only works with data that contains a string time-series format in a column called “ds” and continuous values in a column called “y”. So we need to create the data accordingly:

df = pd.read_csv('BTC-USD.csv')
df = df[["Date", "Close"]]
df.columns = ["ds", "y"]
             ds             y
0    2020-01-05   7411.317383
1    2020-01-06   7769.219238
2    2020-01-07   8163.692383
3    2020-01-08   8079.862793
4    2020-01-09   7879.071289
..          ...           ...
362  2021-01-01  29374.152344
363  2021-01-02  32127.267578
364  2021-01-03  32782.023438
365  2021-01-04  31971.914063
366  2021-01-05  30412.328125

[367 rows x 2 columns]

Now let’s fit the data into our model:

prophet = Prophet()

Now let’s make predictions. The make_future_dataframe method in Prophet model has a parameter named as ‘periods’, we can use it to set the amount of time we need to make predictions. Now let’s make predictions for the next 365 days:

future = prophet.make_future_dataframe(periods=365)
0   2020-01-05
1   2020-01-06
2   2020-01-07
3   2020-01-08
4   2020-01-09
..         ...
727 2022-01-01
728 2022-01-02
729 2022-01-03
730 2022-01-04
731 2022-01-05

[732 rows x 1 columns]
forecast = prophet.predict(future)
forecast[["ds", "yhat", "yhat_lower", "yhat_upper"]].tail(200)
bitcoin price prediction

Now let’s plot our predictions:

from fbprophet.plot import plot
prophet.plot(forecast, figsize=(20, 10))
bitcoin price prediction using machine learning


There are several interpretations of the forecasts calculated by the Facebook prophet model like the current momentum for the bitcoin prices has sky-rocketed, and still, we are likely to see a very rapid increase in the prices of bitcoin.

I hope you liked this article on Bitcoin Price Prediction with Python using Machine Learning. Feel free to ask your valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1538

Leave a Reply