-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_model.py
68 lines (54 loc) · 2.09 KB
/
train_model.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
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
import matplotlib.pyplot as plt
# Load data
data = pd.read_csv('input_data.csv', parse_dates=['Date'], index_col='Date')
data.dropna(inplace=True)
# Preprocessing
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(data[['Cases']])
# Prepare training data
train_size = int(len(scaled_data) * 0.8)
train_data = scaled_data[:train_size]
test_data = scaled_data[train_size:]
# Create time series data
def create_dataset(dataset, time_step=1):
X, Y = [], []
for i in range(len(dataset)-time_step-1):
X.append(dataset[i:(i+time_step), 0])
Y.append(dataset[i + time_step, 0])
return np.array(X), np.array(Y)
time_step = 10
X_train, y_train = create_dataset(train_data, time_step)
X_test, y_test = create_dataset(test_data, time_step)
# Reshape input to be [samples, time steps, features]
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 LSTM Model
model = Sequential([
LSTM(50, return_sequences=True, input_shape=(time_step, 1)),
LSTM(50, return_sequences=False),
Dense(25),
Dense(1)
])
# Compile and train the model
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(X_train, y_train, epochs=10, batch_size=64)
test_loss = model.evaluate(test_X, test_y)
print(f"Test Loss: {test_loss}")
model.save('outbreak_model.h5')
# Predictions
train_predict = model.predict(X_train)
test_predict = model.predict(X_test)
# Inverse scaling
train_predict = scaler.inverse_transform(train_predict)
test_predict = scaler.inverse_transform(test_predict)
# Plot results
plt.plot(data.index, scaler.inverse_transform(scaled_data), label='True Data')
plt.plot(data.index[:train_size], train_predict, label='Train Predictions')
plt.plot(data.index[train_size:], test_predict, label='Test Predictions')
plt.legend()
plt.show()