-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlstm.py
48 lines (39 loc) · 1.78 KB
/
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
# lstm_model.py
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
def preprocess_data(data, prediction_days=60):
# Scale the data
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(data['Close'].values.reshape(-1, 1))
# Prepare the training data
x_train, y_train = [], []
for i in range(prediction_days, len(scaled_data)):
x_train.append(scaled_data[i - prediction_days:i, 0])
y_train.append(scaled_data[i, 0])
x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
return x_train, y_train, scaler
def build_lstm_model(input_shape):
# Build the LSTM model
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(input_shape[1], 1)))
model.add(LSTM(units=50, return_sequences=False))
model.add(Dense(units=25))
model.add(Dense(units=1))
model.compile(optimizer='adam', loss='mean_squared_error')
return model
def train_lstm_model(model, x_train, y_train, epochs=5, batch_size=32):
model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size)
return model
def make_predictions(model, data, scaler, prediction_days=60):
# Prepare test data for predictions
test_data = scaler.transform(data['Close'].values.reshape(-1, 1))
x_test = [test_data[i - prediction_days:i, 0] for i in range(prediction_days, len(test_data))]
x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
# Predict and invert scaling
predictions = model.predict(x_test)
predictions = scaler.inverse_transform(predictions)
return predictions