Activity recognition using neural networks

The aim of this example is to predict human activity from data collected by a smartphone.

The experiments have been carried out with a group of 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.

The sensor signals (accelerometer and gyroscope) were pre-processed by applying noise filters and then sampled in fixed-width sliding windows of 2.56 sec and 50% overlap (128 readings/window). The sensor acceleration signal, which has gravitational and body motion components, was separated using a Butterworth low-pass filter into body acceleration and gravity. The gravitational force is assumed to have only low-frequency components, therefore a filter with 0.3 Hz cutoff frequency was used. From each window, a vector of features was obtained by calculating variables from the time and frequency domain.


  1. Data set
  2. Neural network
  3. Training strategy
  4. Testing analysis
  5. Model deployment

1. Data set

The data set contains 10299 instances, each of them with 561 inputs and one categorical target. The target has six different classes, each of them corresponding to one of the previously mentioned activities. The next chart shows the number of instances belonging to each class in the data set.

As we can see, the data set is well balanced since the number of instances belonging to each category is similar. The class with the largest number of instances (1906) is "STANDING" while the one that has the least number is "WALKING_UPSTAIRS" with 1544.

2. Neural network

The person's activity model will be a neural network composed by:

The scaling method that we use is the minimum-maximum.

The number of perceptron layers is 2, the number of inputs is 561, the number of outputs is 6 and the number of hidden neurons in the hidden layer is 3.

The probabilistic layer uses the softmax method.

3. Training strategy

The procedure used to carry out the learning process is called training (or learning) strategy. The training strategy is applied to the neural network in order to obtain the best possible performance. The type of training is determined by the way in which the adjustment of the parameters in the neural network takes place.

We will set the normalized squared error as error method for the loss index.

The following chart shows how the training loss and the selection loss decrease with the iterations of the quasi-Newton method during the training process.

Training chart.

As we can see, the behaviour of both curves is similar along the iterations which means that no over-fitting has appeared. The initial values of the training loss and the selection loss were 1.34579 and 1.33511, respectively. After 345 iterations, the training loss has decreased to 0.120 and the selection loss is 0.071.

The next table shows the training results by the quasi-Newton method. They include some final states from the neural network, the loss functional and the training algorithm.

Here the final parameters norm is not very big and the final loss and gradient norm are closed to zero. The total training time was around 108 seconds.

4. Testing analysis

Once the model is trained, we are going to test its prediction capacity with a subset of data that have not been used before neither for training nor for selection.

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

Confusion table
As we can see, the number of instances that the model can correctly predict is 2022 while it misclassifies only 37. This shows that our predictive model has a great classification accuracy.

5. Model deployment

The neural network is now ready to be used to predict activity of new people in what it is called the production phase.

By clicking on "write expression" task yo can obtain the mathematical expression that represents the model of this study. It can be implemented in other software to make predictions on new data.