-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstockprice-prediction-LSTM.py
248 lines (193 loc) · 7.08 KB
/
stockprice-prediction-LSTM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# -*- coding: utf-8 -*-
"""StockPricePredictionLSTM_NadyaNovalina.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1-U9H46q70oEfGjek-0c0Bykft_FkAwl1
# Introduction
# Import Library
"""
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, LSTM, Dropout
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.metrics import mean_absolute_error, mean_squared_error, mean_absolute_percentage_error
"""# Import and Visulize Dataset"""
# Read dataset
df = pd.read_csv('/kaggle/input/stockprice/UNVR.JK.csv')
df.head()
# Set Date as index
df['Date'] = pd.to_datetime(df['Date'])
df = df.set_index('Date')
# Check dataframe shape
df.shape
# Check dataframe info
df.info()
# Check missing values
df.isnull().sum()
"""## Dataset Visualization
### High and Low
"""
sns.set_style('darkgrid')
plt.figure(figsize=(15,7))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=12))
x_dates = df.index.values
plt.plot(x_dates, df['High'], label='High')
plt.plot(x_dates, df['Low'], label='Low')
plt.xlabel('Date')
plt.ylabel('Price (Rp)')
plt.title("Stock Price\nUnilever Indonesia", fontsize=20)
plt.legend()
plt.gcf().autofmt_xdate()
plt.show()
"""### Open and Close"""
plt.figure(figsize=(15,7))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=12))
plt.plot(x_dates, df['Open'], label='Open')
plt.plot(x_dates, df['Close'], label='Close')
plt.xlabel('Date')
plt.ylabel('Price (Rp)')
plt.title("Stock Price\nUnilever Indonesia", fontsize=20)
plt.legend()
plt.gcf().autofmt_xdate()
plt.show()
"""### Close and Adj Close"""
plt.figure(figsize=(15,7))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=12))
plt.plot(x_dates, df['Close'], label='Close')
plt.plot(x_dates, df['Adj Close'], label='Adj Close')
plt.xlabel('Date')
plt.ylabel('Price (Rp)')
plt.title("Stock Price\nUnilever Indonesia", fontsize=20)
plt.legend()
plt.gcf().autofmt_xdate()
plt.show()
plt.figure(figsize=(15,7))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=12))
plt.plot(x_dates, df['Close'], label='Close')
plt.xlabel('Date')
plt.ylabel('Price (Rp)')
plt.title("Stock Price\nUnilever Indonesia", fontsize=20)
plt.legend()
plt.gcf().autofmt_xdate()
plt.show()
"""# Data Preprocessing
## Feature Scaling
"""
#MinMaxScaler
ms = MinMaxScaler()
df['Close_ms'] = ms.fit_transform(df[['Close']])
"""## Split Data"""
def split_data(df, train_size):
size = int(len(df) * train_size)
train, test = df.iloc[0:size], df.iloc[size:len(df)]
return train, test
train, test = split_data(df['Close_ms'], 0.8) #80% Train
plt.figure(figsize=(15,7))
plt.plot(train)
plt.plot(test)
plt.legend(labels=['Training', 'Testing'])
plt.title('Stock Price\nUnilever Indonesia\n80% training & 20% Testing', fontsize=20)
plt.show()
def split_target(df, look_back=1):
X, y = [], []
for i in range(len(df) - look_back):
a = df[i:(i + look_back), 0]
X.append(a)
y.append(df[i + look_back, 0])
return np.array(X), np.array(y)
X_train, y_train = split_target(train.values.reshape(len(train), 1))
X_test, y_test = split_target(test.values.reshape(len(test), 1))
X_train = X_train.reshape((X_train.shape[0], 1, X_train.shape[1]))
X_test = X_test.reshape((X_test.shape[0], 1, X_test.shape[1]))
"""# Build Model"""
# Callbacks
class Callback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if logs.get('val_mae') is not None and logs.get('val_mae') < 0.01:
self.model.stop_training = True
model = Sequential([LSTM(128, input_shape=(1, 1), return_sequences=True),
Dropout(0.2),
LSTM(64),
Dropout(0.2),
Dense(32, activation='relu'),
Dense(1)])
model.summary()
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001)
model.compile(optimizer=optimizer,
metrics=["mae"],
loss=tf.keras.losses.Huber())
"""# Training"""
history = model.fit(X_train,
y_train,
epochs=200,
batch_size=32,
validation_data=(X_test, y_test),
shuffle=False,
callbacks=[Callback()])
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15,5))
# Loss
ax1.plot(history.history['loss'])
ax1.plot(history.history['val_loss'])
ax1.legend(['Loss','Val Loss'])
ax1.set_xlabel('Epoch', fontsize=12)
ax1.set_ylabel('Loss', fontsize=12)
ax1.set_title('Loss', fontsize=20)
# MAE
ax2.plot(history.history['mae'])
ax2.plot(history.history['val_mae'])
ax2.legend(['mae','Val mae'])
ax2.set_xlabel('Epoch', fontsize=12)
ax2.set_ylabel('Mean Absolute Error', fontsize=12)
ax2.set_title('Mean Absolute Error', fontsize=20)
plt.show()
"""# Predict"""
# Perform prediction on the test data
pred = model.predict(X_test)
y_pred = np.array(pred).reshape(-1)
# Plot the actual and predicted prices
plt.figure(figsize=(15, 7))
plt.plot(test.index[:-1], y_test, color='blue', label='Actual') # Exclude the last element to match the length
plt.plot(test.index[:-1], y_pred, color='red', label='Predicted') # Exclude the last element to match the length
plt.text(test.index[300], 0.45, f"MAE = {mean_absolute_error(y_test, y_pred)}", style='italic', bbox={
'facecolor': 'orange', 'alpha': 0.5, 'pad': 10})
plt.xlabel('Date')
plt.ylabel('Stock Price')
plt.title('Stock Price Prediction\nUnilever Indonesia\nLSTM', fontsize=20)
plt.gca().xaxis.set_major_locator(mdates.AutoDateLocator())
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.gcf().autofmt_xdate()
plt.legend()
plt.show()
"""## Evaluation"""
mae = mean_absolute_error(y_test, y_pred)
rmse = mean_squared_error(y_test, y_pred, squared=False)
mape = mean_absolute_percentage_error(y_test, y_pred)
print('MAE: ', mae)
print('RSME: ', rmse)
print('MAPE: ', mape)
"""# Plot all data with predicted result"""
# Inverse transform the predicted values
y_pred_original = ms.inverse_transform(np.array(y_pred).reshape(-1, 1))
# Plot the actual and predicted prices
plt.figure(figsize=(15, 7))
plt.plot(df.index, df['Close'], color='blue', label='Actual')
plt.plot(df.index[:-1], [None] * len(train.index) + list(y_pred_original), color='red', label='Predicted')
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.title('Stock Price Prediction\nUnilever Indonesia\nLSTM', fontsize=20)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=12))
# Rotate x-axis labels
plt.xticks(rotation=30)
plt.legend()
plt.show()