Bitcoin Price Prediction using LSTM | Deep-Learning Project #DeepLearning #Machine Learning #Python
Table of Contents
Introduction
In this tutorial, we will predict the price of Bitcoin using Long Short-Term Memory (LSTM) neural networks, a type of deep learning model. This project is ideal for those interested in machine learning and cryptocurrency forecasting. We'll utilize Google Colab or Kaggle Notebook to streamline the process, eliminating the need for setting up a virtual environment or installing TensorFlow locally.
Step 1: Setting Up Your Environment
-
Choose between Google Colab or Kaggle Notebook:
- Both platforms come pre-installed with TensorFlow, making it easy to get started.
- Create a new notebook in your chosen platform.
-
Import necessary libraries:
import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential from keras.layers import LSTM, Dense, Dropout
Step 2: Data Collection
-
Download historical Bitcoin data:
- Go to Yahoo Finance and download the historical price data as a CSV file.
- Alternatively, use the data directly in your code by fetching it from a URL.
-
Load the dataset:
dataset = pd.read_csv('path_to_your_file/BTC-USD.csv')
- Make sure to adjust the path to where your CSV file is stored.
Step 3: Data Preprocessing
-
Select relevant columns:
- Focus on the 'Close' price for prediction.
data = dataset['Close'].values.reshape(-1, 1)
-
Normalize the data:
- Use MinMaxScaler to scale data between 0 and 1.
scaler = MinMaxScaler(feature_range=(0, 1)) scaled_data = scaler.fit_transform(data)
-
Create training and testing datasets:
- Split the data into training and testing sets (e.g., 80% training, 20% testing).
training_data_len = int(np.ceil(len(scaled_data) * .8)) train_data = scaled_data[0:training_data_len, :]
-
Prepare the input and output sequences:
- Create sequences for LSTM input.
x_train, y_train = [], [] for i in range(60, len(train_data)): x_train.append(train_data[i-60:i, 0]) y_train.append(train_data[i, 0]) x_train, y_train = np.array(x_train), np.array(y_train)
Step 4: Building the LSTM Model
-
Reshape the input data:
- LSTM expects input in the format of (samples, time steps, features).
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
-
Create the LSTM model:
model = Sequential() model.add(LSTM(50, return_sequences=True, input_shape=(x_train.shape[1], 1))) model.add(Dropout(0.2)) model.add(LSTM(50, return_sequences=False)) model.add(Dropout(0.2)) model.add(Dense(25)) model.add(Dense(1)) # Output layer
-
Compile the model:
model.compile(optimizer='adam', loss='mean_squared_error')
Step 5: Training the Model
- Train the model:
model.fit(x_train, y_train, batch_size=1, epochs=1)
Step 6: Making Predictions
-
Prepare test data and make predictions:
- Repeat the preprocessing steps for the test dataset.
- Predict and inverse transform the predictions.
predictions = model.predict(x_test) predictions = scaler.inverse_transform(predictions)
-
Visualize the results:
- Plot the predictions against actual prices.
plt.plot(actual_prices, label='Actual Price') plt.plot(predictions, label='Predicted Price') plt.legend() plt.show()
Conclusion
In this tutorial, we covered the essential steps to predict Bitcoin prices using an LSTM model in TensorFlow. You can further enhance your model by experimenting with different parameters, adding more layers, or increasing the dataset size. To explore variations and improvements, refer to the project's code on Kaggle or GitHub. Happy learning!