Bitcoin price prediction using machine learning in Python

Bitcoin is in its boom nowadays. So, it’s important to study cryto. In this tutorial, you will learn about the prediction of the price of bitcoin after 30 days using machine learning.

Bitcoin price prediction using machine learning

This tutorial involves the following:

  1. Loading packages
  2. Exploring dataset
  3. Data preprocessing
  4. Model training
  5. Prediction

Loading packages:

import pandas as pd
import numpy as np

Loading the data:
In this tutorial, we have used bitcoin.csv as the dataset

df = pd.read_csv("PATH OF THE DATASET")
df.head()
         Date            Price
0   07-02-2021 00:00  39323.26
1   08-02-2021 00:00  38928.10
2   09-02-2021 00:00  46364.30
3   10-02-2021 00:00  46589.58
4   11-02-2021 00:00  44878.17
df.shape
(366, 2)

Data Preprocessing:
We are dropping the date column as it’s not necessary for prediction.

df.drop(["Date"],1,inplace=True)
df.head()
      Price
0    39323.26
1    38928.10
2    46364.30
3    46589.58
4    44878.17

We need to create another column in which the price is shifted by 30 days. We can achieve this by the following:

days = 30
df["Predicted"] = df.shift(-1*days)
df.head()
   Price    Predicted
0 39323.26  52299.33
1 38928.10  54881.52
2 46364.30  55997.23
3 46589.58  57764.00
4 44878.17  57253.28

As the price is shifted by 30 days are stored in the Predicted column, the  column will be empty for the last 30 days as shown:

df.tail()
     Price    Predicted
361 36912.68    NaN
362 37092.40    NaN
363 41584.19    NaN
364 41435.43    NaN
365 42450.98    NaN

Data conversion for prediction:

X = np.array(df.drop(["Predicted"],1))
X = X[:len(df) - days]
print(X)
y = np.array(df["Predicted"])
y = y[:-days] 
print(y)

Data splitting:
Data is splitted into train data and test data with a factor of 0.2:

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
prediction_x_array = np.array(df.drop(["Predicted"],1))
prediction_x_array = prediction_x_array[-days:]
print(type(prediction_x_array))

Prediction:
Here we have used Support Vector Regression for prediction:

from sklearn.svm import SVR

svr = SVR(kernel = 'rbf', C = 1e3, gamma = 0.00001)  
svr.fit(X_train,y_train)
svr.score(X_test,y_test)
0.6398292876976701

We have achieved an accuracy of 63%.

predicted_price = svr.predict(prediction_x_array)
print(predicted_price)
[47197.26245446 47635.62536883 47578.70393876 50774.33949452 47886.95348751 49830.13489697 49222.00176253 48842.52596164 49196.64995432 48874.18382602 49150.19444518 47214.79127822 48831.13200552 48042.20686501 45703.32044236 47806.14077015 47698.82856553 46628.01287717 47178.26577315 45609.85029161 46152.56000551 45274.21884468 46427.31908589 45028.84066194 46540.49065046 46793.0362517 45909.55688879 47236.63237682 47613.97669264 49374.39596964]
X_price = np.array(df["Price"])[-days:]
df = pd.DataFrame({"Price":X_price, "Prediction":predicted_price})
df.head()
    Price   Prediction
0 41690.93 47197.262454
1 41864.44 47635.625369
2 41849.00 47578.703939
3 42723.37 50774.339495
4 43926.00 47886.953488

In the above, we predicted the price of the last 30 days in the dataset.

Prediction for user input:

print("Enter price:")
c = list(map(float,input().split()))
print("Predicted price after 30 days:",end = " ")
y_pred = svr.predict(np.array(c).reshape(-1, 1))
print(int(y_pred))
Enter price: 
40000 
Predicted price after 30 days: 48925

In this way, the model predicts a given input.

Thanks for reading!!

Leave a Reply

Your email address will not be published.