-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSTM_model.py
More file actions
120 lines (99 loc) · 3.83 KB
/
LSTM_model.py
File metadata and controls
120 lines (99 loc) · 3.83 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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout, Bidirectional
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam
# Load dataset
data = pd.read_excel(r'/Users/prashankulathunga/Documents/Projects/GOLD_PRICE_PREDICT-SYSTEM/Project_G/pythonProject/data_set/Gdata.xlsx')
data = data[data['Date'] > '2012-01-01']
# Convert 'Date' to datetime and set as index
data['Date'] = pd.to_datetime(data['Date'])
data.set_index('Date', inplace=True)
# Optional additional features
data['Year'] = data.index.year
data['Month'] = data.index.month
data['Day'] = data.index.day
# Normalize the 'USD' column
scaler = MinMaxScaler(feature_range=(0, 1))
data['USD'] = scaler.fit_transform(data[['USD']])
# Convert to numpy array
values = data['USD'].values
# Function to create sequences
def create_sequences(data, seq_length):
X, y = [], []
for i in range(len(data) - seq_length):
X.append(data[i:i + seq_length])
y.append(data[i + seq_length])
return np.array(X), np.array(y)
# Create sequences
seq_length = 90 # Sequence length
X, y = create_sequences(values, seq_length)
# Split data into training and testing sets
split = int(len(X) * 0.8)
X_train, X_test = X[:split], X[split:]
y_train, y_test = y[:split], y[split:]
# Reshape for LSTM input
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(Bidirectional(LSTM(64, return_sequences=True), input_shape=(X_train.shape[1], 1)))
model.add(Dropout(0.3))
model.add(Bidirectional(LSTM(64, return_sequences=False)))
model.add(Dropout(0.3))
model.add(Dense(32, activation='relu'))
model.add(Dense(1))
# Compile the model
optimizer = Adam(learning_rate=0.001)
model.compile(optimizer=optimizer, loss='mean_squared_error')
# Early stopping to prevent overfitting
early_stop = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
# Train the model
history = model.fit(
X_train, y_train,
epochs=100,
batch_size=16,
validation_data=(X_test, y_test),
callbacks=[early_stop],
verbose=1
)
# Plot training and validation loss
plt.figure(figsize=(10, 5))
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Model Loss During Training')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
# Make predictions
y_pred = model.predict(X_test)
# Inverse transform the scaled values
y_test_original = scaler.inverse_transform(y_test.reshape(-1, 1))
y_pred_original = scaler.inverse_transform(y_pred)
# Evaluate the model
mae = mean_absolute_error(y_test_original, y_pred_original)
mse = mean_squared_error(y_test_original, y_pred_original)
r2 = r2_score(y_test_original, y_pred_original)
print(f"MAE: {mae}\nMSE: {mse}\nR²: {r2}")
# Plot actual vs predicted prices
plt.figure(figsize=(10, 5))
plt.plot(range(len(y_test_original)), y_test_original, color='red', label='Actual Price')
plt.plot(range(len(y_pred_original)), y_pred_original, color='blue', label='Predicted Price')
plt.title('Gold Price Prediction (Enhanced LSTM)')
plt.xlabel('Days')
plt.ylabel('Price (USD)')
plt.legend()
plt.show()
# Create a DataFrame for actual and predicted prices
predictedDataFrame = pd.DataFrame({
"Date": data.index[-len(y_test_original):], # Align dates with test set
"Actual_Price": y_test_original.flatten(),
"Predicted_Price": y_pred_original.flatten()
})
# Display the DataFrame
print(predictedDataFrame.head(5))