Created
April 11, 2022 15:46
-
-
Save computerphysicslab/fac75430da8d4bfce4659d4037bdbc9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
from datetime import datetime | |
from autots import AutoTS | |
data = pd.read_csv('BitcoinHistoricalData.csv') | |
print("Shape of Dataset is: ", data.shape, "\n") | |
print(data.head()) | |
# Convert DataFrame column type from string to datetime | |
data['Date'] = pd.to_datetime(data['Date']) | |
# Sort by date column | |
data = data.sort_values('Date').reset_index(drop=True) | |
# Select the column "Price" for daily price | |
# Price strings have commas as thousands separators so you will have to remove them | |
# before the call to float | |
data['Price'] = (data['Price'].str.split()).apply(lambda x: float(x[0].replace(',', ''))) | |
data['Price'] = data['Price'].astype(float) | |
# Soften data w/ a moving average on price | |
movingAvgWindow = 30 | |
data['Price'] = data['Price'].rolling(window=movingAvgWindow).mean() | |
data = data[movingAvgWindow:] | |
model = AutoTS(forecast_length=120, frequency='infer', ensemble='simple', drop_data_older_than_periods=3000) | |
model = model.fit(data, date_col='Date', value_col='Price', id_col=None) | |
prediction = model.predict() | |
forecast = prediction.forecast | |
# Save, in case you need to store results | |
# forecast.to_csv('forecast.csv', index=True) | |
# Load stored forecast, in case you need to load previous results | |
# forecast = pd.read_csv('forecast.csv') | |
# Add column headers | |
forecast.columns = ['Date', 'Price'] | |
# Convert DataFrame column type from string to datetime | |
forecast['Date'] = pd.to_datetime(forecast['Date']) | |
print("Bitcoin Price Prediction") | |
print(forecast) | |
# Draw it | |
plt.figure(figsize=(24,10)) | |
plt.plot(data['Date'].values, data['Price'].values, label = 'Real Bitcoin Price', color = 'red') | |
plt.plot(forecast['Date'].values, forecast['Price'].values, label = 'Predicted Bitcoin Price', color = 'blue') | |
plt.xlabel('Date') | |
plt.ylabel('Price ($)') | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment