Bitcoin Prise Using LSTM.ipynb - Colab
Bitcoin Prise Using LSTM.ipynb - Colab
Bitcoin Prise Using LSTM.ipynb - Colab
Long short-term memory is an arti cial recurrent neural network architecture used in the eld of deep
learning. Unlike standard feedforward neural networks, LSTM has feedback connections. It can process
not only single data points, but also entire sequences of data.
Long Short-Term Memory (LSTM) networks are a type of recurrent neural network capable of learning
order dependence in sequence prediction problems. This is a behavior required in complex problem
domains like machine translation, speech recognition, and more. LSTMs are a complex area of deep
learning.
LSTMs are often referred to as fancy RNNs. Vanilla RNNs do not have a cell state. They only have
hidden states and those hidden states serve as the memory for RNNs. Meanwhile, LSTM has both cell
states and a hidden states.
2. Importing Library
import matplotlib.pyplot
as plt from itertools
import cycle import
plotly.graph_objects as go
import plotly.express as
px
from plotly.subplots import make_subplots
3. Loading Dataset
maindf=pd.read_csv('/content/BTC-USD.csv')
maindf.shape
(2713, 7)
maindf.head()
Date Open High Low Close Adj CloseVolume
0 2014-09-17 465.864014 468.174011 452.421997 457.334015 457.334015 21056800
1 2014-09-18 456.859985 456.859985 413.104004 424.440002 424.440002 34483200
2 2014-09-19 424.102997 427.834991 384.532013 394.795990 394.795990 37919700
3 2014-09-20 394.673004 423.295990 389.882996 408.903992 408.903992 36863600
4 2014-09-21 408.084991 412.425995 393.181000 398.821014 398.821014 26580100
maindf.tail()
Date Open High Low Close Adj Close Volume
maindf.describe()
Open High Low Close Adj Close Volume
mean11311.04106911614.29248210975.55505711323.91463711323.9146371.470462e+10
std 16106.42889116537.39064 15608.57256016110.36501016110.3650102.001627e+10
9
min 176.897003 211.731003 171.509995 178.102997 178.102997 5.914570e+06
25% 606.396973 609.260986 604.109985 606.718994 606.718994 7.991080e+07
50% 6301.569824 6434.617676 6214.220215 6317.609863 6317.609863 5.098183e+09
75% 10452.39941410762.64453 10202.38769510462.25976 10462.2597662.456992e+10
1 6
max 67549.73437568789.62500 66382.06250067566.82812 67566.8281253.509679e+11
0 5
Checking for Null Values
print('Null Values:',maindf.isnull().values.sum())
Null Values: 0
print('NA values:',maindf.isnull().values.any())
NA values: False
maindf.shape
(2713, 7)
4. EDA(Exploratory Data Analysis)bold text
sd=maindf.iloc[0][0]
ed=maindf.iloc[-1][0]
print('Starting Date',sd)
print('Ending Date',ed)
Starting Date 2014-09-17
Ending Date 2022-02-19 <ipython-input-104-d7c77f12f3f6>:3: FutureWarning: Series.__getitem__
treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels
Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be
treated as labels (con
monthvise= y_2014.groupby(y_2014['Date'].dt.strftime('%B'))
[['Open','Close']].mean()
new_order = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
'August',
'September', 'October', 'November', 'December']
monthvise = monthvise.reindex(new_order, axis=0)
monthvise
Open Close
Date
Marc Na NaN
h N
April Na NaN
N
May Na NaN
N
June Na NaN
N
July Na NaN
N
Augus Na NaN
t N
Septem 412.654 407.182
ber 003 428
Octob 365.748 364.148
er 000 873
Novem 364.850 366.099
ber 235 799
Decem 344.146 341.970
ber 864 366
fig = go.Figure()
fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Open'],
marker_color='crimson'
))
fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
marker_color='lightsalmon'
))
fig.update_layout(barmode='group', xaxis_tickangle=-45,
fig.show()
350
300
250
200
150
100
50
January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov
Note that we only have few months in 2014 so the rest of the months are not plotted
since we do not have the data
y_2014.groupby(y_2014['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2014.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)
monthvise_low = y_2014.groupby(y_2014['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))
fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()
400
300
200
100
0
January
February
MarchApril Ma June July August
September
October
No December
y vember
Note that we only have few months in 2014 so the rest of the months are not plotted
since we do not have the data
names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])
360
340
320
300
280
Oct 2014 Nov 2014 Dec 2014
Date
Note that we only have few months in 2014 so the rest of the months are not plotted
since we do not have the data
monthvise= y_2015.groupby(y_2015['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close
Date
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))
fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()
350
300
250
200
150
100
50
January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov
y_2015.groupby(y_2015['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2015.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)
monthvise_low = y_2015.groupby(y_2015['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))
fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()
400
300
200
100
0
January
February
MarchApril Ma June July August
September
October
No December
y vember
names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])
300
250
200
monthvise= y_2016.groupby(y_2016['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close
Date
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))
fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()
700
600
500
400
300
200
100
January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov
y_2016.groupby(y_2016['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2016.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)
monthvise_low = y_2016.groupby(y_2016['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))
fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()
800
600
400
200
0
January
February
MarchApril Ma June July August
September
October
No December
y vember
names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])
Stock Open
Price
900Stock Close Price
Stock High Price
Stock Low
Price
800
700
600
500
400
Date
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))
fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()
12k
10k
8k
6k
4k
2k
January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov
y_2017.groupby(y_2017['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2017.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)
monthvise_low = y_2017.groupby(y_2017['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))
fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()
Monthwise High and Low stock price
15k
10k
5k
0
January
February
MarchApril Ma June July August
September
October
No December
y vember
names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])
5k
0
Jan 2017 Apr 2017 Jul 2017 Oct 2017
Date
monthvise= y_2018.groupby(y_2018['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close
Date
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))
fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()
10k
8k
6k
4k
2k
January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov
y_2018.groupby(y_2018['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2018.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)
monthvise_low = y_2018.groupby(y_2018['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))
fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()
14k
12k
10k
8k
6k
4k
2k
0
January
February
MarchApril Ma June July August
September
October
No December
y vember
names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])
10k
8k
6k
4k
Date
monthvise= y_2019.groupby(y_2019['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close
Date
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))
fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()
8k
6k
4k
2k
January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov
y_2019.groupby(y_2019['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2019.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)
monthvise_low = y_2019.groupby(y_2019['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))
fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()
12k
10k
8k
6k
4k
2k
0
January
February
MarchApril Ma June July August
September
October
No December
y vember
names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])
8k
6k
4k
Date
Analysis of Year 2020
monthvise= y_2020.groupby(y_2020['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close
Date
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))
fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()
Monthwise comparision between Stock open and close price
15k
10k
5k
January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov
y_2020.groupby(y_2020['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2020.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)
monthvise_low = y_2020.groupby(y_2020['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))
fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()
Monthwise High and Low stock price
30k
Stock high Price
Stock low Price
25k
20k
15k
10k
5k
0
January
February
MarchApril Ma June July August
September
October
No December
y vember
names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])
30k
Stock Parameters
Stock Open Price
Stock Close Price
25k Stock High Price
Stock Low Price
alue
20k
Stock v
15k
10k
5k
Date
monthvise= y_2021.groupby(y_2021['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close
Date
Since we had data till 24-08-2021 in Months after August its showing NaN
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))
fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()
Monthwise comparision between Stock open and close price
50k
40k
30k
20k
10k
January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov
y_2021.groupby(y_2021['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2021.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)
monthvise_low = y_2021.groupby(y_2021['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))
fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()
Monthwise High and Low stock price
60k
50k
40k
30k
20k
10k
0
January
February
MarchApril Ma June July August
September
October
No December
y vember
names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])
70k
Stock Parameters
Stock Open Price
65k
Stock Close Price
Stock High Price
60k
Stock Low Price
alue55k
Stock v
50k
45k
40k
35k
30k
Date
Open Close
Date
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise.index,
y=monthvise['Open'],
name='Stock Open Price',
marker_color='crimson'
)) fig.add_trace(go.Bar(
x=monthvise.index,
y=monthvise['Close'],
name='Stock Close
Price',
marker_color='lightsal
mon'
))
fig.update_layout(barmode='group', xaxis_tickangle=-45,
title='Monthwise comparision between Stock open and close
price') fig.show()
Monthwise comparision between Stock open and close price
35k
30k
25k
20k
15k
10k
5k
January
0 ebruaryMarch April September
October December
June July ugust ember
May
A
F Nov
y_2022.groupby(y_2022['Date'].dt.strftime('%B'))['Low'].min()
monthvise_high =
y_2022.groupby(maindf['Date'].dt.strftime('%B'))
['High'].max() monthvise_high =
monthvise_high.reindex(new_order, axis=0)
monthvise_low = y_2022.groupby(y_2022['Date'].dt.strftime('%B'))
['Low'].min() monthvise_low = monthvise_low.reindex(new_order,
axis=0)
fig = go.Figure()
fig.add_trace(go.Bar( x=
monthvise_high.index,
y=monthvise_high,
name='Stock high Price',
marker_color='rgb(0, 153,
204)'
))
fig.add_trace(go.Bar( x=
monthvise_low.index,
y=monthvise_low,
name='Stock low Price',
marker_color='rgb(255,
128, 0)'
))
fig.update_layout(barmode='group',
title=' Monthwise High and Low stock price')
fig.show()
Monthwise High and Low stock price
50k
Stock high Price
Stock low Price
40k
30k
20k
10k
0
January
February
MarchApril Ma June July August
September
October
No December
y vember
names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])
40k
38k
36k
34k
Date
monthvise= y_overall.groupby(y_overall['Date'].dt.strftime('%B'))
[['Open','Close']].mean() new_order = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November',
'December'] monthvise =
monthvise.reindex(new_order, axis=0)
monthvise
Open Close
Date
names = cycle(['Stock Open Price','Stock Close Price','Stock High Price','Stock Low Price'])
30k
20k
10k
0
2016 2018 2020 2022
Date
60k
50k
Close
40k
30k
20k
10k
0
2016 2018 2020 2022
Date
Date Close
65k
60k
55k
Close
50k
45k
40k
35k
30k
Mar 2021 May 2021 Jul 2021 Sep 2021 Nov 2021 Jan 2022
Date
MinMaxScaler. For each value in a feature, MinMaxScaler subtracts the minimum value in the feature
and then divides by the range. The range is the difference between the original maximum and original
minimum. MinMaxScaler preserves the shape of the original distribution.
# deleting date column and normalizing using MinMax Scaler
del closedf['Date']
scaler=MinMaxScaler(feature_range=(0,1))
closedf=scaler.fit_transform(np.array(closedf).re
shape(-1,1)) print(closedf.shape)
(365, 1)
training_size=int(len(closedf)*0.60) test_size=len(closedf)-
training_size
train_data,test_data=closedf[0:training_size,:],closedf[training_size:l
en(closedf),:1] print("train_data: ", train_data.shape)
print("test_data: ", test_data.shape)
train_data: (219, 1)
test_data: (146, 1)
Now we Transform the Close price based on Time-series-analysis forecasting requirement , Here we
will take 15
# convert an array of values into a dataset matrix
time_step = 15
X_train, y_train = create_dataset(train_data,
time_step) X_test, y_test =
create_dataset(test_data, time_step)
# reshape input to be [samples, time steps, features] which is required for LSTM
X_train
=X_train.reshape(X_train.shape[0],X_train.shape[1] ,
1) X_test =
X_test.reshape(X_test.shape[0],X_test.shape[1] , 1)
model=Sequential()
model.add(LSTM(10,input_shape=(None,1),activation="rel
u")) model.add(Dense(1))
model.compile(loss="mean_squared_error",optimizer="ada
m")
/usr/local/lib/python3.10/dist-packages/keras/src/layers/rnn/rnn.py:204: UserWarning:
Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer
using an `Input(shape)` object as
history =
model.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=200,batch_size=32,verbose=1)