|
a |
|
b/influx.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 |
|
|
10 |
file_path = "/mnt/data/PPG_Dataset.csv" |
|
|
11 |
df = pd.read_csv(file_path) |
|
|
12 |
|
|
|
13 |
# Select only PPG-related fields (HR, SpO2) as features |
|
|
14 |
X = df[['Heart_Rate', 'SpO2']].values # Only HR & SpO2 |
|
|
15 |
y = df['Label'].values # Binary classification (0 = Normal, 1 = Abnormal) |
|
|
16 |
|
|
|
17 |
# Normalize features |
|
|
18 |
scaler = StandardScaler() |
|
|
19 |
X = scaler.fit_transform(X) |
|
|
20 |
|
|
|
21 |
# Reshape X to fit LSTM input shape (samples, timesteps=1, features) |
|
|
22 |
X = X.reshape(X.shape[0], 1, X.shape[1]) # (samples, 1 time step, 2 features) |
|
|
23 |
|
|
|
24 |
# Split data into training and testing sets (80% train, 20% test) |
|
|
25 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
|
|
26 |
|
|
|
27 |
# Define the LSTM model |
|
|
28 |
model = Sequential([ |
|
|
29 |
LSTM(64, input_shape=(1, X.shape[2])), # Single timestep |
|
|
30 |
Dropout(0.2), |
|
|
31 |
Dense(32, activation='relu'), |
|
|
32 |
Dense(1, activation='sigmoid') # Binary classification |
|
|
33 |
]) |
|
|
34 |
|
|
|
35 |
# Compile the model |
|
|
36 |
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) |
|
|
37 |
|
|
|
38 |
# Train the model |
|
|
39 |
model.fit(X_train, y_train, epochs=20, batch_size=32, validation_data=(X_test, y_test)) |
|
|
40 |
|
|
|
41 |
# Save the trained model |
|
|
42 |
model.save("heart_monitor_lstm_no_timesteps.h5") |
|
|
43 |
print("✅ Model training complete without time steps!") |