-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstock_prediction_using_machine_learning.py
More file actions
314 lines (247 loc) · 11.1 KB
/
stock_prediction_using_machine_learning.py
File metadata and controls
314 lines (247 loc) · 11.1 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# -*- coding: utf-8 -*-
"""Stock Prediction using Machine Learning.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1bAB3yMlf2kQoV6SNnkNyYTbCMLkeOQJ-
"""
import pandas as pd
# Load the stock data
file_path = r'C:\Users\numan.yaqoob\Desktop\UP\finance ML Articles\AAPL_short_volume.csv'
data = pd.read_csv(file_path)
close_prices_AAPL = data['Close']
# Reverse the order of the data
close_prices_AAPL_reverse = close_prices_AAPL.iloc[::-1]
# Reset index to maintain the correct time series order in the plot
close_prices_AAPL_reverse.reset_index(drop=True, inplace=True)
# Plot the line chart
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6))
plt.plot(close_prices_AAPL_reverse)
plt.xlabel('Time')
plt.ylabel('Close Prices')
plt.title('AAPL Stock Close Prices')
plt.grid(True)
plt.show()
# Data preprocessing
data = close_prices_AAPL_reverse.values.reshape(-1, 1) # Reshape the data
data_normalized = data / np.max(data) # Normalize the data
# Split the data into training and testing sets
train_size = int(len(data_normalized) * 0.8)
train_data = data_normalized[:train_size]
test_data = data_normalized[train_size:]
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
from tensorflow.keras.optimizers import Adam
# Function to create LSTM model
def create_lstm_model(units, activation, learning_rate):
model = Sequential()
model.add(LSTM(units=units, activation=activation, input_shape=(1, 1)))
model.add(Dense(units=1))
optimizer = Adam(learning_rate=learning_rate)
model.compile(optimizer=optimizer, loss='mean_squared_error')
return model
# Define hyperparameters for tuning
lstm_units = [50, 100, 200]
lstm_activations = ['relu', 'tanh']
learning_rates = [0.001, 0.01, 0.1]
epochs = 100
batch_size = 32
# Perform hyperparameter tuning for LSTM model
best_rmse = float('inf')
best_lstm_model = None
from sklearn.metrics import mean_squared_error
for units in lstm_units:
for activation in lstm_activations:
for learning_rate in learning_rates:
# Create and train LSTM model
model = create_lstm_model(units=units, activation=activation, learning_rate=learning_rate)
model.fit(train_data[:-1].reshape(-1, 1, 1), train_data[1:], epochs=epochs, batch_size=batch_size, verbose=0)
# Predict on test data
test_predictions = model.predict(test_data[:-1].reshape(-1, 1, 1)).flatten()
# Calculate RMSE
rmse = np.sqrt(mean_squared_error(test_data[1:], test_predictions))
# Check if current model has lower RMSE
if rmse < best_rmse:
best_rmse = rmse
best_lstm_model = model
# Predict on the entire dataset using the best LSTM model
all_lstm_predictions = best_lstm_model.predict(data_normalized[:-1].reshape(-1, 1, 1)).flatten()
# Inverse normalize the LSTM predictions
all_lstm_predictions = all_lstm_predictions * np.max(data)
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV
# Support Vector Machines (SVM) Model
svm_model = SVR()
svm_params = {
'C': [0.1, 1, 10],
'gamma': [0.01, 0.1, 1]
}
svm_grid_search = GridSearchCV(svm_model, svm_params, scoring='neg_mean_squared_error')
svm_grid_search.fit(np.arange(len(close_prices_AAPL_reverse)).reshape(-1, 1), close_prices_AAPL_reverse)
svm_best_model = svm_grid_search.best_estimator_
svm_predictions = svm_best_model.predict(np.arange(len(close_prices_AAPL_reverse)).reshape(-1, 1))
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
# Random Forest Model
rf_model = RandomForestRegressor()
rf_params = {
'n_estimators': [50, 100, 200],
'max_depth': [None, 5, 10]
}
rf_grid_search = GridSearchCV(rf_model, rf_params, scoring='neg_mean_squared_error')
rf_grid_search.fit(np.arange(len(close_prices_AAPL_reverse)).reshape(-1, 1), close_prices_AAPL_reverse)
rf_best_model = rf_grid_search.best_estimator_
rf_predictions = rf_best_model.predict(np.arange(len(close_prices_AAPL_reverse)).reshape(-1, 1))
# Gradient Boosting Methods (XGBoost)
xgb_model = XGBRegressor()
xgb_params = {
'learning_rate': [0.1, 0.01, 0.001],
'max_depth': [3, 5, 7]
}
xgb_grid_search = GridSearchCV(xgb_model, xgb_params, scoring='neg_mean_squared_error')
xgb_grid_search.fit(np.arange(len(close_prices_AAPL_reverse)).reshape(-1, 1), close_prices_AAPL_reverse)
xgb_best_model = xgb_grid_search.best_estimator_
xgb_predictions = xgb_best_model.predict(np.arange(len(close_prices_AAPL_reverse)).reshape(-1, 1))
# Gradient Boosting Methods (LightGBM)
lgbm_model = LGBMRegressor()
lgbm_params = {
'learning_rate': [0.1, 0.01, 0.001],
'max_depth': [3, 5, 7]
}
lgbm_grid_search = GridSearchCV(lgbm_model, lgbm_params, scoring='neg_mean_squared_error')
lgbm_grid_search.fit(np.arange(len(close_prices_AAPL_reverse)).reshape(-1, 1), close_prices_AAPL_reverse)
lgbm_best_model = lgbm_grid_search.best_estimator_
lgbm_predictions = lgbm_best_model.predict(np.arange(len(close_prices_AAPL_reverse)).reshape(-1, 1))
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error
# Function to calculate RMSE
def rmse(y_true, y_pred):
return np.sqrt(mean_squared_error(y_true, y_pred))
# List of model names and predictions
model_names = ['Support Vector Machines (SVM)', 'Random Forest', 'XGBoost', 'LightGBM']
predictions = [svm_predictions, rf_predictions, xgb_predictions, lgbm_predictions]
best_models = [svm_best_model, rf_best_model, xgb_best_model, lgbm_best_model]
# Truncate actual values to match the length of predictions
actual_values_reverse = close_prices_AAPL_reverse[-len(svm_predictions):]
# Evaluate models and plot graphs
for i, model_name in enumerate(model_names):
model_prediction = predictions[i]
model_prediction_truncated = model_prediction[-len(actual_values_reverse):] # Truncate predicted values
model_rmse = rmse(actual_values_reverse, model_prediction_truncated)
# Plotting actual and predicted values
plt.figure(figsize=(10, 6))
plt.plot(actual_values_reverse, label='Actual')
plt.plot(model_prediction_truncated, label='Predicted')
plt.title(f"{model_name} - RMSE: {model_rmse:.2f}")
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()
plt.show()
# Print the best hyperparameters for the model
best_model = best_models[i]
print(f"Best Hyperparameters for {model_name}:")
print(best_model)
print("-----------------------------")
# Plotting LSTM predictions
plt.figure(figsize=(10, 6))
plt.plot(actual_values_reverse, label='Actual')
plt.plot(all_lstm_predictions, label='LSTM Predicted')
plt.title(f"LSTM Model - RMSE: {best_rmse:.2f}")
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()
plt.show()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVR
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
from tensorflow.keras.optimizers import Adam
# Load the stock data
file_path = r'C:\Users\numan.yaqoob\Desktop\UP\finance ML Articles\AAPL_short_volume.csv'
data = pd.read_csv(file_path)
close_prices_AAPL = data['Close']
# Reverse the order of the data
close_prices_AAPL_reverse = close_prices_AAPL.iloc[::-1]
# Reset index to maintain the correct time series order in the plot
close_prices_AAPL_reverse.reset_index(drop=True, inplace=True)
# Data preprocessing
data = close_prices_AAPL_reverse.values.reshape(-1, 1) # Reshape the data
data_normalized = data / np.max(data) # Normalize the data
# Split the data into training and testing sets
train_size = int(len(data_normalized) * 0.8)
train_data = data_normalized[:train_size]
test_data = data_normalized[train_size:]
# Function to create LSTM model
def create_lstm_model(units, activation, learning_rate):
model = Sequential()
model.add(LSTM(units=units, activation=activation, input_shape=(1, 1)))
model.add(Dense(units=1))
optimizer = Adam(learning_rate=learning_rate)
model.compile(optimizer=optimizer, loss='mean_squared_error')
return model
# Define hyperparameters for tuning
lstm_units = [50, 100, 200]
lstm_activations = ['relu', 'tanh']
learning_rates = [0.001, 0.01, 0.1]
epochs = 100
batch_size = 32
# Perform hyperparameter tuning for LSTM model
best_rmse = float('inf')
best_lstm_model = None
for units in lstm_units:
for activation in lstm_activations:
for learning_rate in learning_rates:
# Create and train LSTM model
model = create_lstm_model(units=units, activation=activation, learning_rate=learning_rate)
model.fit(train_data[:-1].reshape(-1, 1, 1), train_data[1:], epochs=epochs, batch_size=batch_size, verbose=0)
# Predict on test data
test_predictions = model.predict(test_data[:-1].reshape(-1, 1, 1)).flatten()
# Calculate RMSE
rmse = np.sqrt(mean_squared_error(test_data[1:], test_predictions))
# Check if current model has lower RMSE
if rmse < best_rmse:
best_rmse = rmse
best_lstm_model = model
# Predict on the entire dataset using the best LSTM model
all_lstm_predictions = best_lstm_model.predict(data_normalized[:-1].reshape(-1, 1, 1)).flatten()
# Inverse normalize the LSTM predictions
all_lstm_predictions = all_lstm_predictions * np.max(data)
# Calculate the scaling factor based on the maximum value of the original data
scaling_factor = np.max(close_prices_AAPL_reverse)
# Function to predict future stock prices using the LSTM model
def predict_future_lstm(model, data, num_predictions, scaling_factor):
predictions = []
# Get the last data point from the input data
last_data_point = data[-1]
for _ in range(num_predictions):
# Predict the next time step
prediction = model.predict(last_data_point.reshape(1, 1, 1))
predictions.append(prediction[0, 0])
# Update last_data_point to include the predicted value for the next iteration
last_data_point = np.append(last_data_point[1:], prediction)
# Inverse normalize the predictions
predictions = np.array(predictions) * scaling_factor
return predictions
# Predict the next 10 days using the LSTM model
num_predictions = 10
lstm_predictions = predict_future_lstm(best_lstm_model, data_normalized, num_predictions, scaling_factor)
# Plot the LSTM predictions for the next 10 days
plt.figure(figsize=(10, 6))
plt.plot(close_prices_AAPL_reverse, label='Actual')
plt.plot(np.arange(len(close_prices_AAPL_reverse), len(close_prices_AAPL_reverse) + num_predictions), lstm_predictions, label='LSTM Predicted')
plt.title(f"LSTM Model - RMSE: {best_rmse:.2f}")
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()
plt.show()
# Print the predicted stock prices for the next 10 days using LSTM
print("Predicted stock prices for the next 10 days:")
for i, prediction in enumerate(lstm_predictions, start=1):
print(f"Day {i}: {prediction:.2f}")