Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Machine Learning and Iot For Prediction and Detection of Stress

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Machine Learning and IoT for Prediction and

Detection of Stress

Mr.Purnendu Shekhar Pandey


BML, Munjal University, Gurgaon
Haryana, India
purnendu.pandey@bml.edu.in

Abstract— According to a cardiac surgeon, it is difficult to


predict age from heart rate as it is nonlinear, but we can use a
Through mapping of stress and heartbeat, we can identify a lot
person’s heart beat to predict whether that person is fit, unfit and of things, for example whether the person is nervous or not,
overtrained or not, provided we have that person’s age. Based on whether the person is in apprehension or fear, whether the
heart beat we can predict whether a person is in Stress or not. person is working out, whether the person is over trained,
Stress is one of the main factors that are affecting millions of remote monitoring of a patient with heart disease etc[4][7].
lives. Thus, it is important to inform the person about his Each one of the application targets a wide user base and has
unhealthy life style and even alarm him/her before any acute significant uses in the medical industry. Moreover, there are
condition occurs. To detect the stress beforehand we have used hardly few data sets available which gives data about a user
heart beat rate as one of the parameters. Internet of Things (IoT) resting heart rate and elevated heart rate and the one’s which
along with Machine Learning (ML) is used to alarm the situation
when the person is in real risk. ML is used to predict the
are available contain data of mere 200 individuals which is not
condition of the patient and IoT is used to communicate the enough to implement a machine learning algorithm. Through
patience about his/her acute stress condition. in this paper, we can very easily gather reliable data of
different individuals. Additionally, when we are training, our
Keywords—Stress, heart beat rate, IoT, ML heartbeat rate should be elevated, especially if a person is
doing any cardio-exercise and is dependent upon a person’s
I. INTRODUCTION age. Since exercising has become a part of most people’s daily
regime, it is crucial to identify whether he/she is exercising
Remote Stress detector is an IOT device which can detect the correctly or not.
stress level of a person using his/her heartbeat reading. When
II. LITERATURE REVIEW
people are stressed or nervous, there is an increase in their
heartbeat just like there’s a spike in the heartbeat when a Heart rate variability refers to the beat to beat alterations in the
person is having a heart attack, scientifically known as heart rate. According to National Institute of Health, infants
myocardial infarction [1][6]. This device locally collects heart generally have a heart rate of more than 100 beats per minute
beat reading from a person and sends it to a server on which settles down between 60 to 100 beats per minute
Digitalocean. All the computation is done on the server, which (resting heart rate) for children 10 years and older, and adults.
then finally predicts whether the person is stressed or not. It Heart rate is directly proportional to a person’s fitness [8]. A
generates a chart of the data of individual server. Node MCU person who is in good shape will have a resting heart rate in
is used as the development board and micro-python for the range of 50-60 bpm compared to an average individual
programing language [2]. The micro-python code runs on an whose heart rate might fluctuate between 60 -80 bpm, whereas
REPL (Read–Eval–Print Loop) interpreter. It is a high level a well-trained athlete can have an heart rate as low as 40 bpm
language shell that takes a single input from the user, executes [9].
it and return the results back to the user. Moreover, this work The diagnosticity of heart rate is restricted by several factors
uses a pulse sensor to detect the pulses from which we like environmental stressors and mental and physical
calculated the heartbeat rate. workload. The demand control model of job strain tells that
jobs in which people have low social support and no control
The medical world has seen strong correlations between stress over their job are more stressful i.e. when there is an
and heart disease, cancer and such terminal illnesses. Further imbalance between the job demand and the person’s ability to
stress has been shown to weaken immune systems, as well as meet those demands[10]. If the condition is long lasting, it
drop performance in all metrics of success[3][5]. Stress cannot may lead to cardiovascular diseases. There is a clear increase
be quantified and is very difficult to detect. in a person’s heart rate when he is stress or nervous, but the
offset differs from individual to individual,
978-1-5386-3893-4/17/$31.00 ©2017 IEEE According to a cardiac surgeon, it is difficult to predict age
from heart rate as it is nonlinear. But we can use a person’s
heart beat to predict whether that person is fit, unfit and
overtrained or not, provided we have that person’s age.
Studies have shown that if athletes have a higher resting heart
rate at 7:00 am in the morning for three or more consecutive
days then he/she is overtrained. Overtraining can lead to
decrease in fitness and strength of an athlete. A person can
have a maximum heart rate of (220 - (his/her age)). Using this
as a guideline, doctors charge up the defibrillator during
cardiac arrest. When a person is working out or in the gym
then his heart rate should be in the range of 50% - 70% of
(220 - (his/her age)). If his heart rate is less than this then he
needs to exercise harder [4][6].
III. DESIGN & IMPLEMENTATION
A. Methodology Fig.2. Block Diagram
The developed prototype detects whether a person is in stress
using variability in his/her heart rate. It can also help in IV. HARDWARE AND IMPLEMENTATION
detecting pattern of changes in a person’s heart rate when A. Components Used
he/she is working out at the gym. Each device is individual Node MCU
specific and needs to be calibrated for it to function properly. It contains a wifi module ESP8266 along with a 32-bit
During calibration the person should be in a relaxed mood and microprocessor (Tensilica Xtensa LX106) along with 160KB
should be resting. This is done to set up a baseline (Fig.1), RAM which is much better than 8-bit ATMega328P with 2KB
after calibration, the device uses this baseline (which is RAM on Arduino UNO (Fig.3). It establishes a connection
different for every individual) to determine whether that with Wifi and supports micropython which is a minimal
person is in stress/nervous, over trained or currently training version of python. Python allows flexibility, is quick and easy
(Fig.2). to do.
The heartbeat readings are pushed to the server where they are
filtered using a user’s network id to keep track of readings for
a particular individual. They are visually shown using a
connected scatter plot (Fig.5).

Fig.3.Node MCU
B. Pulse Sensor
It detects the pulse rate of the body which can be counted per
unit time to find out the heart rate. This heart rate data can be
used in multiple scenarios such as in sports, healthcare and
amongst mobile developers etc. It can be included in our daily
lives to monitor our anxiety and stress levels which might help
in better living. It records a reading by placing the tip of a
finger on it or attaching the sensor with a wrist. Pulse sensor
has a threshold value ranging from 525 to 610, which needs to
be calibrated (Fig.4). To count the heart rate, one needs to
count the number of pulses in a minute. This is done by
Fig.1. Data Flow Chart calculating the interbeat interval.
A. Snapshots

Fig.4.Pulse Sensor
C. Server and Program Flow
The NodeMCU periodically takes a heartbeat reading and
sends it, along with a timestamp to our server. The server has Fig.5. Heart rate vs time graph
been implemented in flask and deployed on a digitalocean
droplet running Ubuntu 16.04 LTS. We are using the most
basic tier that digitalocean offers, due to the lack of heavy
hardware requirements for our paper.

The Server is a Flask overlay on top of a Gunicorn WSGI


Server with NginX to handle asynchronous requests and
perform load balancing dynamically. The server is built on the
Restful API, MVC Controller model - and thus exposes
various endpoints to the user, the index endpoint allows users
to see the visualizations of the data posted to server, and
whether they’re stressed or not at the particular instant.
Fig.6. Determining whether the person is exercising
The /<age> endpoint exposes the activity tracker part of our
paper - it takes the age provided in the request and determines
whether someone is currently exercising enough or not (Fig.6
and Fig.7). The </data/add> endpoint is the location to point
all the sensor data being sent via the nodemcu.
Over time, the server assembles a fingerprinted record of the
user’s heartbeat at different times of the day. We can then
perform data analysis on the server - look at various times of
the day that the user has an elevated heart rate over the
baseline and thus determine his or her stress levels to some
degree of accuracy. Further, the website draws visualizations
of this data using the google graphs API and a few fractal
drawing libraries . Fig.7. Determining whether the person is at rest or relaxed state
V. RESULTS AND ANALYSIS
VI. CLASSIFICATION
Stress labels are calculated and median in our data set was A. Logistic Regression
stressed and map that to our detector. Further it is not possible Supervised learning comprises in taking in the connection
to detect someone’s age by their resting heart rate - while between two datasets: the watched information X and an outer
some correlations exist, the relationship is not clearly defined. variable y that we are attempting to anticipate, as a rule called
The exercise detection part of our paper does work well "target" or "names". Frequently, y is a 1D cluster of length
because that relationship is clearly defined as 50-80% of the n_samples.
max heart rate (220-Age) Every supervised estimator in scikit-learn actualize a fit(X, y)
method to fit the model and a predict(X) technique that, given
unlabeled perceptions X, gives back the anticipated marks y.
In the event that the expectation assignment is to order the
perceptions in an arrangement of limited marks, at the end of
the day to "name" the items watched, the undertaking is said to
be a grouping errand. Then again, if the objective is to foresee
a ceaseless target variable, it is said to be a regression task.
Logistic regression is the fitting relapse examination to lead
when the needy variable is dichotomous (binary). Like all
relapse examinations, the calculated relapse is a prescient
investigation. Strategic relapse is utilized to depict information
and to clarify the connection between one ward double factor
and at least one ostensible, ordinal, interim or proportion level
autonomous factors.

Procedure : Fig.8. Accuracy = 100 %


● Training Size = 318
● Number of Features = 270 SVM Trained Model Confusion Matrix
● Using One vs. Rest Classifier
● Creating Confusion Matrix
● Calculating Training and Test Accuracy
B. Support Vector Machine ( SVM )
A Support Vector Machine (SVM) is a discriminative
classifier formally characterized by an isolating hyperplane.
At the end of the day, given named preparing information
(managed taking in), the calculation yields an ideal hyperplane
which classifies new cases.

Procedure :
● Training Size = 318
Fig.9. Accuracy = 97 %
● Number of Features = 270
● Creating Confusion Matrix
● Calculating Training and Test Accuracy B. Test Accuracy
Logistic Regression
VII. COMPARING RESULTS
In the field of machine learning and specifically the problem
of statistical classification, a confusion matrix, also known as
an error matrix, is a specific table layout that allows
visualization of the performance of an algorithm, typically a
supervised learning one (in unsupervised learning it is usually
called a matching matrix)as shown in fig.8 and fig.9
respectively and its test accuracy is shown in fig.10 and fig.11.
Each column of the matrix represents the instances in a
predicted class while each row represents the instances in an
actual class (or vice versa). The name stems from the fact that
it makes it easy to see if the system is confusing two classes Fig.10. Accuracy = 66 %
(i.e. commonly mislabelling one as another).
In the confusion matrices below predicted label is in the y-axis SVM
and the true label is the x-axis.
A. Trained Model Confusion Matrix
Confusion Matrix of Logistic Regression

Fig.11. Accuracy = 68 %
VIII. CONCLUSION Crucial analysis like Arrhythmia and stress we need high
For calculating accuracy we find the missed predictions in the accuracy, for which more data is needed which can be
confusing matrix which gives us the error rate, subtracting it preferably acquired from sources like health bands,
from 1 gives us the accuracy of the classifier. smartphones, smart watches etc. Also for more accurate
modelling we can use Neural Networks, which work quite
well on datasets with high number of features.
Classifier Train Test Accuracy
Accuracy

Logistic Regression 100 % 66 % REFERENCES


[1] Effects of physical and mental tasks on heart rate variability.
SVM 97 % 68 % http://etd.lsu.edu/docs/available/etd-08222009-204359/unrestricted/thesis.pdf
by By Satya A. Paritala
[2] Cornelia Setz, Bert Arnrich, Johannes Schumm, Roberto La Marca, and
Two algorithms for classification are being used VF - 15 Gerhard Troster, Member, IEEE, and Ulrike Ehlert, “Discriminating Stress
From Cognitive Load Using a Wearable EDA Device”, IEEE transactions on
algorithm, which is a feature interval based classifier, which Information Technology in Biomedicine, Vol. 14, No. 2, March 2010
creates classification intervals during training and use it to test [3] http://www.sweetwaterhrv.com/documentation/stressandhrv.pdf
the classifier gives an accuracy of 62 % and Naive Bayes [4] Hock Beng Lim, “A Soldier Health Monitoring System for Military
approach which is a Bayesian classification algorithm gives 50 Applications,” International Conference on Body Sensor Networks.
% of accuracy while testing. [5] Y. Kawamoto, H. Nishiyama, N. Kato, Y. Shimizu, A. Takahara, and
T. Jiang, ``Effectively collecting data for the location-based authentication
Classifier Test Accuracy in Internet of Things,'' IEEE Syst. J., vol. PP, no. 99, pp. 1_9, Sep. 2015,
doi: 10.1109/JSYST.2015.2456878.
VF - 15 62 % [6] P. Gope and T. Hwang, ``BSN-care: A secure IoT-based modern
healthcare system using body sensor network,'' IEEE Sensors J., vol. 16, no. 5,
Naive Bayes 50 % pp. 1368_1376, Mar. 2016.
[7] H. Viswanathan, E. K. Lee, and D. Pompili, ``Mobile grid computing
for data- and patient-centric ubiquitous healthcare,'' in Proc. 1st IEEE
VF - 15 with weights 68 % Workshop Enabling Technol. Smartphone Internet Things (ETSIoT),
to features Jun. 2012, pp. 36_41
[8] N. Yang, X. Zhao, and H. Zhang, ``A non-contact health monitoring
model based on the Internet of Things,'' in Proc. 8th Int. Conf. Natural
We applied SVM and Logistic Regression which show Comput. (ICNC), May 2012, pp. 506_510.
[9] G. Yang et al., ``A health-IoT platform based on the integration of
considerable improvement over VF - 15 and Naive Bayes intelligent packaging, unobtrusive bio-sensor, and intelligent medicine box,''
without any external weights provided. In the weighted VF - IEEE Trans. Ind. Informat., vol. 10, no. 4, pp. 2180_2191, Nov. 2014.
15 the accuracy was comparable to SVM, but in SVM no [10] C. Doukas and I. Maglogiannis, ``Bringing IoT and cloud computing
external weights were provided as they were in the weighted towards pervasive healthcare,'' in Proc. Int. Conf. Innov. Mobile Internet
Services Ubiquitous Comput. (IMIS), Jul. 2012, pp. 922_926.
VF - 15 using genetic algorithms.
Using Logistic Regression and SVM we get an accuracy of 66
% and 68 % respectively, which shows an improvement in Author
accuracy after using SVM.
IX. FUTURE SCOPE Mr. Purnendu Shekhar Pandey is
currently working as Assistant Professor
This paper provide an insight into the applications of heart rate at BML, Munjal University, Hero Group,
monitoring and serve as a stepping stone for any new research Gurgaon, Haryana. PhD from Indian
work in this field. Moreover, the paper faces the challenge of Institute of Information Technology,
inadequate data, as any machine learning algorithm can only Allahabad, India. His areas of
give correct readings/predictions if it is applied on reliable specialization include Mobile IPv6,
data. Hence, the next step in this work would be to gather Wireless Network, Cellular networks,
heart rate readings of different individuals. Machine Learning and IoT. He has
We can integrate this work with any health monitoring device authored many research papers in
and safety device. To get better results, we are using his heart National and International Journals (SCI)
rate and also his daily activity pattern to determine at what and Conferences. He had reviewed
time of the day he is exercising and what time he is stressed, various IEEE conferences and SCI
as any device can give a false alert when the person is not in Journals. He is a professional member of
danger. One of the most promising future aspects of this work various Technical bodies such as ACM
can be to use a person’s profile and his daily heart rate and IEEE.
measurements along with his galvanic skin response to E-mail: purnendu.pandey@bml.edu.in
determine the mood of a person.

You might also like