-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
103 lines (78 loc) · 3.08 KB
/
main.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
file_path = '/Users/mo/Downloads/NVDA.NE.csv'
df = pd.read_csv(file_path)
df.head()
df.describe()
data = df['Close'].values
data = data.reshape(-1, 1)
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(data)
def create_dataset(dataset, time_step=60):
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 = 60
X, y = create_dataset(scaled_data, time_step)
# Split the data into training and testing sets
train_size = int(len(X) * 0.8)
test_size = len(X) - train_size
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]
# 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)
# Build the LSTM model
model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape=(time_step, 1)))
model.add(LSTM(50, return_sequences=False))
model.add(Dense(25))
model.add(Dense(1))
# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')
# Train the model
model.fit(X_train, y_train, batch_size=1, epochs=1)
# Make predictions
train_predict = model.predict(X_train)
test_predict = model.predict(X_test)
# Transform back to original form
train_predict = scaler.inverse_transform(train_predict)
test_predict = scaler.inverse_transform(test_predict)
y_train = scaler.inverse_transform(y_train.reshape(-1, 1))
y_test = scaler.inverse_transform(y_test.reshape(-1, 1))
# Debugging: Print shapes
print("Train predict shape:", train_predict.shape)
print("Test predict shape:", test_predict.shape)
print("y_train shape:", y_train.shape)
print("y_test shape:", y_test.shape)
# Plot the results
plt.figure(figsize=(16, 8))
plt.plot(df['Close'], label='Actual Stock Price')
# Shift train predictions for plotting
train_plot = np.empty_like(scaled_data)
train_plot[:, :] = np.nan
train_plot[time_step:len(train_predict) + time_step, :] = train_predict
# Shift test predictions for plotting
test_plot = np.empty_like(scaled_data)
test_plot[:, :] = np.nan
test_plot_start_idx = len(train_predict) + (time_step * 1) # Correcting the index
# Debugging: Print start and end indices
print(f"Test plot start index: {test_plot_start_idx}")
print(f"Test plot end index: {test_plot_start_idx + len(test_predict)}")
# Ensure the indices do not exceed the bounds
end_idx = min(test_plot_start_idx + len(test_predict), len(test_plot))
test_plot[test_plot_start_idx:end_idx, :] = test_predict[:end_idx - test_plot_start_idx]
# Plot the predictions
plt.plot(train_plot, label='Train Predictions')
plt.plot(test_plot, label='Test Predictions')
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()
plt.show()