Stress Level Detection
Stress Level Detection
WARNING:tensorflow:From C:\Users\SIMRAN\anaconda3\lib\site-packages\keras
\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is de
precated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy inst
ead.
In [3]: df.head()
Out[3]:
MEAN_RR MEDIAN_RR SDRR RMSSD SDSD SDRR_RMSSD HR
5 rows × 36 columns
Scaling the features, ensuring that all features are on the same scale, which is crucial for
optimal model performance
localhost:8888/notebooks/Python_ML_Udemy1/Health_Stress/StressLevelDetection.ipynb 1/10
10/21/24, 11:59 AM StressLevelDetection - Jupyter Notebook
In [6]: X = df[features].values
y = df[label].values
In [7]: y
Out[7]: array([[0],
[1],
[1],
...,
[0],
[0],
[0]], dtype=int64)
In [9]: print(X_train.shape,y_train.shape,X_test.shape,y_test.shape)
In [11]: y_train
Creating Model
In [12]: from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
localhost:8888/notebooks/Python_ML_Udemy1/Health_Stress/StressLevelDetection.ipynb 2/10
10/21/24, 11:59 AM StressLevelDetection - Jupyter Notebook
WARNING:tensorflow:From C:\Users\SIMRAN\anaconda3\lib\site-packages\keras
\src\backend.py:873: The name tf.get_default_graph is deprecated. Please u
se tf.compat.v1.get_default_graph instead.
WARNING:tensorflow:From C:\Users\SIMRAN\anaconda3\lib\site-packages\keras
\src\optimizers\__init__.py:309: The name tf.train.Optimizer is deprecate
d. Please use tf.compat.v1.train.Optimizer instead.
localhost:8888/notebooks/Python_ML_Udemy1/Health_Stress/StressLevelDetection.ipynb 3/10
10/21/24, 11:59 AM StressLevelDetection - Jupyter Notebook
localhost:8888/notebooks/Python_ML_Udemy1/Health_Stress/StressLevelDetection.ipynb 4/10
10/21/24, 11:59 AM StressLevelDetection - Jupyter Notebook
Epoch 1/50
WARNING:tensorflow:From C:\Users\SIMRAN\anaconda3\lib\site-packages\keras
\src\utils\tf_utils.py:492: The name tf.ragged.RaggedTensorValue is deprec
ated. Please use tf.compat.v1.ragged.RaggedTensorValue instead.
WARNING:tensorflow:From C:\Users\SIMRAN\anaconda3\lib\site-packages\keras
\src\engine\base_layer_utils.py:384: The name tf.executing_eagerly_outside
_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outsid
e_functions instead.
localhost:8888/notebooks/Python_ML_Udemy1/Health_Stress/StressLevelDetection.ipynb 6/10
10/21/24, 11:59 AM StressLevelDetection - Jupyter Notebook
curacy: 0.9496 - val_loss: 0.1441 - val_accuracy: 0.9470
Epoch 39/50
253/253 [==============================] - 1s 5ms/step - loss: 0.1366 - ac
curacy: 0.9502 - val_loss: 0.1367 - val_accuracy: 0.9509
Epoch 40/50
253/253 [==============================] - 1s 5ms/step - loss: 0.1340 - ac
curacy: 0.9517 - val_loss: 0.1377 - val_accuracy: 0.9496
Epoch 41/50
253/253 [==============================] - 2s 6ms/step - loss: 0.1333 - ac
curacy: 0.9513 - val_loss: 0.1350 - val_accuracy: 0.9498
Epoch 42/50
253/253 [==============================] - 1s 5ms/step - loss: 0.1302 - ac
curacy: 0.9529 - val_loss: 0.1290 - val_accuracy: 0.9534
Epoch 43/50
253/253 [==============================] - 1s 5ms/step - loss: 0.1286 - ac
curacy: 0.9530 - val_loss: 0.1334 - val_accuracy: 0.9517
Epoch 44/50
253/253 [==============================] - 1s 5ms/step - loss: 0.1276 - ac
curacy: 0.9537 - val_loss: 0.1286 - val_accuracy: 0.9536
Epoch 45/50
253/253 [==============================] - 1s 6ms/step - loss: 0.1250 - ac
curacy: 0.9550 - val_loss: 0.1301 - val_accuracy: 0.9529
Epoch 46/50
253/253 [==============================] - 1s 6ms/step - loss: 0.1237 - ac
curacy: 0.9556 - val_loss: 0.1234 - val_accuracy: 0.9549
Epoch 47/50
253/253 [==============================] - 1s 5ms/step - loss: 0.1216 - ac
curacy: 0.9557 - val_loss: 0.1250 - val_accuracy: 0.9545
Epoch 48/50
253/253 [==============================] - 1s 5ms/step - loss: 0.1202 - ac
curacy: 0.9566 - val_loss: 0.1262 - val_accuracy: 0.9542
Epoch 49/50
253/253 [==============================] - 1s 5ms/step - loss: 0.1185 - ac
curacy: 0.9574 - val_loss: 0.1246 - val_accuracy: 0.9541
Epoch 50/50
253/253 [==============================] - 1s 5ms/step - loss: 0.1176 - ac
curacy: 0.9577 - val_loss: 0.1287 - val_accuracy: 0.9523
localhost:8888/notebooks/Python_ML_Udemy1/Health_Stress/StressLevelDetection.ipynb 7/10
10/21/24, 11:59 AM StressLevelDetection - Jupyter Notebook
In [16]: pd.DataFrame(history.history).plot(figsize=(10,7))
we can see above there is a steady decrease in the valiadtion loss and training loss, with no
signs of overfitting
Testing on testset
In [17]: df_test = pd.read_csv('test.csv')
Out[18]:
MEAN_RR RMSSD pNN25 pNN50 LF HF LF_HF
In [19]: X = df_test[features].values
y = df_test[label].values
localhost:8888/notebooks/Python_ML_Udemy1/Health_Stress/StressLevelDetection.ipynb 8/10
10/21/24, 11:59 AM StressLevelDetection - Jupyter Notebook
In [20]: y = k.utils.to_categorical(y)
In [24]: t = scaler.transform(data[features].iloc[5201].values.reshape(1,-1))
print(t)
C:\Users\SIMRAN\anaconda3\lib\site-packages\sklearn\base.py:420: UserWarni
ng: X does not have valid feature names, but StandardScaler was fitted wit
h feature names
warnings.warn(
In [26]: print(np.argmax(y_pred[0]))
print(data[label].iloc[5201])
1
condition 1
Name: 5201, dtype: int64
In [27]: t = scaler.transform(data[features].iloc[5545].values.reshape(1,-1))
print(t)
C:\Users\SIMRAN\anaconda3\lib\site-packages\sklearn\base.py:420: UserWarni
ng: X does not have valid feature names, but StandardScaler was fitted wit
h feature names
warnings.warn(
localhost:8888/notebooks/Python_ML_Udemy1/Health_Stress/StressLevelDetection.ipynb 9/10
10/21/24, 11:59 AM StressLevelDetection - Jupyter Notebook
In [29]: print(np.argmax(y_pred[0]))
print(data[label].iloc[5545])
0
condition 0
Name: 5545, dtype: int64
In [30]: t = scaler.transform(data[features].iloc[41032].values.reshape(1,-1))
print(t)
y_pred = model.predict(t)
print(np.argmax(y_pred[0]))
print(data[label].iloc[41032])
C:\Users\SIMRAN\anaconda3\lib\site-packages\sklearn\base.py:420: UserWarni
ng: X does not have valid feature names, but StandardScaler was fitted wit
h feature names
warnings.warn(
localhost:8888/notebooks/Python_ML_Udemy1/Health_Stress/StressLevelDetection.ipynb 10/10