-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotSlopPCA.py
More file actions
149 lines (115 loc) · 4.36 KB
/
NotSlopPCA.py
File metadata and controls
149 lines (115 loc) · 4.36 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
from datetime import datetime
import tensorflow as tf
from tensorflow import keras
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import numpy as np
import seaborn as sns
tqqq = pd.read_csv('uptodatetqqq.csv')
pca = pd.read_csv('pca_results.csv')
pca_df = pd.DataFrame(pca)
df = pd.DataFrame(tqqq)
pca_df = pca_df.sort_values('Date')
df = df.sort_values('Date')
pca_df = pca_df.set_index('Date')
df = df.set_index('Date')
df = df.join(pca_df, how='inner')
df = df.bfill().ffill()
df.index = pd.to_datetime(df.index)
print(df)
scaler = MinMaxScaler(feature_range=(0,1))
noscale = ['Date']
doscale = [col for col in df.columns if col not in noscale]
scaled_tqqq = scaler.fit_transform(df[doscale])
scaled_df = pd.DataFrame(scaled_tqqq, columns=doscale)
close_scaler = MinMaxScaler(feature_range=(0,1))
close_scaler.fit(df[['Close']].values)
def make_sequences(data, seq_length):
if isinstance(data, pd.DataFrame):
data = data.values
sequences = []
labels = []
for i in range(len(data) - seq_length):
sequences.append(data[i:i + seq_length])
price_2d = [[df['Close'].iloc[i + seq_length]]]
scaled_2d = close_scaler.transform(price_2d)
scaled_value = scaled_2d[0][0]
labels.append(scaled_value)
return np.array(sequences), np.array(labels)
length = 30
seqSet, labSet = make_sequences(scaled_df, length)
train_split = int(len(seqSet) * 0.8)
indices = np.random.permutation(len(seqSet))
seqSetTrain, seqSetTest = seqSet[indices[:train_split]], seqSet[indices[train_split:]]
labSetTrain, labSetTest = labSet[indices[:train_split]], labSet[indices[train_split:]]
print(labSetTest.shape)
seqSetTrain = seqSetTrain.reshape(seqSetTrain.shape[0], seqSetTrain.shape[1], seqSetTrain.shape[2])
seqSetTest = seqSetTest.reshape(seqSetTest.shape[0], seqSetTest.shape[1], seqSetTest.shape[2])
model = keras.models.Sequential([
keras.layers.LSTM(128, input_shape=(length, seqSetTest.shape[2])),
keras.layers.Dropout(0.2),
keras.layers.Dense(1)
])
model.compile(
optimizer = keras.optimizers.Adam(learning_rate=0.005),
loss = keras.losses.MeanSquaredError()
)
early_stopping = keras.callbacks.EarlyStopping(
monitor='val_loss',
patience=10,
restore_best_weights=True
)
history = model.fit(
x=seqSetTrain,
y=labSetTrain,
validation_data=(seqSetTest, labSetTest),
epochs=75,
callbacks=[early_stopping],
batch_size=32
)
predictions = model.predict(seqSetTest)
predict_rescale = close_scaler.inverse_transform(predictions)
labSetTest_rescale = close_scaler.inverse_transform(labSetTest.reshape(-1,1))
test_dates = df.index[train_split + length:]
plt.figure(figsize=(12,6))
plt.plot(test_dates, labSetTest_rescale, label = 'Actual Prices')
plt.plot(test_dates, predict_rescale, label = 'Predicted Prices')
plt.legend()
plt.title('Stock Price Prediction from LSTM - Test Data')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
df.index = pd.to_datetime(df.index)
last_sequence = seqSetTest[-1:] # Shape: [1, 30, n_features]
future_steps = 30
current_sequence = last_sequence.copy()
future_predictions = []
for _ in range(future_steps):
current_pred = model.predict(current_sequence, verbose=0)
current_pred_unscaled = close_scaler.inverse_transform(current_pred)[0][0]
future_predictions.append(current_pred_unscaled)
current_sequence = np.roll(current_sequence, -1, axis=1)
current_sequence[0, -1, 3] = current_pred[0][0]
future_predictions = np.array(future_predictions)
last_date = df.index[-1]
future_dates = [last_date + pd.Timedelta(days=x) for x in range(1, future_steps+1)]
plt.figure(figsize=(14,7))
plt.plot(df.index[-100:], df['Close'][-100:], 'b-', label='Historical Prices')
plt.plot(future_dates, future_predictions, 'r-', label='Predicted Future Prices')
plt.axvline(x=last_date, color='k', linestyle='--', label='Prediction Start')
plt.legend()
plt.title(f'Stock Price Prediction - Next {future_steps} Days')
plt.xlabel('Date')
plt.ylabel('Price ($)')
plt.xticks(rotation=45)
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
print("\nFuture Price Predictions:")
print("+" + "-"*32 + "+")
print("| Date | Predicted Price |")
print("+" + "-"*32 + "+")
for date, price in zip(future_dates, future_predictions):
print(f"| {date.strftime('%Y-%m-%d')} | ${price:>14.2f} |")
print("+" + "-"*32 + "+")