import numpy as np
import pandas as pd
import plotly.graph_objs as go
from pandas_datareader import data, wb
from datetime import date
import cufflinks as cf
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sklearn.metrics as metrics
import sklearn.metrics as sm
from sklearn.metrics import mean_squared_error
from sklearn.metrics import accuracy_score, confusion_matrix
import tensorflow as tf
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
import math
import numpy as np import pandas as pd import plotly.graph_objs as go from pandas_datareader import data, wb from datetime import date import cufflinks as cf import yfinance as yf import pandas as pd import numpy as np import matplotlib.pyplot as plt import sklearn.metrics as metrics import sklearn.metrics as sm from sklearn.metrics import mean_squared_error from sklearn.metrics import accuracy_score, confusion_matrix import tensorflow as tf from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from keras.layers import Dropout import math
In[2]:
start=pd.to_datetime('2013-01-01') end=pd.to_datetime('2018-01-01')
In[3]:
BITCOIN = data.DataReader('BTC-USD', 'yahoo', start, end)
In[4]:
BITCOIN_processed=BITCOIN.iloc[:, 3:4].values
In[5]:
scaler = MinMaxScaler(feature_range = (0, 1)) BITCOIN_training_scaled = scaler.fit_transform(BITCOIN_processed)
In[6]:
x_train = [] y_train = [] for i in range(60, 1204): x_train.append(BITCOIN_training_scaled[i-60:i, 0]) y_train.append(BITCOIN_training_scaled[i, 0]) x_train, y_train = np.array(x_train), np.array(y_train)
In[7]:
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
In[8]:
model = Sequential() model.add(LSTM(units=200, return_sequences=True, activation='relu',input_shape=(x_train.shape[1], 1))) model.add(Dropout(0.2))
model.add(LSTM(units=200, return_sequences=True)) model.add(Dropout(0.2))
model.add(LSTM(units=200, return_sequences=True)) model.add(Dropout(0.2))
model.add(LSTM(units=200, return_sequences=False)) model.add(Dropout(0.2))
In[9]:
model.add(Dense(units = 1)) model.compile(optimizer = 'adam', loss = 'mean_squared_error')
In[10]:
model.fit(x_train, y_train, epochs = 100, batch_size = 32)
In[11]:
start=pd.to_datetime('2018-01-01') end=pd.to_datetime('2018-01-8') BITCOIN2 = data.DataReader('BTC-USD', 'yahoo', start, end)
In[12]:
BITCOIN2_processed=BITCOIN2.iloc[:, 3:4].values
In[13]:
BITCOIN_total = pd.concat((BITCOIN['Close'], BITCOIN2['Close']), axis=0)
In[14]:
test_inputs = BITCOIN_total[len(BITCOIN_total) - len(BITCOIN2) - 60:].values
In[15]:
test_inputs = test_inputs.reshape(-1,1) test_inputs = scaler.transform(test_inputs)
In[16]:
test_features = [] for i in range(60, 68): test_features.append(test_inputs[i-60:i, 0])
In[17]:
test_features = np.array(test_features) test_features = np.reshape(test_features, (test_features.shape[0], test_features.shape[1], 1))
In[18]:
predictions = model.predict(test_features)
In[19]:
predictions = scaler.inverse_transform(predictions)
In[20]:
plt.figure(figsize=(10,6)) plt.plot(BITCOIN2_processed, color='blue', label='Actual Bitcoin Stock Price') plt.plot(predictions , color='red', label='Predicted Bitcoin Stock Price') plt.title('Bitcoin Stock Price Prediction') plt.xlabel('Date') plt.ylabel('Bitcoin Stock Price') plt.legend() plt.show()
In[23]:
print("Mean absolute error =", round(sm.mean_absolute_error(BITCOIN2_processed, predictions), 2)) print("Mean squared error =", round(sm.mean_squared_error(BITCOIN2_processed, predictions), 2)) print("Median absolute error =", round(sm.median_absolute_error(BITCOIN2_processed, predictions), 2)) print("Explain variance score =", round(sm.explained_variance_score(BITCOIN2_processed, predictions), 2)) print("R2 score =", round(sm.r2_score(BITCOIN2_processed, predictions), 2))
In[24]:
start=pd.to_datetime('2018-01-01') end=pd.to_datetime('2018-02-01') BITCOIN3 = data.DataReader('BTC-USD', 'yahoo', start, end)
In[25]:
BITCOIN3_processed=BITCOIN3.iloc[:, 3:4].values
In[26]:
BITCOIN_total = pd.concat((BITCOIN['Close'], BITCOIN3['Close']), axis=0)
In[27]:
test_inputs = BITCOIN_total[len(BITCOIN_total) - len(BITCOIN3) - 60:].values
In[28]:
test_inputs = test_inputs.reshape(-1,1) test_inputs = scaler.transform(test_inputs)
In[29]:
test_features = [] for i in range(60, 94): test_features.append(test_inputs[i-60:i, 0])
In[30]:
test_features = np.array(test_features) test_features = np.reshape(test_features, (test_features.shape[0], test_features.shape[1], 1))
In[31]:
predictions = model.predict(test_features)
In[32]:
predictions = scaler.inverse_transform(predictions)
In[33]:
plt.figure(figsize=(10,6)) plt.plot(BITCOIN3_processed, color='blue', label='Actual Bitcoin Stock Price') plt.plot(predictions , color='red', label='Predicted Bitcoin Stock Price') plt.title('Bitcoin Stock Price Prediction') plt.xlabel('Date') plt.ylabel('Bitcoin Stock Price') plt.legend() plt.show()
In[34]:
print("Mean absolute error =", round(sm.mean_absolute_error(BITCOIN3_processed, predictions), 2)) print("Mean squared error =", round(sm.mean_squared_error(BITCOIN3_processed, predictions), 2)) print("Median absolute error =", round(sm.median_absolute_error(BITCOIN3_processed, predictions), 2)) print("Explain variance score =", round(sm.explained_variance_score(BITCOIN3_processed, predictions), 2)) print("R2 score =", round(sm.r2_score(BITCOIN3_processed, predictions), 2))
In[ ]: