Diff of /heart.py [000000] .. [5369f3]

Switch to unified view

a b/heart.py
1
import numpy as np
2
import pandas as pd
3
import tensorflow as tf
4
from tensorflow.keras.models import Sequential
5
from tensorflow.keras.layers import LSTM, Dense, Dropout
6
from sklearn.model_selection import train_test_split
7
from sklearn.preprocessing import StandardScaler
8
9
# Load dataset (replace with actual dataset path)
10
file_path = "PPG_Dataset.csv"  
11
df = pd.read_csv(file_path)
12
13
# Select only Heart Rate (HR) & SpO₂ as features
14
X = df[['Heart_Rate', 'SpO2']].values  
15
y = df['Label'].values  # Label: 0 = Normal, 1 = Abnormal
16
17
# Normalize input features
18
scaler = StandardScaler()
19
X = scaler.fit_transform(X)
20
21
# Reshape for LSTM input (samples, timesteps, features)
22
X = X.reshape(X.shape[0], 1, 2)  # 1 timestep, 2 features (HR & SpO₂)
23
24
# Split data into training (80%) and testing (20%)
25
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
26
27
# Define LSTM model
28
model = Sequential([
29
    LSTM(64, return_sequences=True, input_shape=(1, 2)),  
30
    Dropout(0.2),
31
    LSTM(32),
32
    Dropout(0.2),
33
    Dense(16, activation='relu'),
34
    Dense(1, activation='sigmoid')  # Binary classification output
35
])
36
37
# Compile model
38
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
39
40
# Train the model
41
model.fit(X_train, y_train, epochs=20, batch_size=32, validation_data=(X_test, y_test))
42
43
# Save the trained model
44
model.save("heart_monitor_lstm.h5")
45
np.save("scaler.npy", scaler.mean_)  # Save scaler for real-time data
46
47
print("✅ Model training complete!")