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:
Free Step-by-step Guide To Become A Data Scientist
Subscribe and get this detailed guide absolutely FREE
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
data_dir = ‘/home/prateek/Documents/LearnersHeaven/content_preparation/course_content/development/case_studies/ML/data/HumanActivityRecogmnition/Activity Recognition from Single Chest-Mounted Accelerometer_’
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 axes. 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 whereas 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 modeling is performed.
Defining the variables
Train data split
KNeighborsClassifier(algorithm=’auto’, leaf_size=30, metric=’minkowski’,
metric_params=None, n_jobs=None, n_neighbors=5, p=2,
Misclassified samples: 160845
How to decide the value of n-neighbors
Choosing a large value of K will lead to greater amount of execution time & under-fitting. Selecting the small value of K will lead to over-fitting. There is no such guaranteed way to find the best value of K.
Accuracy is 65.9405711082933 % for K-Value: 1
Accuracy is 65.22024187423993 % for K-Value: 2
Accuracy is 69.8665730215597 % for K-Value: 3
Accuracy is 71.19980486758502 % for K-Value: 4
Accuracy is 72.17546694252762 % for K-Value: 5
Accuracy is 72.69357118267888 % for K-Value: 6
Accuracy is 73.21721109417734 % for K-Value: 7
Accuracy is 73.55160024149366 % for K-Value: 8
Accuracy is 73.79499679104052 % for K-Value: 9
Accuracy is 73.97490610982426 % for K-Value: 10
Accuracy is 74.16242697671039 % for K-Value: 11
Accuracy is 74.28075195175663 % for K-Value: 12
Accuracy is 74.40409362896125 % for K-Value: 13
Accuracy is 74.47311653107155 % for K-Value: 14
Accuracy is 74.55113489912104 % for K-Value: 15
Accuracy is 74.62794233906332 % for K-Value: 16
Accuracy is 74.69298647739284 % for K-Value: 17
Accuracy is 74.73761782762958 % for K-Value: 18
Accuracy is 74.81096547297987 % for K-Value: 19
Accuracy is 74.84400651133343 % for K-Value: 20
Accuracy is 74.84054671674143 % for K-Value: 21
Accuracy is 74.89538446102455 % for K-Value: 22
Accuracy is 74.9305013761333 % for K-Value: 23
Accuracy is 74.95420096908848 % for K-Value: 24
Accuracy is 74.97478674691084 % for K-Value: 25