This example uses machine learning to recognize human activities from smartphone sensor data.
Human activity recognition classifies a person’s movement from accelerometer and gyroscope measurements. In this example, the model distinguishes six daily activities: laying, sitting, standing, walking, walking downstairs, and walking upstairs.
The original UCI data set contains 561 engineered sensor features. We use the complete data set and let Neural Designer select a compact subset of relevant inputs before training the classifier.
We have built this model using the data science and machine learning platform Neural Designer. Download Neural Designer to explore the workflow.
- Application type
- Data set
- Input selection
- Neural network
- Training strategy
- Testing analysis
- Model deployment
- References
1. Application type
This is a classification project: the model maps smartphone movement features to the activity performed by the user.
The target variable is activity. It has six possible classes, so the neural network outputs the probability of each activity and assigns the sample to the most probable one.
2. Data set
The first step is to prepare the data set, which is the source of information for the classification problem. It is composed of:
- Data source.
- Variables.
- Instances.
Data source
The file UCI_HAR.csv contains the data for this example.
The original data set was recorded with a smartphone worn on the waist by 30 volunteers. The phone measured acceleration and angular velocity while each person performed daily activities.
The raw signals were divided into fixed-width time windows and transformed into time- and frequency-domain features. The resulting file contains 10,299 samples, 561 input variables, and one categorical target.
Variables
This problem has the following variable groups:
| Variable group | Role | Description |
|---|---|---|
| time-domain signals | Input | Statistics calculated from body acceleration, gravity acceleration, gyroscope, jerk, and magnitude signals over each time window. |
| frequency-domain signals | Input | Frequency features obtained with a Fast Fourier Transform, including energy and frequency-band measurements. |
| angle signals | Input | Angles between averaged body or jerk vectors and the gravity direction. |
| activity | Target | Observed activity: laying, sitting, standing, walking, walking downstairs, or walking upstairs. |
The input values are normalized, as provided by the UCI data set. Metadata columns such as subject and original partition have been removed, so Neural Designer reads only the 561 candidate inputs and the target.
Instances
They are divided randomly into training, selection, and testing subsets, containing 60%, 20%, and 20% of the instances, respectively. More specifically, 6,181 samples are used here for training, 2,059 for validation, and 2,059 for testing.
Variables distribution
Once we establish the data set information, we perform analytics to check the data quality.
The next figure depicts the distribution of the target classes.

The six activities are represented with similar frequencies. Laying is the most frequent class with 1,944 samples, while walking downstairs is the least frequent with 1,406 samples.
Inputs-targets correlations
The next figure depicts the ten greatest inputs-targets correlations calculated by Neural Designer from the complete data set.

Acceleration statistics and frequency-domain energy measurements show the strongest individual relationships with activity. However, the target depends on several sensor features simultaneously, so correlation alone does not determine the best subset.
3. Input selection
Working with all 561 inputs would make the model unnecessarily complex because many sensor features carry overlapping information. Input selection searches for a smaller subset that preserves predictive performance.
We use the growing inputs algorithm. It starts from a small set and progressively adds the variables that most improve the selection error. The model is retrained for each candidate subset, so the decision is based on its ability to generalize rather than on correlation alone.
For this example, the search uses three trials, warm starts, and a maximum of 50 inputs. The minimum selection error is obtained with the 50-input subset:
| Growing inputs result | Value |
|---|---|
| Selected inputs | 50 of 561 |
| Training error | 0.1047 cross entropy |
| Selection error | 0.1171 cross entropy |
| Trials per subset | 3 |
| Stopping criterion | Maximum inputs reached |
| Elapsed time | 3 minutes 41 seconds |
The procedure reduces the number of model inputs by about 91%. Since the stopping criterion is the configured 50-input limit, this result should be interpreted as the best subset found within that search range.
4. Neural network
The neural network outputs the activity class as a function of the selected smartphone sensor features.
The final classification model contains:
- A scaling layer for the 50 selected inputs.
- A classification layer with six softmax neurons, one for each activity.
The softmax layer returns six probabilities whose sum is one. The activity with the greatest probability is taken as the model prediction.
5. Training strategy
The next step is selecting an appropriate training strategy to define what the neural network will learn. A general training strategy is composed of two concepts:
- A loss index.
- An optimization algorithm.
The loss index chosen is the cross entropy, which is the standard loss for classification problems.
The optimization algorithm chosen is the quasi-Newton method.
Once the strategy has been set, we can train the neural network. The following chart shows how the training and selection errors decrease during the training process.

The final training error is 0.109 cross entropy, and the final selection error is 0.124 cross entropy. Training stopped after 248 epochs because the minimum loss decrease criterion was reached.
6. Testing analysis
The objective of the testing analysis is to validate the generalization performance of the trained neural network. The testing compares the predicted activities to the observed activities using an independent testing set.
For classification problems, a standard testing technique is the confusion matrix. Rows represent the real activity, and columns represent the predicted activity.
| Real / predicted | laying | sitting | standing | walking | walking downstairs | walking upstairs | Total |
|---|---|---|---|---|---|---|---|
| laying | 375 | 0 | 0 | 0 | 0 | 0 | 375 |
| sitting | 0 | 345 | 53 | 0 | 0 | 0 | 398 |
| standing | 0 | 39 | 325 | 0 | 0 | 0 | 364 |
| walking | 0 | 0 | 0 | 323 | 0 | 6 | 329 |
| walking downstairs | 0 | 0 | 0 | 0 | 283 | 11 | 294 |
| walking upstairs | 0 | 0 | 0 | 7 | 3 | 289 | 299 |
| Total | 375 | 384 | 378 | 330 | 286 | 306 | 2059 |
The model correctly classifies 1,940 of 2,059 testing samples, which is an accuracy of 94.2%. Most errors occur between sitting and standing, two static activities with very similar movement patterns.
7. Model deployment
We can plot a directional output of the neural network to see how the output probabilities vary with a given input while all other inputs remain fixed.
The next plots show how the laying and sitting probabilities change with tGravityAcc-mean()-X, one of the inputs retained by the growing inputs algorithm.

The exported model can then be used to classify new smartphone windows after calculating the same sensor features selected during model development.
References
- UCI Machine Learning Repository. Human Activity Recognition Using Smartphones Data Set.
- Anguita, D., Ghio, A., Oneto, L., Parra, X., and Reyes-Ortiz, J. L. A Public Domain Dataset for Human Activity Recognition Using Smartphones. ESANN 2013.




