A banner image is titled 'Human activity recognition with smart phone.' The background image shows a gathering of people using smartphone.

Human Activity Recognition With Smartphone

By Manas Kochar Category Machine Learning Reading time 7 mins Last Updated on Mar 28, 2023

How Do Smartphones Conduct Human Activity Recognition Using Machine Learning?

Smartphones are becoming smarter every day. Starting from being a reliable navigator to a document scanner, and even your entertainment system, everything is now possible with a smartphone. In fact, you can do everything without touching your phone, just by giving commands. But all of these advancements have been left behind with the introduction of human activity recognition using machine learning in smartphones.

Although they can't provide you with accuracy like dedicated medical devices still for regular and precautionary health tracking, smartphones have become handy.

A model for human activity recognition using machine learning functions is an exciting development in the field. This model is useful in health monitoring, biometrics, etc. These machine learning algorithms can be helpful in running health research on humans in their daily lives.

Built-in sensors in smart devices can help develop models that identify human activity. Inertial sensors like accelerometers and gyroscopes enable regular reviews of our daily activities.

Since the model can calculate speed and angular velocity, these sensors have various purposes in the daily well-being of an individual.

What is Human Activity Recognition (HAR)?

Human Action Recognition or Activity Recognition is an area of study where daily human tasks are detected using sensors. The sensors accurately record the activities it detects and try to figure out the body movements based on the recording.

Sensors like IoT, edge computing, and cloud have been modified to a much-advanced level over the years. These sensors are utilized in smartphones and smartwatches to capture human movements.

Nowadays, HAR is not only being utilized in sports activity calculations but also in daily routine tasks. Activities like sleeping, eating, drinking, brushing teeth, etc., have been included to provide a precise health check for individuals.

Advantages of (HAR) Human Activity Recognition with smartphone

  • Continuous Health Monitoring:

Wearable with dedicated sensors can detect heart rate, BP, BMR, etc. Even ECG can also be monitored with top-end wearable devices, like Apple watches and Pixel watches.

  • Activity Pattern detection:

As continuous monitoring is now possible, so the dedicated apps for HAR collect and store data for time-bound analysis. For such analysis, both the regular and irregular patterns of health conditions get easily detected.

  • Unusual Activity Detection:

Due to the live as well as historic pattern identification, any kind of unusual health condition or activity gets instantly detected. As a result, the concerned person can be taken care of with the required precautions with immediate effect.

Workings process of human activity recognition using machine learning

In this case study, we design a model by which a smartphone can precisely detect its owner's activity.

A mobile screen displays a fingerprint scanner surrounded by the logos of messages, Wi-Fi, alerts, weather, browsers, etc.

Most smartphones have two sensors:

  1. An accelerometer
  2. A gyroscope

These are IoT sensors for collecting data and detecting changes in the environment.

An IoT sensor easily performs human activity recognition using machine learning algorithms. The accelerometer collects data on mobile movements.

For example, the moving landscapes and portraits when playing mobile games.

The gyroscope measures the rotational movement.

For example, whether a user walks normally, goes upstairs, downstairs, lies down or sits- these can be tracked with an accelerometer and gyroscope.

Some accelerometers and gyroscopes measure heart rate, calories burned, etc., by reading human activities. However, these are done on the basis of step walks and other measures. It displays how much work has been done in a day by humans. This is also the area of the Artificial Internet of Things (AIoT).

Explanation of Human Activity Recognition

With the help of sensor data, we collect body movement data captured by the smartphone. Movements often include indoor activities such as walking upstairs, downstairs, lying down, sitting, and standing. The data is recorded for data prediction.

Human Activity Recognition with Smartphones (Data Set)

  • The experiments are carried out with 30 volunteers within an age bracket of 19-48 years. Each person performs six activities wearing a smartphone (e.g., Samsung Galaxy S II) on the waist. It records activities such as Walking, WALKING_UPSTAIRS, WALKING_DOWNSTAIRS, SITTING, STANDING, and LYING.

  • We can capture 3-axial linear velocity and 3-axial angular velocity. We can calculate them at a constant rate of 50Hz using the embedded accelerometer and gyroscope.

  • Human activity recognition experiments are video-recorded to label the data manually. The obtained dataset is randomly partitioned into two sets. We select 70% of the volunteers for generating the training data and 30% for the test data.

  • Data preprocessing is done on the sensor signals (accelerometer and gyroscope) by applying noise filters. Signals are then sampled in fixed-width sliding windows of 2.56 sec and 50% overlap (128 readings/window).

  • The sensor acceleration signal possesses gravitational and body motion components. Therefore, it is separated using a Butterworth low-pass filter into body motion and gravity.

  • The gravitational force has low-frequency components. Therefore, we use a filter with a 0.3 Hz cutoff frequency. From each window, a vector of features is obtained by calculating variables from the time and frequency domain.

Downloading the Human Activity Recognition Dataset:-

  1. There are "_train_" and "_test_" folders containing the split portions of the data for modeling (e.g., 70%/30%).
  2. There is a "_txt_" file containing a detailed technical description of the dataset and the contents of the unzipped files.
  3. There is a "_txt_" file containing a technical description of the engineered features.

The contents of the "_train_" and "_test_" folders are similar (e.g., file names), although they have differences in the specific data.

 # Load set data and process it. 
 # Important libraries to import for data processing. 

Start with some necessary imports.

  1. import NumPy as np
  2. import pandas as pd
  3. from google.colab import files
  4. uploaded = files.upload()

(google.colab is used to fetch the data from the collaborator files.)

  1. train_data = pd.read_csv("train.csv")
  2. train_data.head()

We select the training data set for the modeling.

  1. train_data.Activity.value_counts()
  2. train_data.shape

The above function defines how many rows and columns the dataset has.


It describes that there are 8 rows and 563 columns with all the data features. For numeric data, the result's index will include count, mean, std, min, and max. As well as lower, '50', and upper percentiles.

By default, the lower percentile is 25, and the upper percentile is 75. The '50' percentile is the same as the median.

  1. uploaded = files.upload()
  2. test_data = pd.read_csv('test.csv')
  3. test_data.head()

Here we read the CSV file to analyze the data set. The operation that is supposed to be programmed is also determined. test_data.head() shows the first 5 rows with their respective columns, so here we have 5 rows and 563 columns.

Shuffling Data

  1. from sklearn.utils import shuffle
  2. test = shuffle(test)
  3. train_data = shuffle(train_data)

Shuffling data serves the purpose of reducing variance and making sure that models remain general and overfit less.

When your data is sorted by class/target, it's evident that you'd shuffle it. Here, you will want to shuffle to ensure that your training/test/validation sets represent the data's overall distribution.

Separating data inputs and output labels

  1. trainData = train_data.drop('Activity' , axis=1).values
  2. trainLabel = train_data.Activity.values
  3. testData = test_data.drop('Activity' , axis=1).values
  4. testLabel = test_data.Activity.values
  5. print(testLabel)

By using the above code, we separate the input and output. It performs human activity recognition using machine learning captured by the IoT device. The human activities of walking, standing, walking upstairs, walking downstairs, sitting, and lying are separated to optimize the result.

Encoding labels

  1. from sklearn import preprocessing
  2. encoder = preprocessing.LabelEncoder()

Encoding test labels

  1. encoder.fit(testLabel)
  2. testLabelE = encoder.transform(testLabel)

Encoding train labels

  1. encoder.fit(trainLabel)
  2. trainLabelE = encoder.transform(trainLabel)

Hold the label for each class and encode explicit features using a one-hot or ordinal encoding scheme. We may use this to transform non-numerical labels (as long as they are hashable and comparable) to numerical labels.

Applying supervised neural network using multi-layer perceptron

  1. import sklearn.neural_network as nn
  2. mlpSGD = nn.MLPClassifier(hidden_layer_sizes=(90,) , max_iter=1000 , alpha=1e-4 , solver='sgd' , verbose=10 , tol=1e-19 , random_state=1 , learning_rate_init=.001)
  3. mlpADAM = nn.MLPClassifier(hidden_layer_sizes=(90,) , max_iter=1000 , alpha=1e-4 , solver='adam' , verbose=10 , tol=1e-19 , random_state=1 , learning_rate_init=.001)
  4. nnModelSGD = mlpSGD.fit(trainData , trainLabelE)
  5. y_pred = mlpSGD.predict(testData).reshape(-1,1)
  6. #print(y_pred)
  7. from sklearn.metrics import classification_report
  8. print(classification_report(testLabelE, y_pred))
  9. import matplotlib.pyplot as plt
  10. import seaborn as sns
  11. fig = plt.figure(figsize=(32,24))
  12. ax1 = fig.add_subplot(221)
  13. ax1 = sns.stripplot(x='Activity', y=sub_01.iloc[:,0], data=sub_01, jitter=True)
  14. ax2 = fig.add_subplot(222)
  15. ax2 = sns.stripplot(x='Activity', y=sub_01.iloc[:,1], data=sub_01, jitter=True)
  16. plt.show()
Two scattered plots show the use multi-layer perceptron with supervised neural network.

Final Remarks

This examination concludes that smartphones are suitable for human activity recognition using machine learning activities. In the future, collecting and processing the data should be improved, providing a viable method for analyzing a human being's health.

Human activity recognition with smartphones has become a great step towards the positive well-being of individuals. It's slowly blending itself into the healthcare systems for preventing obesity, providing elderly care, etc.

With the rise in new AI/ML technologies, we may be able to extend the advantages of HAR, Eg. Human Activity Recognition using deep learning is one major topic of interest among researchers.

If you want to study more projects like human activity recognition, you can join Artificial Intelligence and Machine Learning Program. Also, keep following us on Twitter, Facebook, LinkedIn, and Youtube for the latest updates on data science, AI, and full-stack development.