Skip to content

Instantly share code, notes, and snippets.

@DiogoRibeiro7
Last active September 28, 2023 07:42
Show Gist options
  • Save DiogoRibeiro7/7c7c7d152f2164e88e9befcb0920f41d to your computer and use it in GitHub Desktop.
Save DiogoRibeiro7/7c7c7d152f2164e88e9befcb0920f41d to your computer and use it in GitHub Desktop.
import numpy as np
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
def preprocess_data(X_raw):
"""
Normalize the raw data for LSTM processing.
Parameters:
X_raw (array): The raw feature set.
Returns:
array: Normalized feature set.
"""
# Min-max scaling for normalization
X_min = np.min(X_raw, axis=0)
X_max = np.max(X_raw, axis=0)
X_normalized = (X_raw - X_min) / (X_max - X_min)
return X_normalized
def build_lstm(input_shape):
"""
Build a Long Short-Term Memory (LSTM) model for processing EHR data.
Parameters:
input_shape (tuple): The shape of the input sequences.
Returns:
Sequential: A simple LSTM model.
"""
model = Sequential()
model.add(LSTM(50, input_shape=input_shape))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return model
def train_and_evaluate_model(model, X_train, y_train, X_val, y_val):
"""
Train and evaluate the LSTM model.
Parameters:
model (Sequential): The LSTM model.
X_train (array): Training feature set.
y_train (array): Training labels.
X_val (array): Validation feature set.
y_val (array): Validation labels.
Returns:
dict: Training history.
"""
history = model.fit(X_train, y_train, epochs=10, validation_data=(X_val, y_val))
return history.history
# Generate synthetic data for demonstration
X_raw = np.random.rand(100, 10, 5) # 100 samples, 10 timesteps, 5 features
y_raw = np.random.randint(2, size=100) # 100 labels (0 or 1)
# Preprocess the data
X_processed = preprocess_data(X_raw)
# Split the data into training and validation sets
X_train, X_val, y_train, y_val = train_test_split(X_processed, y_raw, test_size=0.2, random_state=42)
# Build the LSTM model
input_shape = (X_train.shape[1], X_train.shape[2])
model = build_lstm(input_shape)
# Train and evaluate the model
history = train_and_evaluate_model(model, X_train, y_train, X_val, y_val)
# Output the training history
print("Training History:", history)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment