Human Activity Recognition or HAR for short is the problem of predicting what a person is doing based on a trace of their movement using sensors.
Movements are often normal indoor activities such as standing, sitting, jumping, and going up stairs. Sensors are often located on the subject such as a smartphone or vest and often record accelerometer data in three dimensions (x, y, z).
The idea is that once the subject’s activity is recognized and known, an intelligent computer system can then offer assistance.
It is a challenging problem because there is no clear analytical way to relate the sensor data to specific actions in a general way. It is technically challenging because of the large volume of sensor data collected (e.g. tens or hundreds of observations per second) and the classical use of hand crafted features and heuristics from this data in developing predictive models
The dataset “Activity Recognition from Single Chest-Mounted Accelerometer Data Set” was collected and made available by Casale, Pujol et al. from the University of Barcelona in Spain. It is freely available from the UCI Machine Learning repository:
The dataset is comprised of un-calibrated accelerometer data from 15 different subjects, each performing 7 activities. Each subject wore a custom-developed chest-mounted accelerometer and data was collected at 52 Hz(52 observations per second).
Un-calibrated Accelerometer Data are collected from 15 participants performing 7 activities. The dataset provides challenges for identification and authentication of people using motion patterns.
Data Set Information:
— The dataset collects data from a wearable accelerometer mounted on the chest
— Sampling frequency of the accelerometer: 52 Hz
— Accelerometer Data are Un-calibrated
— Number of Participants: 15
— Number of Activities: 7
— Data Format: CSV
— Data are separated by participant
— Each file contains the following information
—- Sequential number, x acceleration, y acceleration, z acceleration, label
— Labels are codified by numbers
— 1: Working at Computer
— 2: Standing Up, Walking and Going updown stairs
— 3: Standing
— 4: Walking
— 5: Going UpDown Stairs
— 6: Walking and Talking with Someone
— 7: Talking while Standing
Import libraries and tools
[‘/home/ubuntu/Documents/LearnersHeaven/content_preparation/course_content/development/case_studies/ML/data/HumanActivityRecogmnition/Activity Recognition from Single Chest-Mounted Accelerometer_/4.csv’,
‘/home/ubuntu/Documents/LearnersHeaven/content_preparation/course_content/development/case_studies/ML/data/HumanActivityRecogmnition/Activity Recognition from Single Chest-Mounted Accelerometer_/2.csv’,
‘/home/ubuntu/Documents/LearnersHeaven/content_preparation/course_content/development/case_studies/ML/data/HumanActivityRecogmnition/Activity Recognition from Single Chest-Mounted Accelerometer_/8.csv’]
Load data set
Output: Loaded 15 subjects
Plot a subject
Running the example creates a line plot for each variable for the first loaded subject. We can see some very large movement in the beginning of the sequence that may be an outlier or unusual behaviour that could be removed. We can also see that the subject performed some actions multiple times. For example, a closer look at the plot of the class variable (bottom plot) suggests the subject performed activities in the following order, 1, 2, 0, 3, 0, 4, 3, 5, 3, 6, 7. Note that activity 3 was performed twice.
Plot Total Activity Durations
We can see that there is relatively fewer observations for activities 0 (no activity), 2 (standing up, walking and going up/down stairs), 5 (going up/down stairs) and 6 (walking and talking). We can also see that each subject spent a lot of time on activity 1 (standing Up, walking and going up/down stairs) and activity 7 (talking while standing).
Running the example creates a single figure with 15 plots, one for each subject, and 3 histograms on each plot for each of the 3 axis of accelerometer data. The three colors blue, orange and green represent the x, y and z axises. This plot suggests that the distribution of each axis of accelerometer is Gaussian or really close to Gaussian. This may help with simple outlier detection and removal along each axis of the accelerometer data. The plot really helps to show both the relationship between the distributions within a subject and differences in the distributions between the subjects.
Within each subject, a common pattern is for the x (blue) and z (green) are grouped together to the left and y data (orange) is separate to the right. The distribution of y is often sharper where as the distributions of x and z are flatter.
Across subjects, we can see a general clustering of values around 2,000 (whatever the units are), although with a lot of spread. This marked difference in distributions does suggest the need to at least standardize (shift to zero mean and unit variance) the data per axis and per subject before any cross-subject modelling is performed.
Defining the variables
Train Data split
LogisticRegression(C=1e-05, class_weight=None, dual=False, fit_intercept=True,
intercept_scaling=1, max_iter=100, multi_class=’warn’,
n_jobs=None, penalty=’l2′, random_state=None, solver=’warn’,
tol=0.0001, verbose=0, warm_start=False)
precision recall f1-score support
0 0.00 0.00 0.00 1096
1 0.35 0.70 0.47 182820
2 0.00 0.00 0.00 14466
3 0.00 0.00 0.00 64780
4 0.00 0.00 0.00 107026
5 0.00 0.00 0.00 15457
6 0.00 0.00 0.00 14426
7 0.34 0.41 0.37 177998
micro avg 0.35 0.35 0.35 578069
macro avg 0.09 0.14 0.11 578069
weighted avg 0.22 0.35 0.26 578069
cross validated accuracy: 0.24478941532945137