<jupyter_start><jupyter_text>Data collection<jupyter_code>from google.colab import files

uploaded = files.upload()
# start = dt.datetime(2013,9,17)
# end = dt.datetime.now()

# data_alt = yf.download('BTC-USD', start, end)
# data_alt.head()
# data_alt.to_csv('bitcoin-2014-09-to-2021-12-20.csv')
df = pd.read_csv('/content/bitcoin-2014-09-to-2021-12-20.csv')
df.head()
df.tail()
df.dtypes
df1 = df.reset_index()['Close']

df1
plt.figure(figsize=(15,12))
plt.plot(df1)
scaler = MinMaxScaler(feature_range=(0, 1))
df1 = scaler.fit_transform(np.array(df1).reshape(-1, 1))
print(df1)
# save the scaler
import pickle
pickle.dump(scaler, open('scaler-BTC.pkl', 'wb'))
# Spitting dataset into train and test split

training_size = int(len(df1)*0.80)

test_size = len(df1) - training_size
train_data, test_data = df1[0:training_size, : ], df1[training_size: len(df1), : 1]
len(train_data), len(test_data)
def create_dataset(dataset, time_step=1):
    dataX, dataY = [], []
    for i in range(len(dataset)-time_step-1):
        a = dataset[i: (i+time_step), 0]
        dataX.append(a)
        dataY.append(dataset[i + time_step, 0])

    return np.array(dataX), np.array(dataY)
time_step = 15
X_train, y_train = create_dataset(train_data, time_step)
print(X_train.shape), print(y_train.shape)
X_test, y_test = create_dataset(test_data, time_step)
print(X_test.shape), print(y_test.shape)
# reshape the inputs to be [sample, time_steps, features] which is required for LSTM
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)
X_train.shape
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)
X_test.shape
model_0 = Sequential()
model_0.add(LSTM(50, return_sequences=True, input_shape=(time_step,1)))
model_0.add(LSTM(50, return_sequences=True))
model_0.add(LSTM(50))
model_0.add(Dense(1))


model_0.compile(optimizer='adam', loss='mean_squared_error')
model_0.summary()
history = model_0.fit(X_train, y_train, epochs=100, batch_size=64, validation_split=0.1)
model_0.save('BTC-lstm.h5')
train_predict = model_0.predict(X_train)
test_predict = model_0.predict(X_test)
train_predict = scaler.inverse_transform(train_predict)
test_predict = scaler.inverse_transform(test_predict)
from sklearn.metrics import mean_squared_error
math.sqrt(mean_squared_error(y_train, train_predict))
math.sqrt(mean_squared_error(y_test, test_predict))
# !pip install mpld3
import mpld3
from mpld3 import plugins
%matplotlib notebook
# shift train predictions for plotting
look_back = 15
plt.figure(figsize=(20,18))
trainPredictPlot = np.empty_like(df1)
trainPredictPlot[:, :] = np.nan
trainPredictPlot[look_back: len(train_predict) + look_back, :] = train_predict
# shift test predictions for plotting
testPredictPlot = np.empty_like(df1)
testPredictPlot[:, :] = np.nan
testPredictPlot[len(train_predict)+(look_back*2)+1:len(df1)-1, :] = test_predict
# plot baseline and predictions
fig, ax = plt.subplots(figsize=(15,12))
l1 = ax.plot(scaler.inverse_transform(df1))
l2 = ax.plot(trainPredictPlot)
l3 = ax.plot(testPredictPlot)

line_collections = [l1, l2, l3]
legends = ['Real', 'TrainPredict', 'TestPredict']
plugins.connect(fig, plugins.InteractiveLegendPlugin(line_collections, legends))

mpld3.display()
# data for prediction
pred_input = np.array(df['Close'].iloc[-16:-1])
pred_input_shaped = pred_input.reshape(-1, 1)
pred_input_shaped.shape
loaded_scaler = pickle.load(open('/content/scaler-BTC.pkl', 'rb'))
transformed_input = loaded_scaler.transform(pred_input_shaped)
transformed_input.shape
x_input = transformed_input.reshape(1, transformed_input.shape[0], transformed_input.shape[1])
x_input.shape
# Prediction for today day
yhat = model_0.predict(x_input)
print(loaded_scaler.inverse_transform(yhat))
df['Close'].iloc[-1]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14373170082512643, 'fraction_numerical': 0.03593292520628161, 'mean_word_length': 6.945031712473573, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '555812', 'license': 'MIT', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1422, 'n_words': 245, 'path': 'backend/notebooks/Nighteye.ipynb', 'repo_name': 'rohankmr414/nighteye', 'size': '3757'}