This example aims to build a machine learning model to identify human activity recognition from data collected by a smartphone.

For instance, activity recognition allows the development of health apps that track users’ behavior and provide recommendations. The experiments have been carried out with 30 volunteers within an age bracket of 19-48 years. Each person performed six activities (walking, walking_upstairs, walking_downstairs, sitting, standing, laying) wearing a smartphone on the waist. Using its embedded accelerometer and gyroscope, we captured 3-axial linear acceleration and 3-axial angular velocity at a constant rate of 50Hz. The experiments have been video-recorded to label the data manually.


  1. Application type.
  2. Data set.
  3. Neural network.
  4. Training strategy.
  5. Model selection.
  6. Testing analysis.
  7. Model deployment.

This example is solved with Neural Designer. To follow it step by step, you can use the free trial.

1. Application type

We want to predict a categorical variable (walking, walking_upstairs, walking_downstairs, sitting, standing, or laying). Therefore this is a classification project.

The goal is to model class membership probabilities conditioned on the input variables.

2. Data set

The file activity_recognition.csv contains 10299 samples, each of them with 561 inputs and one categorical target.

The input variables include time and frequency domain signals obtained from the smartphone sensors:

  • body_acceleration
  • gravity_acceleration
  • body_acceleration_jerk
  • body_angular_speed
  • body_angular_acceleration
  • body_acceleration_magnitude
  • gravity_acceleration_magnitude
  • body_acceleration_jerk_magnitude
  • body_angular_speed_magnitude
  • body_angular_acceleration_magnitude

The target variable has six different classes, each corresponding to one of the previously mentioned activities.

  • walking
  • walking_upstairs
  • walking_downstairs
  • sitting
  • standing
  • laying

The instances are split at random into training (60%), selection (20%), and testing (20%) subsets.

By calculating the data distribution, we can see the number of instances belonging to each class in the data set.

As we can see, the number of instances belonging to each category is similar. Therefore, this data set exhibits good balance.

We can also calculate the inputs-targets correlations to see which signals better define each activity. The following chart shows the 20 variables most correlated with the activity “standing.”

3. Neural network

A neural network will compose the person’s activity model, consisting of:

  • Scaling layer.
  • Perceptron layers.
  • Probabilistic layer.

The scaling layer uses the minimum and maximum scaling method.

The number of perceptron layers is 2:

  • The first perceptron layer has 561 inputs and 3 neurons.
  • The second perceptron layer has 3 inputs and 6 neurons (the number of classes).

The probabilistic layer uses the softmax probabilistic method.

4. Training strategy

The procedure used to carry out the learning process is called a training strategy. The training strategy is applied to the neural network to obtain the best possible performance. How the adjustment of the parameters in the neural network occurs determines the type of training.

We set the normalized squared error with L2 regularization as the loss index.

On the other hand, we use the quasi-Newton method as optimization algorithm.

The following chart shows how the training and selection errors decrease with the quasi-Newton method’s epochs during the training process.

As we can see, both curves’ behavior is similar along with the iterations, which means that no over-fitting has appeared. The final training and selection errors are training error = 0.008 NSE and selection error = 0.048 NSE. That indicates that the neural network has good generalization capabilities.

5. Model selection

The objective of model selection is to find the network architecture with the best generalization properties.

Since our final selection error so far is minimal (0.048 NSE), we don’t need to use these algorithms here.

6. Testing analysis

Once the model is trained, we perform a testing analysis to validate its prediction capacity. We use a subset of data that has not been used before, the testing instances.

The next table shows the confusion matrix for our problem. In the confusion matrix, the rows represent the actual classes, and the columns represent the predicted classes for the testing data.

Real STANDING 376 (18.3%) 18 (0.874%) 0 0 0 0
Real SITTING 18 (0.874%) 330 (16%) 1 (0.0486%) 0 0 0
Real LAYING 0 2 (0.0971%) 402 (19.5%) 0 0 0
Real WALKING 1 (0.0486%) 0 0 307 (14.9%) 0 0
Real WALKING_DOWNSTAIRS 0 0 0 0 279 (13.6%) 1 (0.0486%)
Real WALKING_UPSTAIRS 1 (0.0486%) 0 0 0 5 (0.243%) 318 (15.4%)

We can see that the number of instances the model can correctly predict is 2012, while it misclassifies only 47. This shows that our predictive model has a great classification accuracy.

7. Model deployment

The neural network is now ready to predict the activity of new people in the so-called model deployment phase.

The file implements the mathematical expression of the neural network in Python. This software can be embedded in any tool to predict new data.


  • UCI Machine Learning Repository Human Activity Recognition Using Smartphones Data Set.
  • Davide Anguita, Alessandro Ghio, Luca Oneto, Xavier Parra, and Jorge L. Reyes-Ortiz. A Public Domain Dataset for Human Activity Recognition Using Smartphones. 21st European Symposium on Artificial Neural Networks, Computational Intelligence, and Machine Learning, ESANN 2013. Bruges, Belgium 24-26 April 2013.

Related posts