You are on page 1of 46

Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

CHAPTER 1

INTRODUCTION

1.1 HUMAN ACTIVITY DETECTION

Human activity detection plays one of the most significant roles in smart home
Environments and covers a wide range of applications including healthcare, daily fitness
recording, and anomalous situations alerting. Some of the simple human activities
includes (walking, running, sitting, etc.) are necessary for understanding complex human
behaviors. Also, the detection of activities such as walking and sitting can provide useful
information about a person's health condition. Nowadays, smartphone is not only a
device for communication but also smartphone-based applications are used everywhere.
Most smart phones have a tri-axial accelerometer and gyroscope as well as orientation
sensors. All these sensors can be used to know the acceleration and rate of rotation on
and around three physical axes (X, Y, and Z). With all these embedded sensors, we can
recognize different kinds of activities instead of using other wearable sensors attached to
human body. In this project, we are trying to build a smart phone-based human activity
detection system. The data are collected by the embedded sensors of the smartphones.
And some machine learning algorithms (Naive Bayes, Decision tree and Multilayer
Perceptron) are used to make the classifications.

Activity detection is the technique of recognizing the different physical activities or


the action performed by the users from a set of observations recorded during the user
activities in the context of the decisive atmosphere. In the recent time we have observed
that the Human Activity Detection system servs the multiple challenging applications
developed on the increase in pervasive, wearable and persuasive computing. Human
Activity Detection has turned out to be as the most significant technology that is changing
the situation of daily routine of the people and hence contributing to a wide range of
applications. For example, rehabilitation assistive technology, health-care domain mainly
in elder care support, diabetes, cognitive disorder and fitness tracking. Furthermore, the

Dept of CSE, NHCE 1


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

study in the activity detection has been so quick and progressive that it started to serve
as the applications that go beyond the activity detection.

The classical workflow of a human activity detection system deals with data
acquisition from different sensors of the smartphones generated during each activity
performed by the users that are recorded and sent to the remote system. This data is then
processed to remove noise and to obtain a cleaner and transformed data suitable for
further processing. The data is then explored to understand its nature and the type of
processing that can be applied in the next stages. The design of the later phases could
differ vastly on the application and its domain. But typically, the data is engineered to
make it more suitable by extracting more useful features from it. Moreover, the data is
segmented on basis of the window size of some frequency that needs to be performed.
Finally, a analytical model is created to recognize the activities performed by the user and
is assessed for its performance. This detected activity can be repurposed for numerous
applications as detecting change in the user activity, assistance in regard to the detected
activity etc.

1.2 OBJECTIVES OF THE PROPOSED PROJECT WORK


The following project on “Human Activity Detection based on Multiple Smart Phone
Sensors using Machine Learning” will help in understanding the complex human
behaviour by identifying the different activity performed by the users. Understanding
people's actions and their communication with the environment is a crucial element for
the development of the above-mentioned intelligent systems. Human Activity
Recognition is a field that specifically deals with this issue through the combination of
sensing and reasoning. The project will take data of the different activities as the input
and based on the data results will be formed. The data that has been provided to the code
is examined using the machine learning algorithms provided. Using those algorithms, the
conclusions are made and the statistical results are formed with the group or classes
assigned to each of the parameters. Using the help of this analysis the user will get to
know what physical action is being performed by him/her. To prepare this project several

Dept of CSE, NHCE 2


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

machine learning algorithms are employed to make the classifications and predict the
activities with promising preliminary results.

1.3 PROJECT DEFINITION


Human Activity Detection is considered as one of the most significant issues in
making the people’s life smarter and easier and covers a wide range of applications in
health-care domain, daily fitness recording and anomalous situation alerting. Some simple
human activities include walking, running, sitting, etc. are quite important in
understanding the complex human behavior. Also, the recognition of these activities can
provide all the necessary and useful information regarding the health condition of a
person.

Human activity detection has been extensively studied in the literature. Some
approaches and prototypes have been developed according to the different kinds of
information and algorithms. The data used for detection comprise video data or sensor
data (acceleration, orientation, and angular speed, etc.). The sensors could be on-board
smartphone sensors or sensors installed in wearable devices. Additionally, different
methods and algorithms can be used for activity detection. The hidden Markov model
(HMM) and the conditional random field (CRF) are among the most popular modelling
techniques.

1.4 PROJECT FEATURES


Our scheme is to develop and design a light weight and accurate system on a
smartphone that can identify the different human activities. However, to reduce the
computational time, storage and burden overhead supervised learning models are being
employed. Through testing, training and comparing different supervised learning
algorithm, we find the one that is best fit for our system in terms of accuracy and efficiency
on a smartphone and also in real life scenario.

We monitor the changes of accelerometer, orientation, gyroscope, and linear


acceleration values. And every 20th times the sensors values are changed, the 20th sensors

Dept of CSE, NHCE 3


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

values are being recorded. The sampling frequency is about 20Hz. Moreover, four-time
domain features (maximum, minimum, mean, and standard deviation of every 20 values)
are computed and stored. So, a fixed window length without overlapping is used in the
feature extraction phase. For each sensor, we consider its three axes as three individual
features. As we monitor four sensors and there are five-time domain features for each
axis as well as three axes for each sensor, a total of 60 features are extracted for each
instance. The activity types are labelled manually in each file.

The main features of the proposed system are:


• More efficient.
• Better activity detection systems.
• Reduced the time complexity of the system.
• Has a simpler architecture to understand for new users.
• Processing of large chunks of data becomes easier.

1.5 MODULES DESCRIPTION

1.5.1 DATA ACQUISITION

Data acquisition involves accelerometer reading which is generated during each


activity are recorded and sent to the remote system. At this point, the signals are detected
from the embedded smartphone sensors like tri-axial linear accelerometer, gyroscope and
orientation sensors to conclude the different human activities. To collect the data using
these sensors the user puts a smartphone in his/her pocket or hand and performs some
activities. Initial data collection and analyses show no difference when the smartphone is
being placed in different alignments in one pocket or in different pockets. On the other
hand, this would make the smart phone-based detection system simpler and more
practical. Hence, the subject does not need to care about how to place the smart phone
in his/her pockets during the data collection. Sensor data are saved as CSV files. In order
to increase the classification accuracy, data recorded in the beginning and end of each
activities are clipped from the data files. Due to the Android APIs, we record the sensor
data when the sensor values are changed every 20 times.

Dept of CSE, NHCE 4


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

1.5.2 DATA PREPROCESSING

Data preprocessing involves the removal of noise and normalization of data to


obtain a cleaner and transformed data suitable for additional processing. It is basically a
technique which is used to convert the raw data into an understandable format.
Whenever the data is collected from different sources it is being collected in raw format
which is not feasible enough for the analysis as it often incomplete, inconsistent and is
likely to hold many errors. Data preprocessing helps in resolving such issues.

1.5.3 SEGMENTATION

After the data preprocessing, comes the segmentation where the data is being
segmented based on the window size. Here, window size refers to the time interval
between each activity.

1.5.4 FEATURE EXTRACTION

Feature extraction includes a large number of features, and hence the array is
reduced before classifier construction. The step includes selection of the most significant
features for classification. Here, we monitor the changes in linear accelerometer,
gyroscope and orientation sensors values. The values are changes after a fixed time
interval and are recorded and we are finding out the relation within the data values such
as maximum, minimum, standard deviation and the mean. Hence the data values are
computed and stored. So, a fixed window size without overlapping is used in the feature
extraction.

1.5.5 ACTIVITY RECOGNISITION

Here, we are using different classifier algorithms (Naïve Bayes, Multilayer


Perceptron and Decision Tree) are used to predict the different activities performed by
the user.

Dept of CSE, NHCE 5


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

CHAPTER 2

LITERATURE SURVEY

2.1 HUMAN ACTIVITY DETECTION


Literature survey is the most important step in software development process.
Before developing the tool, it is necessary to determine the time factor, economy and
company strength. Once these things are satisfied, then next steps are to determine which
operating system and language can be used for developing the tool. Once the
programmers start building the tool the programmers need lot of external support. This
support can be obtained from senior programmers, from book or from websites. Before
building the system, the above consideration is taken into account for developing the
proposed system. We have to analyze the Human Activity Detection Survey:

Human Activity Detection


Human activity detection has been studied for many years by the researchers and
they have proposed different solutions to challenge the problem. Existing approaches
classically used vision sensors, inertial solutions as well as the mixture of both. Machine
learning and threshold-base algorithms are frequently applied. One of the most basic
differences between the two is that while machine learning usually delivered more
accurate and reliable results while threshold-base algorithms are much faster and simpler.
In order to accomplish human activity detection task or to detect the activity one
or more multiple cameras have been used which were used to capture the different
positions or the postures of the body. There are many approaches that have been
proposed which combine both the use of cameras and sensors. The most key element of
all the algorithms is data processing because the quality of input features seems to have
a greater impact on the performance of the system. Some of the earlier works are focused
on generating useful features from time series data.
Active learning technique has also been applied on several machine learning
problems which are time-consuming and labor-expensive to the input samples. There are

Dept of CSE, NHCE 6


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

some applications like Speech Recognition, Information Extraction and Handwriting


Recognitions and these techniques has also been applied on human activity problem
before Face Recognition points used by FBI in surveillance.

2.2 EXISTING SYSTEM


The First and Foremost is the use of Body Worn sensors. The older HAR systems
comprises of different body worn sensors that are tightly coupled with different parts of
the human body to obtain the activity of a person. Practically, it is not possible to carry an
external device with you every time and also people often tend to forget to wear these
devices.

The use of multiple sensors to achieve the same goal makes the application bulky
leading to slower processing of the data and also affects its cost.

In most of the application positioning of the device is also concerned with the
success of the application i.e. Most of the application build are position specific device. If
the device kept in hands then the values will be different from the values being generated
when the device is kept in pocket.

2.3 PROPOSED SYSTEM


The system will be able to identify the different activity based on the data
generated from tri-axial accelerometer on all the axes (x, y, z) during each activity like
walking, sitting, standing etc. and each activity is labeled. To deploy the system various
machine learning algorithms are used to fulfil our purpose.

The working of deployment will be as follows:

• Firstly, the data generated from the different sensors are taken as an input to the
system for the analysis.

• After the data is provided by the user the code comes into action and from that
machine learning algorithms and tools will come into action.

• The first step will be the preparation of the data for the analysis purpose which
also involves cleaning of the data and discarding the data with null or no values.

Dept of CSE, NHCE 7


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

• After the data is prepared for the analysis purpose the data is segmented further
according the window size. Here the window size is the time interval between each
activity.

• After the data is being segmented the data is further sent for feature extraction
where we find out the relationship between the data values. (MAX, MIN, MEAN,
Standard Deviation).

• And finally, with the help of different classifier algorithm the system will be able
to detect the human activity.

2.4 SOFTWARE DESCRIPTION

2.4.1 Python

Python is a popular platform used for research and development of production


systems. It is a vast language with number of modules, packages and libraries which
delivers several ways of achieving a task.

Python and its libraries like NumPy, SciPy, Scikit-Learn, Matplotlib are used in data
science and data analysis. They are also widely used for creating scalable machine
learning algorithms. Python implements popular machine learning techniques such as
Classification, Regression, Recommendation, and Clustering.

Python offers convenient framework for performing data mining tasks on large
volumes of data effectively and efficiently in lesser time. It comprises several
implementations achieved through algorithms such as linear regression, logistic
regression, Naïve Bayes, k-means, K nearest neighbor, and Random Forest.

2.4.2 Python in Machine Learning

Python offers libraries that allows developers to use optimized algorithms. It


implements popular machine learning techniques such as recommendation,
classification, and clustering. Hence, it is necessary to have a brief introduction to
machine learning before we move further.

Dept of CSE, NHCE 8


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

2.4.3 What is Machine Learning?

Data science, machine learning and artificial intelligence are some of the most top
trending topics in the tech world today. Data mining and Bayesian analysis are trending
and this is adding the demand for machine learning.

Machine learning is basically a discipline that deals with programming that


provides system the ability to automatically learn and improve with experience. Here,
learning infers recognizing and understanding the input data and taking informed
decisions based on the supplied data. It is very difficult to consider all the decisions based
on all possible inputs. To solve this problem, algorithms are developed so as to build
knowledge from a specific data and past experience by applying the principles of
statistical science, probability, logic, mathematical optimization, reinforcement learning,
and control theory. So, machine learning is capable to predict future observation on the
basis of past experiences.

2.4.4 Application of Machine Learning Algorithms

The developed machine learning algorithms are used in various applications such as –

▪ Vision processing

▪ Language processing

▪ Forecasting things like stock market trends, weather

▪ Pattern recognition

▪ Games

▪ Data mining

▪ Expert systems

▪ Robotics

2.4.5 Steps Involved in Machine Learning


A machine learning project involves the following steps −

Dept of CSE, NHCE 9


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

▪ Defining a Problem

▪ Preparing Data

▪ Evaluating Algorithms

▪ Improving Results

▪ Presenting Results

The best way to get started using Python for machine learning is to work through a
project end-to-end and cover the crucial steps like loading data, summarizing data,
evaluating algorithms and making some predictions. This gives you a replicable method
that can be used dataset after dataset. You can also add further data and improve the
results.

2.5 ENVIRONMENTAL SETUP

2.5.1 Libraries and Packages

In order to understand machine learning, first you need to have a basic knowledge
of Python programming. Furthermore, there are a number of libraries and packages that
are generally used in performing several machine learning tasks as listed below –

numpy − is used for its N-dimensional array objects.

NumPy is the important package for scientific computing with Python. It


comprises among other things:

▪ a powerful N-dimensional array object.


▪ Sophisticated (broadcasting) functions.
▪ tools for integrating C/C++ and Fortran code.
▪ useful linear algebra, Fourier transform, and random number capabilities.

pandas − is a data analysis library that includes data frames.

Dept of CSE, NHCE 10


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Pandas is an open-source Python Library delivering high-performance data


manipulation and analysis tool using its powerful data structures. The name Pandas is
derived from the word Panel Data – an Econometrics from Multidimensional data.

matplotlib − is 2D plotting library for creating graphs and plots.

Matplotlib is a Python 2D plotting library which produces publication quality figures


in a variety of hardcopy formats and interactive environments across platforms.
Matplotlib can be used in Python scripts, the Python and IPython shells, the Jupyter
Notebook, web application servers, and four graphical user interface toolkits.

scikit-learn − is the algorithms used for data analysis and data mining tasks.

Scikit-learn delivers a wide range of supervised and unsupervised learning


algorithms via a consistent interface in Python. It is licensed under a permissive simplified
BSD license and is distributed under many Linux distributions, encouraging academic and
commercial use. Some popular groups of models provided by scikit-learn contains:

▪ Clustering: is for grouping unlabeled data such as K-Means.


▪ Cross Validation: is for estimating the performance of supervised models on
unseen data.
▪ Datasets: is for test datasets and for generating datasets with specific properties
for examining model behavior.
▪ Dimensionality Reduction: is for reducing the number of attributes in data for
summarization, visualization and feature selection such as Principal component
analysis.
▪ Ensemble methods: is for combining the predictions of multiple supervised
models.
▪ Feature extraction: is for defining the attributes in image and text data.
▪ Feature selection: is for identifying meaningful attributes so as to create
supervised models.
▪ Parameter Tuning: is for getting the most out of supervised models.

Dept of CSE, NHCE 11


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

▪ Manifold Learning: is for summarization and depicting complex multi-dimensional


data.
▪ Supervised Models: is a vast array not restricted to generalized linear models,
discriminate analysis, naive Bayes, lazy methods, neural networks, support vector
machines and decision trees.
seaborn − a data visualization library based on matplotlib.
One of the finest but also more challenging ways to get your insights across is to
visualize them and that way you can more easily identify patterns, hold difficult concepts
or draw the attention to main elements. When you are using Python for data science, you
will most perhaps will have already used Matplotlib, a 2D plotting library that allows you
to create publication-quality figures. One more complimentary package that is based on
this data visualization library is Seaborn, which delivers a high-level interface to draw
statistical graphics.

Install Anaconda

To install Python and other scientific computing and machine learning packages at
the same time, we need to install Anaconda distribution. It is a Python implementation
for Linux, Windows and OSX, and includes various machine learning packages like numpy,
scikit-learn, and matplotlib. It also includes Jupyter Notebook, an interactive Python
environment. We can install Python 2.7 or any 3.x version as per our requirement. Note
that you have to ensure that Anaconda’s Python distribution installs into a single
directory, and does not affect other Python installations, if any, on your system. In order
to work with graphs and plots, we will need these Python library packages -
matplotlib and seaborn.

Dept of CSE, NHCE 12


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

2.6 MACHINE LEARNING ALGORITHM

2.6.1. Classification vs Regression

Fig 2.1 Machine Learning Algorithms

Regression in Machine Learning

In machine learning, regression algorithms attempt to estimate the mapping


function (f) from the input variables (x) to numerical or continuous output variables (y). In
this case, y is a real value, which can be an integer or a floating-point value. Hence,
regression prediction problems are usually quantities or sizes. For example, when
provided with a dataset about houses, and you are asked to predict their prices, that is a
regression task because price will be a continuous output.

Classification in Machine Learning

On the other hand, classification algorithms attempt to estimate the mapping


function (f) from the input variables (x) to discrete or categorical output variables (y). In
this case, y is a category that the mapping function predicts. If provided with a single or
several input variables, a classification model will attempt to predict the value of a single
or several conclusions. For example, when provided with a dataset about houses, a
classification algorithm can try to predict whether the prices for the houses “sell more or
less than the recommended retail price.” Here, the houses will be classified whether their
prices fall into two discrete categories: above or below the said price. Examples of the

Dept of CSE, NHCE 13


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

common classification algorithms include logistic regression, Naïve Bayes, decision trees,
and K Neighbors.

2.6.2. Naïve Bayes Algorithm

It is basically a classification technique based on Bayes Theorem with an


assumption of independence among predictors. In a simpler and easy terms, a Naive
Bayes classifier assumes that the presence of a particular feature in a class is unrelated to
the presence of any other feature. For example, a fruit may be considered to be an apple
if it is red, round, and about 3 inches in diameter. Even if these features depend on each
other or upon the existence of the other features, all of these properties independently
contribute to the probability that this fruit is an apple and that is why it is known as ‘Naive’.
Naive Bayes model is easy to build and particularly useful for very large data sets. Along
with easiness, Naive Bayes is known to outperform even highly sophisticated classification
methods.

Bayes theorem delivers a way of calculating posterior probability P(c|x) from P(c),
P(x) and P(x|c). Look at the equation below:

Fig 2.2 Naïve Bayes Algorithm

• P(c|x) is the posterior probability of class (c, target) given predictor (x, attributes).

• P(c) is the prior probability of class.

Dept of CSE, NHCE 14


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

• P(x|c) is the likelihood which is the probability of predictor given class.

• P(x) is the prior probability of predictor.

How Naïve Bayes algorithm works?

Let’s try to understand it using a simple example. In the following example I have a
training data set of weather and corresponding target variable ‘Play’ (suggesting
possibilities of playing). Now, we need to classify whether players will play or not based
on weather condition. Let’s follow the following steps to perform it.

Step 1: Convert the data set into a frequency table.

Step 2: Create Likelihood table by finding the probabilities like Overcast probability = 0.29
and probability of playing is 0.64.

Table 2.1 Sample Dataset for Naïve Bayes

Step 3: Now, use Naive Bayesian equation to calculate the posterior probability for each
class. The class with the highest posterior probability is the outcome of prediction.

Problem: Players will play if weather is sunny. Is this statement being correct?

We can solve it using above discussed method of posterior probability.

P(Yes | Sunny) = P( Sunny | Yes) * P(Yes) / P (Sunny)

Dept of CSE, NHCE 15


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Here we have P (Sunny |Yes) = 3/9 = 0.33, P(Sunny) = 5/14 = 0.36, P(Yes)= 9/14 = 0.64

Now, P (Yes | Sunny) = 0.33 * 0.64 / 0.36 = 0.60, which has higher probability.

Naive Bayes uses a similar method to predict the probability of different class based
on various attributes. This algorithm is mostly used in text classification and with problems
having multiple classes.

What are the Advantages and Disadvantages of Naive Bayes?

Advantages:

▪ It is easy and fast to predict class of test data set. It also accomplishes well in multi
class prediction
▪ When assumption of independence holds, a Naive Bayes classifier performs better
when compare to other models like logistic regression and you need less training
data.
▪ It performs well in case of categorical input variables compared to numerical
variable(s). For numerical variable, normal distribution is assumed (bell curve,
which is a strong assumption).

Disadvantages:

▪ If categorical variable has a category (in test data set), not being observed in
training data set, then the model will assign a 0 (zero) probability and will be
unable to make a prediction. This is often known as “Zero Frequency”. To solve
this, we can use the smoothing technique. One of the simplest smoothing
techniques is called Laplace estimation.
▪ On the other hand, naive Bayes is also known as a bad estimator, so the probability
outputs from predict_proba are not to be taken too seriously.

Dept of CSE, NHCE 16


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

▪ Another limitation of Naive Bayes is the assumption of independent predictors. In


real life, it is almost impossible that we get a set of predictors which are completely
independent.

2.6.2. Multilayer Perceptron

A multilayer perceptron (MLP) is a feedforward artificial neural network, having


three or more layers of nodes which includes an input layer, a hidden layer and an output
layer and is trained through backpropagation. The weights are updated according to the
amount of error between the target output and the desired/actual output through
comparison.

Fig 2.3 Multilayer Perceptron Algorithm for Single Output Unit

The three basic elements of the perceptron are:

▪ Links – It is basically a set of connection links that carries a weight including a bias
having a weight 1.
▪ Adder – It basically adds the input after they are being multiplied with their
respective weights.
▪ Activation Function – It basically limits the output of neuron and returns 1, if the
input is positive and 0 for any negative input.

Dept of CSE, NHCE 17


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Training Algorithm

The perceptron network can be trained for single as well as multiple output units.
The basics steps involved in training the single Output Unit is described below:

Step 1- Initialize the following to start the training-

• Weights
• Bias
• Learning rate α

For the ease of calculation and simplicity, weights and bias must be assigned to 0 and the
learning rate must be equal to 1.

Step 2 – Continue step 3-8 when the stopping condition is not true.

Step 3 – Continue step 4-6 for every training vector X.

Step 4 – Activate each input unit as follows –

Step 5 – Now find the net input with the following relation -

Here ‘b’ is bias and ‘n’ is total number of input nuerons.

Step 6 – Apply the following activation function to obtain the final output.

Dept of CSE, NHCE 18


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Step 7 – Adjust the weights and bias as follows –

Case 1 – if y ≠ t then,

Case 2 – if y = t then,

Here ‘y’ is actual output and ‘t’ is the target output.

Step 8 – This is Test for the stopping condition that would arise when there is no change
in weight.

Training Algorithm for Multiple Output Units

Fig 2.4 Multilayer Perceptron Algorithm for Multiple Output Units

Dept of CSE, NHCE 19


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Step 1- Initialize the following to start the training-

• Weights
• Bias
• Learning rate α

For the ease of calculation and simplicity, weights and bias must be assigned to 0 and the
learning rate must be equal to 1.

Step 2 – Continue step 3-8 when the stopping condition is not true.

Step 3 – Continue step 4-6 for every training vector X.

Step 4 – Activate each input unit as follows –

Step 5 – Now find the net input with the following relation -

Here ‘b’ is bias and ‘n’ is total number of input nuerons.

Step 6 – Apply the following activation function to obtain the final output for each
output unit j = 1 to m –

Step 7 – Adjust the weights and bias as for x = 1 to n and j = 1 to m as follows -

Dept of CSE, NHCE 20


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Case 1 – if yj ≠ tj then,

Case 2 - if yj = tj then,

Here ‘y’ is actual output and ‘t’ is the target output.

Step 8 – This is Test for the stopping condition that would arise when there is no change
in weight.

Back Propagation Neural Networks

The Back Propagation Neural Network is a multilayer neural network which


consists of the input layer, at least one hidden layer and an output layer. The amount of
the error in this network is calculated at the output layer through comparison of the
target output and the actual output and the network is thus propagated back to the
input layer.

Fig 2.5 Back Propagation Neural Network

Dept of CSE, NHCE 21


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Training Algorithm

The training algorithm for the BPN uses binary sigmoid activation function and
consist of these three phases –

1. Feed Forward Phase


2. Back Propagation Phase
3. Updating of weights

These are the following steps involved in training –

Step 1- Initialize the following to start the training-

• Weights
• Learning rate α

For the ease of calculation and simplicity, take some random values.

Step 2 – Continue step 3-11 when the stopping condition is not true.

Step 3 – Continue step 4-6 for every training pair.

Phase 1

Step 4 − Each input unit receives input signal xi and sends it to the hidden unit for all i = 1
to n

Step 5 − Calculate the net input at the hidden unit using the following relation –

Here b0j is the bias on hidden unit, vij is the weight on j unit of the hidden layer coming
from i unit of the input layer.

Dept of CSE, NHCE 22


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Now calculate the net output by applying the following activation function -

Send these output signals of the hidden layer units to the output layer units.

Step 6 − Calculate the net input at the output layer unit using the following relation −

Here b0k is the bias on output unit, wjk is the weight on k unit of the output layer coming
from j unit of the hidden layer.

Calculate the net output by applying the following activation function.

Phase 2

Step 7 − Calculate the error correcting term, in correspondence with the target pattern
received at each output unit, as follows −

On this basis, update the weight and bias as follows −

Then, send δk back to the hidden layer.

Dept of CSE, NHCE 23


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Step 8 − Now each hidden unit will be the sum of its delta inputs from the output units.

Error term can be calculated as follows –

On this basis, update the weight and bias as follows −

Phase 3

Step 9 − Each output unit (ykk = 1 to m) updates the weight and bias as follows −

Step 10 − Each output unit (zjj = 1 to p) updates the weight and bias as follows –

Step 11 – Now check for the stopping condition, that can be either the number of
epochs reached or the target output matches the actual output.

Dept of CSE, NHCE 24


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

2.6.3. Decision Tree Algorithm

Decision Tree is an important algorithm for predictive modeling and can be used to
visualize and explicitly represent decisions. It is basically a graphical representation of
branching methodology to demonstrate all possible outcomes based on certain
conditions. The decision tree consists of:

• Nodes: is a representation of attribute names of a given data.


• Branches: is a representation of attribute values or the outcome.
• Leaf: is the representation of class labels or the decision made after computing
attribute.

The Decision Tree can be classified into two types:

Classification Tree:

The Classification Tree is used to separate a dataset into different classes based on
the particular basis and is generally used when we expect response variable in categorical
nature.

Regression Tree:

The other type of decision tree is known as Regression Tree which is used when the
response variable is continuous or numerical.

Decision Tree classifier is the simplest and widely used classification technique. It
applies a straightforward idea to solve the classification problem.

How to build a Decision Tree?

The method to build a Decision Tree is known as Two step method:

▪ Tree Construction

1. Pick an attribute for the division of given data.

2. Divide the given data into sets on the basis of this attribute.

3. For every set created above – repeat 1 and 2 until you find leaf nodes in all the
branches of the tree – Terminate.

Dept of CSE, NHCE 25


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

▪ Tree Pruning (Optimization)

Identify and remove branches in the Decision Tree that are not useful for
classification.

▪ Pre-Pruning- It is also called forward pruning or online pruning that


prevents the generation of non-significant branches. It involves a
termination condition to decide when it is desirable enough to terminate
some of the branches prematurely as the tree is produced.

▪ Post-Pruning- It is also known as backward pruning. The first step is to


generate the Decision Tree and then eliminate the non-significant
branches. It suggests that we first begin by generating the tree and then
adjust it with the aim of improving the classification accuracy or unseen
instances. There are basically two major method of doing so. One method
widely starts by converting the tree into equivalent set of rules and the
other one aims to retain the decision tree but to replace some of its subtree
by leaf nodes and thus converting the tree into a smaller pruned one
predicting the classification of unseen occurrences accurately.

Table 2.2 Sample Dataset for Decision Tree

Dept of CSE, NHCE 26


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Table 2.3 Three Data Sets formed after division at root node on the basis of “age” attribute.

age?

<=30

31...40
>40
Student?

Credit rating?
no yes Yes

excellent fair
no Yes

no Yes
Fig 2.6 Decision Tree

The final decision tree is constructed on the basis of described manner, classify a test
sample.

(age, student, credit rating, buys_computer)

(<=30, yes, excellent, ?) – Will the student buy computer?

Dept of CSE, NHCE 27


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Attribute Selection Measures

It is the process of removing the redundant attributes which are considered


irrelevant in the data mining task. The main objective here is to find for a worthy set of
attributes that produce comparable classification results when all the attributes are used.
Some of the attribute selection measures are defined below: -

Information Gain

It measures the expected reduction in entropy caused by portioning the examples


according to the attribute. Iterative Dichotomiser decision tree uses information gain.
Entropy measures the impurity of a set of samples.

Where info(D) is the known as entropy of D.

Gini Ratio

The information gain is partial for the attributes with many outcomes which means
it prefers the attribute with larger number of distinct values. C4.5, is an improvement of
ID3, uses an extension to the information gain known as gain ratio.

Where Gain (A) is expected reduction in the information gain.

Where SplitInfo (A) is the value defined analogously with Info(D) as

Gini Index

Dept of CSE, NHCE 28


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

It is used in Classification and Regression Tree and thus measures the impurity of D,
a data partition or set of training tuples as

Where Pi is the probability that a Tuple in D belongs to the class Ci.

Dept of CSE, NHCE 29


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

CHAPTER 3

REQUIREMENT ANALYSIS

3.1 PURPOSE
1) Framework for Activity detection of Human, by applying Machine Learning
Algorithm.

2) Ascertain the type of actions being performed.

3.1.1 Scope

• Widening the horizon for health-domain in the form of human based


applications, which are the most important issues in smart home environments.

• Classifying the type of activity or the action being performed.

• Finding the details of the activity.

3.2 FUNCTIONAL REQUIREMENTS


The functional requirements of a system are mainly the behavior of the system
when a certain group of inputs are given evaluated through the system software. The
functional requirements of the system are:

▪ The system should be capable of taking data input from the user.

▪ The system should be capable of data preprocessing before analysis of data.

▪ The system should be able to analyze the data.

▪ The system should be able to group data based on patterns.

▪ The system should be able to assign parameters based on data groups.

▪ The system should be able to train the model using the training sets and assign
labels to the parameter.

▪ The system must validate trained model using test set as well as labeled data.

Dept of CSE, NHCE 30


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

▪ The system should be able to classify the data based on the labels.

▪ The system should be able to accurately classify the activity being performed by
the user.

3.2 NON-FUNCTIONAL REQUIREMENTS


The nonfunctional requirements describe how a system must behave and establish
constraints of its functionality. This type of requirements is also known as the system’s
quality attributes. Attributes such as performance, security, usability, compatibility are
not the feature of the system, they are a required characteristic. They are "developing"
properties that emerge from the whole arrangement and hence we can't compose a
particular line of code to execute them. Any attributes required by the customer are
described by the specification. We must include only those requirements that are
appropriate for our project.

Some Non-Functional Requirements are as follows:

▪ Reliability

▪ Maintainability

▪ Performance

▪ Portability

▪ Scalability

▪ Flexibility

Dept of CSE, NHCE 31


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

3.3 HARDWARE REQUIREMENTS

Processor : Intel Pentium or higher


RAM : More than 2 GB
Hard Disk : 10 GB
Input device : Standard Keyboard and Mouse
Output device : High Resolution Monitor

3.4 SOFTWARE REQUIREMENTS

• Operating system : Windows 7 or higher


• IDE : Jupyter Notebook
• Data : Human Activity Recognition
• Modules : NumPy, Pandas, matplotlib, sklearn

Dept of CSE, NHCE 32


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

CHAPTER 4

DESIGN

4.1 GENERAL STRUCTURE


A system architecture or systems architecture is the conceptual model that defines
the structure, behavior, and more views of a system.

4.2 SYSTEM ARCHITECTURE

Fig 4.1 System Architecture

The architecture diagram shown above shows the details of system which is been
carried in our proposed work, activity is being sensed first. Then these activities are then
filtered for the removal of noise and normalization of data. After that in feature extraction,
we monitor the changes in linear accelerometer, gyroscope and orientation sensors
values. The values are changes after a fixed time interval and are recorded and we are
finding out the relation within the data values maximum, minimum, standard deviation
and the mean. Hence the data values are computed and stored. So, a fixed window size
without overlapping is used in the feature extraction. And after that the based on the
classification algorithms the data is being trained first in the training phase and then from
the learned model the activity is prediction is made and the predicted activity is being
recognized in the testing phase.

Dept of CSE, NHCE 33


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

4.3 DATA FLOW DIAGRAM

Data Collection from


Tri-axial
Accelerometer sensor
Raw Data

Data Filtering for


Noise Removal
Filtered Data

Data Segmentation
based on Window
Size Segmented Data

Feature Extraction
(MAX, MIN, MEAN and
Standard Deviation)

Features

Classifier Algorithms

Activity Detection e.g. Sitting, Walking, Standing etc.

Fig 4.2 Data Flow Diagram

The above diagram shows us the flow of the program. The data is first collected
from linear accelerometer and then the raw data is filtered and then it is being segmented
and then data is sent for feature extraction where the relation (MAX, MIN, MEAN and
Standard Deviation) between the data value is found. After that the classifier algorithms
like Naïve Bayes, Multilayer Perceptron and Decision Tree is being applied and the
corresponding activity of the human being is detected with the better accuracy result.

Dept of CSE, NHCE 34


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

CHAPTER 5

IMPLEMENTATION AND TESTING

5.1 EXPERIMENTAL DATA COLLECTION


The data is being collected from the accelerometer contains the following
attributes: time, acceleration along all the three axes (x, y and z). The data is collected
every 20th second so there will be 20 samples per second. We used Naïve Bayes, Decision
Tree and Multilayer Perceptron to analyze raw dataset and the compare their accuracy
after successful training and testing of those datasets.

Importing Raw Data

The raw data is first converted into CSV file before they are analyzed and the results
are produced. To do so we are using Python inbuilt libraries like pandas and NumPy.

Fig 5.1 Importing Raw Data

Fig 5.2 Count Plot for Activity

Dept of CSE, NHCE 35


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Splitting the data into training and testing set

The splitting of data into training and testing set is one of the most key elements
in machine learning. Here we are making the use of inbuilt Python libraries and function
to achieve our task. Here we need to determine the test size sample before splitting the
data. In our project we have taken test size as 20 percent of the original data.

Fig 5.3 Training Data

Fig 5.4 Testing Data

5.2 MODEL BUILDING


Using Naïve Bayes

A model in machine learning is basically a mathematical representation of a real-


world process. In order to build the model, we need to provide a training data so that the
machine learning algorithm can learn from it.

Below is the Python code for building a model using Naïve Bayes:

Dept of CSE, NHCE 36


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Fig 5.5 Model Building Using Naïve Bayes

Using Decision Tree

Below is the Python code for building a model using Decision Tree:

Fig 5.6 Model Building Using Decision Tree

Using Multilayer Perceptron

Below is the Python code for building a model using Multilayer Perceptron:

Fig 5.7 Model Building Using Multilayer Perceptron

5.3 PERFORMANCE EVALUATION


The comprehensive evaluation of machine learning model is primarily a statistical
analysis using the analysis of available data and confusion matrix is the major method
through which the performance of an algorithm can be represented evidently identifying
the types of errors (false positive or false negative) associated with it. Through confusion
matrix various other matrices such as F1 score, Precision, Recall and Accuracy can be
evaluated.

Dept of CSE, NHCE 37


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

5.3 CONFUSION MATRIX


A confusion matrix is basically a summary of prediction results on a classification
problem. The number of correct and incorrect predictions is summarized with count
values and broken down by each class. This is the key to the confusion matrix. It shows
the ways in which your classification model is confused when it makes predictions. It not
only gives us insight of the errors being made by a classifier but more importantly the
types of errors that are being made.

Fig 5.8 Confusion Matrix

Here,

Class 1: Positive and Class 2: Negative

Definition of the Terms:

• True Positive (TP): Observation is positive, and is predicted to be positive.


• False Negative (FN): Observation is positive, but is predicted negative.
• True Negative (TN): Observation is negative, and is predicted to be negative.
• False Positive (FP): Observation is negative, but is predicted positive.

Fig 5.9 Confusion Matrix of Naïve Bayes Model

Dept of CSE, NHCE 38


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

The above confusion matrix diagram shows that through Multilayer Perceptron
classifier we got an accuracy of 97% while in case of Naïve Bayes it is 81.3% and in case of
Decision Tree it is 86.28%. The classes that are being correctly classified is inside the
diagonal while the classes that are being misclassified are outside of the diagonal.

Accuracy:

Classification Rate or Accuracy is given by the relation:

Recall:

It is defined as the ratio of the total number of correctly classified positive


examples divide to the total number of positive examples. High Recall indicates the class
is correctly recognized. The relation representing recall is given by:

Precision:

It is defined as the ratio of the total number of correctly classified positive


examples by the total number of predicted positive examples. High Precision indicates
an example labeled as positive is indeed positive. It is given by the relation:

F1 score:

It is the harmonic mean of precision and recall taking both metrics into account in
the following equation:

Dept of CSE, NHCE 39


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

CHAPTER 6

SNAPSHOT

Fig 6.1 Importing Raw Data

Fig 6.2 Pie Chart for Activity

Fig 6.3 Confusion Matrix of Naïve Bayes Model

Dept of CSE, NHCE 40


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Fig 6.4 Classification Report for Naïve Bayes Model

Fig 6.5 Confusion Matrix of Decision Tree Model

Fig 6.6 Classification Report for Decision Tree Model

Dept of CSE, NHCE 41


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Fig 6.7 Confusion Matrix of Multilayer Perceptron Model

Fig 6.8 Classification Report for Multilayer Perceptron Model

Fig 6.9 Prediction of classifier algorithms

Dept of CSE, NHCE 42


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

Fig 6.10 Accuracy Result of classifier algorithms

Dept of CSE, NHCE 43


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

CHAPTER 6

CONCLUSION AND FUTURE ENHANCEMENT

6.1 CONCLUSION
Human Activity Detection has wide-range of applications in medical research and
human health domain. Through this project, we can recognize six different human activities:
walking, laying, sitting, standing, walking upstairs and walking downstairs. The system
collected time series data using the inbuilt smartphone sensors like accelerometer,
gyroscope and orientation sensor. The activity data were trained and tested using 3
classifiers algorithms like Naïve Bayes, Decision Tree and Multilayer Perceptron.

The best out of the three classifiers in terms of accuracy is being the Multilayer
Perceptron with an accuracy rate of 97.36% while the Decision Tree has the accuracy rate
of 86.28% and the Naïve Bayes being the lowest with an accuracy rate of 81.30%. Hence
these results are enough to demonstrate that the Multilayer Perceptron is the optimal
choice for our problem.

6.2 FUTURE ENHANCEMENT


The future enhancements include:

• Considering more activities in order to have a better accuracy in our model.


• Implementing a real-time system on smartphone.
• Query strategy such as variance reduction and density-weighted methods may be
developed to enhance the performance of active learning schemes.

Dept of CSE, NHCE 44


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

REFERENCES
1 Z. Zhou, X. Chen, Y. Chung, Z. He, T.X. Han, J.M. Keller, "Activity Analysis,
Summarization, and Visualization for Indoor Human Activity Monitoring, " IEEE
Transactions on Circuits and Systems for Video Technology, 18(11), 1489-1498, 2008.

2 J. Cho, J.T. Kim, T. Kim, "Smart Phone-based Human Activity Classification and Energy
Expenditure Generation in Building Environments, " 7th International Symposium on
Sustainable Healthy Buildings (SHB'2012), 2012.

3 M. Boyle, A. Klaustner, D. Starboinski, A. Trachtenberg, and H. Wu, "Poster: gait-


based smartphone user identification, " the 9th International Conference on Mobile
Systems, Applications, and Services (MOBISYS'II), pp. 395,396, 2011.

4 S. Chen, Y. Huang, "Recognizing human activities from multi-modal sensors, " IEEE
International Conference on Intelligence and Security Informatics (ISI'09), pp.
220,222, 2009.

5 G. Fortino, R. Giannantonio, R. Gravina, P. Kuryloski, R. Jafari, "Enabling Effective


Programming and Flexible Management of Efficient Body Sensor Network
Applications, " IEEE Transactions on Human-Machine Systems 43(1), 115-\33, 20\3.

6 S. Fang, Y. Liang, K. Chiu, "Developing a mobile phone-based fall detection system on


Android platform, " Computing, Communications and Applications Conference
(ComComAp), pp. 143,146, 2012.

Dept of CSE, NHCE 45


Human Activity Detection based on Multiple Smartphone Sensors using Machine Learning

7 E. Kim, s. Helal, D. Cook, "Human Activity Recognition and Pattern Discovery, " IEEE
Pervasive Computing, 9(1), 48-53, 2010. [2] Z. Zhou, X. Chen, Y. Chung, Z. He, T.X.
Han, J.M. Keller, "Activity Analysis, Summarization, and Visualization for Indoor
Human Activity Monitoring, " IEEE Transactions on Circuits and Systems for Video
Technology, 18(11), 1489-1498, 2008.

8 M. Zhang, A.A. Sawchuk, "USC-HAD: a daily activity dataset for ubiquitous activity
recognition using wearable sensors, " ACM Conference on Ubiquitous Computing
(UbiComp'12), pp. 1036,1043, 2012.

9 B. Dong, S. Biswas, "Wearable networked sensing for human mobility and activity
analytics: A systems study, " Fourth International Conference on Communication
Systems and Networks (COMSNETS), pp. 1,6, 2012.

10 J. Yin, Q. Yang, J.J. Pan, "Sensor-Based Abnormal Human-Activity Detection, " IEEE
Transactions on Knowledge and Data Engineering, 20(8), 1082-1090, 2008.

11 D. Curone, G.M. Bertolotti, A. Cristiani, E.L. Secco, G. Magenes, "A Real-Time and
Self-Calibrating Algorithm Based on Triaxial Accelerometer Signals for the Detection
of Human Posture and Activity, " IEEE Transactions on Information Technology in
Biomedicine, 14(4), 1098-1105, 2010.

12 C.V.c. Bouten, K. T.M. Koekkoek, M. Verduin, R. Kodde, J.D. Janssen, "A triaxial
accelerometer and portable data processing unit for the assessment of daily physical
activity, " IEEE Transactions on Biomedical Engineering, 44(3), \36-147, 1997.

13 T. AI-Ani, Quynh Trang Le Ba, E. Monacelli, "On-line Automatic Detection of Human


Activity in Home Using Wavelet and Hidden Markov Models Scilab Toolkits, " IEEE
International Conference on Control Applications (CCA'07), pp. 485,490, 2007.

Dept of CSE, NHCE 46

You might also like