Skip to content

Instantly share code, notes, and snippets.

View marcosan93's full-sized avatar

Marco Santos marcosan93

View GitHub Profile
# Splitting 80/20
index = round(len(bc)*.80)
train = bc_log.iloc[:index]
test = bc_log.iloc[index:]
# Fitting the model to the training set
model = SARIMAX(train,
order=(1, 0, 0),
seasonal_order=(0,0,0,0),
freq='D',
# Loading in the data from before
with open("curr_bitcoin.pickle",'rb') as fp:
ts = pickle.load(fp)
# Resetting the index back so Dates are no longer indexed
ts.reset_index(inplace=True)
# Renaming the columns for use in FB prophet
ts.rename(columns={'Date': 'ds', 'Close': 'y'}, inplace=True)
# Library Imports
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
plt.style.use("ggplot")
from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout
def visualize_training_results(results):
"""
Plots the loss and accuracy for the training and testing data
"""
history = results.history
plt.figure(figsize=(12,4))
plt.plot(history['val_loss'])
plt.plot(history['loss'])
plt.legend(['val_loss', 'loss'])
plt.title('Loss')
# How many periods looking back to train
n_per_in = 30
# How many periods ahead to predict
n_per_out = 10
# Features (in this case it's 1 because there is only one feature: price)
n_features = 1
# Splitting the data into appropriate sequences
# Instantiating the model
model = Sequential()
# Activation
activ = "softsign"
# Input layer
model.add(LSTM(30, activation=activ, return_sequences=True, input_shape=(n_per_in, n_features)))
# Hidden layers
plt.figure(figsize=(12,5))
# Getting predictions by predicting from the last available X variable
yhat = model.predict(X[-1].reshape(1, n_per_in, n_features)).tolist()[0]
# Transforming values back to their normal prices
yhat = scaler.inverse_transform(np.array(yhat).reshape(-1,1)).tolist()
# Getting the actual values from the last available y variable which correspond to its respective X variable
actual = scaler.inverse_transform(y[-1].reshape(-1,1))
# Predicting off of y because it contains the most recent dates
yhat = model.predict(np.array(df.tail(n_per_in)).reshape(1, n_per_in, n_features)).tolist()[0]
# Transforming the predicted values back to their original prices
yhat = scaler.inverse_transform(np.array(yhat).reshape(-1,1)).tolist()
# Creating a DF of the predicted prices
preds = pd.DataFrame(yhat, index=pd.date_range(start=df.index[-1], periods=len(yhat), freq="D"), columns=df.columns)
# Printing the predicted prices
# Pandas in order to view and format the data
import pandas as pd
# Pandas option so we can see all rows and columns
pd.set_option('display.max_rows', 200)
pd.set_option('display.max_columns', 70)
# Numpy for necessary calculations
import numpy as np
def setting_index(df):
"""
Returns a sorted datetime index
"""
df['Quarter end'] = pd.to_datetime(df['Quarter end'])
df.set_index("Quarter end", inplace=True)
return df.sort_index(ascending=True)
def class_creation(df, thres=3):
"""