1 Introduction

Smartphones have a wide range of sensors to record human interaction with our surroundings. Sensors like cameras, gyroscopes, accelerometers, magnetometers, temperature sensors, humidity sensors give the environment characteristics apart from leading telecommunications to the World. Even Wireless Sensor Networks have been widely applied in many fields, such as smart home, industrial control, telemedicine, and disaster relief [1, 2]. There is an increased necessity in developing control models for data planes in software WSN's [3]. The availability of continuous and instantaneous smartphone sensor data of a lifestyle enables us to understand human behaviour and different human interactions better. Activity recognition and Human behaviour cognition are the most sought-after applications of smartphone sensor data. Human activity recognition plays a vital role in medicine and surveillance, especially in monitoring the health of the elderly, where we can continuously track their health condition based on their current activity, which is diagnosed for the sensor data using machine learning techniques [4]. HAR has multiple other applications, including helping patients with cognitive disabilities, Human fall detection, Human–computer interaction, ambient intelligence [5]. Identification of Human activity monitoring with sensors has been used extensively for health monitoring, sports science, navigation, emergency response, etc. [6, 7]. A combination of the latest positioning technologies and sensors are captured human movements in natural environments [8]. Human activities are tracked with the help of individual/dedicated sensors attached to a person, which would be inconvenient to travel and work. The smartphone sensors are capable of detecting such abnormalities are comparatively less expensive than the individual sensors alone. Least Square-SVM (Support vector machines) algorithm is implemented for HAR [8]. Ling Pei et al. [8] found that the motion states recognized with an accuracy rate of up to 92.9% using LS-SVM classifiers. However, there is a need to develop an appropriate machine learning algorithm to understand complex HAR feature classification. In this method, tree-based and random forest methods evaluated for predicting HAR features. Tree-based methods do not assume the distribution of data. The simplest model is the Decision tree, and the superior version of the decision tree algorithm is that the Random forest algorithm implemented with smartphone sensors data. The decision tree is a straight learning algorithm with a pre-defined target, which is mostly used in classification, whereas Random forest algorithms in machine learning can handle large sets of attributes. Mobile phones with sensors like accelerometer and gyroscope acquired at the "Koneru Lakshmaiah Education Foundation," located in Guntur district, Andhra Pradesh, India. The outcome of this work would be useful for characterizing human activity recognition using smartphone sensors data.

2 Experimental Setup

In this paper, we have implemented machine learning algorithms on the data collected through mobile phone sensors. The mobile phone sensors record the data in the form of CSV files. The following testing human activity features investigated for applying the machine learning algorithms.

  1. 1.

    Run or Walk

  2. 2.

    Climbing up or down the stairs, the data collected from the sensors have been grouped in CSV format for simple classification. The grouped data has been given input to the proposed machine learning algorithm. The proposed algorithms were evaluated with the Gini index (Fig. 1).

Honor 10 Lite: mobile used to collect sensors data at KLEF University campus, Guntur, India, on 7 November 2019. The Sensors data was collected using Sensor Data Collector and Sensor Record CSV mobile applications available on Google play store. The sampling rate of data is 100 samples per second (Figs. 2, 3).

Fig. 1
figure 1

Flow chart showing the sequence of operation of HAR and machine learning algorithm

Fig. 2
figure 2

Sensor Record App downloaded from Google play store

Fig. 3
figure 3

Sensor Data Recorder App downloaded from Google play store. Sensor activation page (Top-Left), Recorded Acceleration data page (Second image from Left), and Recorded Gyroscope data (Third Image from Left Sensor activation page (Left) and Downloaded files or recorded data files page (Right)

2.1 Accelerometer

A mobile phone accelerometer is used to track the smartphone's orientation (Table 1). An accelerometer tests angular motion acceleration. The accelerometer is an electro-mechanical device that measures the acceleration force induced by motion or gravity. The detector of the accelerometer has been instrumental in running or walking. The accelerometer in the mobile we used is ST-LIS3DH.

Table 1 Accelerometer specifications

2.2 Gyroscope

The Gyroscope allows the accelerometer to know how your device is positioned, providing another level of accuracy. The gyroscopes inside the phones use Micro Electronics and Mechanical System (MEMS) based gyroscopes, a smaller version of the design embedded in a PCB (Table 2).

Table 2 Gyroscope specifications

The data collected from these sensors give us a total number of six parameters. The accelerometer provides acceleration values in x, y, z directions, and Gyroscope provides orientation values that represent roll, pitch, yaw axis as x, y, z-axis, respectively. These values give us the motion and orientation of a person when moving. These values are recorded w.r.t time in milliseconds in a CSV (comma-separated values) sheet. The collected data is raw data from sensors that vary spontaneously with time. This data is then processed, grouped, and classified according to the algorithm is implemented to the recorded smartphone dataset.

3 Algorithms Under Evaluation

In this paper, HAR analysis is conducted with three different machine learning algorithms (Support Vector Machines (SVM), Decision tree, and random forest methods) based on smartphone sensors.

3.1 Support Vector Machine (SVM)

The SVM method is one of the primitive supervised learning techniques. It increases the probability of geometric margin and decreases classification error, mainly the empirical one. This property made the algorithm exceptional, and it can be classified as a maximum Margin Classifier [9]. Hyperplanes play a significant role in SVM classifiers. A hyperplane is a plane or a subspace that has one dimension less than the space surrounding it. In any data provided, several hyperplanes exist, segregating the data points. SVM classifier recognizes the best hyperplane, which causes the largest segregation between the points of a decision function, thus classifying the data [10]. SVM classifiers are categorized into linear and non-linear classifiers.

3.2 Decision Tree

It is a tree-based algorithm that is, by definition, a supervised learning technique but can also be termed as a technique in unsupervised learning because this helps cluster the data. It uses a tree-like model of decisions [11]. A decision tree is drawn from up to down with its root at the top. Visualization of datasets and their corresponding outputs is interpreted easily using this algorithm. The non-linear relationship between parameters does not affect the accuracy of the result, which is one of this algorithm's significant features.

Learning the decision tree is constructing a decision tree from class-labeled tuples of learning. A decision tree is a flow-chart structure in which each inner (non-leaf) node denotes a check on an attribute, each branch represents a test result, and each leaf (or terminal) node has a class tag. The root node is the highest in a network. Tree models where the target variable can take a discreet set of values called classification trees; in these tree structures, the leaves represent the labels of the class, and the branches represent the conjuncts of the characteristics that lead to the labels of the class. The root node (decision node) is the highest in a network. Internal nodes (chance nodes) are the nodes that represent the available outcomes at that point. The top edge of the node is connected to the parent node. The bottom edge is connected to the child node. Leaf node (final nodes) is the resultant value. Branches of a decision tree are paths from root nodes to internal nodes, which define the classification decision rule splitting is a significant component in the decision tree algorithm where parent nodes split into the target variable's purer child nodes. A recursive process is applied, which occurs until a stopping criterion [12]. Characteristics such as entropy, information gain, Gini index took into account as a reference for splitting. Stopping is a feature that has to consider for avoiding overfitting, which later makes the model non-reliable. One must identify the correct input variables before training the set, split into child nodes (Fig. 4). This process is implemented to ensure that the model does not lack robustness. The minimum number of records in a leaf node, the minimum number of nodes prior splitting, and the depth of any leaf node from the root node prevent overfitting. Pruning is an alternative technique to avoid stopping. In this technique, the model is optimized by removing excess nodes that do not contain much-needed information. Pruning is again classified into two types pre-pruning and post-pruning. In pre- pruning, adjustments are made such that so that it avoids the generation of redundant nodes. Post-pruning occurs after the decision tree is made and then it removes branches with redundancy to improve accuracy when the algorithm is implemented on a validation dataset [13].

Fig. 4
figure 4

Decision tree classification using python

3.3 Random Forest

As its name implies, the Random Forest consists of a large number of individual decision-making trees that function as a group. That tree in the random forest spits out the class prediction, and the class with the most votes is our model prediction [3]. The low correlation between models is the solution to this classifier. Random forest classifier is an advancement to the decision trees. The random forest algorithm is implemented in two steps. First, a forest is created with random samples of data and then the classifier's prediction from the first step. As the name suggests, random data should select from the dataset, and a decision tree should construct for the selected set. A prediction result was obtained, and the voting mechanism was implemented in this context. The prediction result with the maximum number of votes considered as the final prediction. The redundant values are removed in the voting process. By sampling N samples randomly, these samples are used to create a tree, then a forest. For K input variables, a value k is selected, variables selected at random such that k<<K (Fig. 5). K remains constant throughout the process, even though the size of the forest grows [12]. This algorithm is more accurate and efficient than decision trees. This classifier avoids overfitting, which is one of the significant disadvantages raised when the decision tree is used.

Fig. 5
figure 5

Random forests classification using python

4 Accuracy Computations

The proposed machine learning algorithms performance evaluated using the Gini Index parameter.

4.1 Gini Index

The Gini Index is determined by subtracting from one the sum of the squared probabilities of each group [13] Knowledge Gain multiplies the likelihood of the class by the log (base = 2) of the group's probability. Knowledge Gain prefers smaller partitions with many different values. Gini index value ranges between 0 and 1. Gini index is faster to compute as compared to Entropy [3].

4.1.1 Computations Involved

$${\text{Gini}} = {\text{ 1}} - \sum i = {\text{1}}({\text{pi}})^{{\text{2}}}$$
(1)
$$\text{Entropy } = \sum i = \text{1} - {\text{pi}*\text{log}}^{{\text{2}}} (\text{pi})$$
(2)
$${\text{Information}}\;{\text{Gain}} = {\text{Entropy}}\;{\text{ }}\left( {{\text{parent node}}} \right) - \left[ {{\text{Average }}\;{\text{Entropy }}\;\left( {{\text{Child}}\;{\text{ node}}} \right)} \right]$$
(3)

where, pi indicates the probability of each class.

Entropy determines how the decision tree chooses to divide the data.

$${\text{Gain }}\;\left( {{\text{T}},{\text{ X}}} \right) = {\text{Entropy}}\;{\text{ }}\left( {\text{T}} \right){-}{\text{Entropy }}\;\left( {{\text{T}},{\text{ X}}} \right)$$
(4)

4.2 Confusion Matrix

A confusion Matrix is a matrix that has four values, True positive (TP), true negatives (TN), false positives (FP), and false negatives (FN). The four essential factors are calculated using this matrix: accuracy, precision, recall, and f1-score [3]

$${\text{Accuracy}}~ = ~(TP + TN)/(TP + TF + FP + FN)$$
(5)
$${\text{Recall }} = ~\left( {{\text{TP}}} \right)/\left( {{\text{TP}} + {\text{FN}}} \right)$$
(6)
$${\text{Precision }} = ~\left( {{\text{TP}}} \right)/\left( {{\text{TP}} + {\text{FP}}} \right)$$
(7)
$${\text{F1}} - {\text{score}} = {\text{2}}*\left( {{\text{Recall}}*{\text{Precision}}} \right)/{\text{ }}\left( {{\text{Recall}} + {\text{Precision}}} \right)$$
(8)

5 Results and Discussions

Initially collected sensor data from the accelerometer and Gyroscope is plotted against time in Fig. 6. the change in the type of motion is noticed, running, and walking for our test data from Fig. 7. This behavior change was investigated to classify smartphone sensors data points.

Fig. 6
figure 6

Accelerometer and gyroscope data change plotted together against time

Fig. 7
figure 7

Identified human behaviour change

The accelerometer data and gyroscope data individually depict the change in human behaviours (Fig. 8). The raw data collected is enough to recognize the type of human motion, different plots of each sensor's individual axis data (Fig. 9). In general, this unprocessed data used to identify the feature through human involvement and differentiate the data for different human behaviours like walking, running, climbing up-stairs, getting down-stairs, standing still. Here to distinguish the two activities, we denote walking as 0 and running as 1.

Fig. 8
figure 8

Identified human behaviour change for individual sensors

Fig. 9
figure 9

Individual sensor axes

Figures 10 and 11 Show the accuracy of activity recognition between walking and running when the Decision tree algorithm is used with a 20% training set. The point of change activity is identified for each sensor (Fig. 11). Similarly, from Figs. 12 and 13, It is observed that the accuracy of activity recognition between walking and running with a 20% training set using random forest algorithm. Figures 13 and 14 show the accuracy of activity recognition between walking and running when the SVM algorithm is used with a 20% training set (Fig. 15).

Fig. 10
figure 10

Plot b/w activity and test inputs for decision tree algorithm

Fig. 11
figure 11

Grid plot of decision tree algorithm

Fig. 12
figure 12

Plot b/w activity and test inputs for the Random forest algorithm

Fig. 13
figure 13

Grid plot of Random forest algorithm

Fig. 14
figure 14

Plot b/w activity and test inputs for SVM algorithm

Fig. 15
figure 15

Grid plot of SVM algorithm

We have tested and placed two features in Run or Walk, climbing up or down the stairs. The training set percent arranged in 4 sets as 20%, 40%, 60%, and 80% each for both accelerometer and gyroscope X, Y, Z in SVM, Decision tree, and Random forest algorithms. Table 3 shows the accuracy results of Run or Walk data. The accuracy, when compared between these algorithms SVM, is proven to be the least efficient one. The accuracy of the Decision tree algorithm is a little better than that of the random forest algorithm. Random forest helps in reducing the variance part of the error more than the bias part. Hence, the decision tree may be more reliable on a given training data set. But in terms of accuracy, Random forest is often better on a random collection of validation data. As we can see, if we provide the algorithm with more testing attributes, the result gets more accurate in the decision tree algorithm, excluding the first case, which is a result of the large quantity of test data set available. The highest accuracy of 83.79%, 90.016%, and 89.19% are achieved for Gyroscope and 84.39%, 95.48%, and 94.59% for Accelerometer Run or Walk data attributes using SVM, Decision Tree and Random Forest algorithms, respectively. Total accuracy of 87.56%, 95.58%, and 94.43% for SVM, decision tree, and Random Forest. Climbing up and getting down the stairs are very similar activities (Table 4).

Table 3 Human activity recognition of Run or Walk features
Table 4 Human activity recognition of Climbing up or getting downstairs features

Here, we achieve the highest accuracy of 65.21%, 67.11%, 67.25% for Gyroscope and 57.13%, 59.51%, and 60.25% for SVM, Decision Tree, and Random Forest respectively. Here the accuracy of prediction for the random forest when using only accelerometer data can be observed to grow with an increase in training data set, but the same is not valid for other cases.

6 Conclusion

The results indicate that the SVM's accuracy,decision tree algorithm, and random forest algorithm differ in nominal rates in running or walking as the features do not change much, and the data has many similar features. But on the contrary, climbing up and down has many spike changes in the dataset, which makes the data challenging for accuracy tests. In this case, the random Forest algorithm is proven to be more efficient, and the rate of efficiency is directly proportional to the increase in training to the testing ratio, which indicates that the algorithm works more efficiently upon more training. The accuracy of algorithms is determined using the Gini Index analysis. It is found that the test data set results are closely following the actual sensors data for random forest and Decision tree methods than SVM method. But the random forest is often better on a random collection of validation data, which is generally the case. As the data in walking or running was easy to differentiate, both the algorithms(DT and RF) have proven to be robust, and in the case of climbing up or down, as the data changes unevenly, the differentiation seemed entirely less accurate, and we believe more training on such data sets can improve the prediction accuracy. The accuracy of the algorithm is also dependent on the type of activities under consideration. In the future, more features and more training data sets will be investigated.