Daylight savings is here as am I. With the most difficult semester of my Masters reaching its conclusion, blogging about a Machine Learning Project that involved Human Activity Recognition enticed me.
The project was essentially an improvisation upon the existing research carried out by Davide Anguita, Alessandro Ghio, Luca Oneto , Xavier Parra and Jorge.L.Reyes-Ortiz on the Human Activity Recognition(HAR) dataset. The HAR dataset was retrieved from the UCI Machine Learning repository. While the experiments carried out by the entities named above made use of only a single algorithm, Support Vector Machine with Multi Class classifiers, three additional algorithms were deployed by us, namely K Nearest Neighbour Classification, Multi Class Logistic Regression with Elastic Net, and Random Forest in addition to the Support Vector Machine(SVM) algorithm. We compare the performances of each of these algorithms on the dataset and reach closure on the best algorithm to deploy on the HAR dataset.
A brief description of the dataset is required of us. 30 Entities aged 19-48 years were asked to perform Activities of Daily Living(ADL) i.e standing, sitting, laying down, walking, walking downstairs, and upstairs by sporting a waist mounted Samsung Galaxy S2 smartphone. To describe each activity, a total of 561 features were extracted.(A Public Domain Dataset for Human Activity
Recognition Using Smartphones, pg. 438).
The dataset retrieved follows a 70-30 split with the training set consisting of 7352 instances and the test set consisting of 2947 instances.
A quick note before we begin exploring the algorithms used on the HAR dataset. Running the dataset on a GPU based system is highly recommended than traditional CPU based systems. Certain algorithms such as Support Vector Machine(SVM) and Random Forest take copious amounts of time(approximately an hour) to execute on a traditional CPU based system. The other algorithms, Multi Class Logistic Regression , and K Nearest Neighbours gobble up considerable amounts of time as well when run upon traditional CPU based systems. Colab, a gem of a service provided by Google allows the use of a GPU based hardware accelerator free of cost. We now proceed to explore the algorithms run on the HAR dataset:
- K Nearest Neighbours
- Using a 10 Fold Cross Validation technique, the number of neighbours (k values) were varied from 1 to 50.
- The best values of k were chosen based on the highest model f1-score since accuracy is a rather biased metric in assessing model performance.
- A plot of the best k values with respect to the accuracies obtained at each iteration is as shown below

- Using the optimal value of the number of neighbours(k=10), the model performance was evaluated on the test dataset, where we procured an accuracy and f1-score of 0.906 and 0.903 respectively.
- The confusion matrix is as shown below:
[[486 36 51 0 0 0]
[ 0 431 41 4 0 0]
[ 10 4 328 0 0 0]
[ 0 0 0 409 47 2]
[ 0 0 0 78 485 2]
[ 0 0 0 0 0 533]]
2. Multi Class Logistic Regression with Elastic Net
- Using a Grid Search Cross Validation technique the ‘alpha’ values were varied from 1e-4,3e-4,1e-3,3e-3, 1e-2,3e-2 and the ‘l1-ratio’ values were varied from 0,0.15,0.5,0.7,1.
- The best values of ‘alpha’ and ‘l1 ratio’ were selected based on the model f1-score.
- A surface plot of the f1-scores along with the corresponding ‘alpha’ and ‘l1 ratio’ values are as shown below.

- Using the optimal value of alpha(0.001) and l1-ratio(0.7), the model performance was evaluated on the test dataset, where we procured an accuracy of 0.9528 and a f1-score of 0.9525.
- The confusion matrix is as shown below:
[[495 26 9 0 2 0]
[ 0 440 14 3 0 0]
[ 1 5 397 0 0 0]
[ 0 0 0 426 17 0]
[ 0 0 0 62 513 0]
[ 0 0 0 0 0 537]]
3. Support Vector Machine with RBF Kernel
- Using a Grid Search Cross Validation technique as in Multi Class Logistic Regression above, the ‘gamma’ values were varied from 1e-3 and 1e-4 while the cost parameter values ‘c’ were varied from 1,10,100,1000.
- The best values of ‘gamma’ and ‘c’ were selected based on the model f1-score.
- A surface plot of the f1-scores along with the corresponding ‘gamma’ and ‘c’ values are as shown below:

- Using the optimal value of c(1000) and gamma(0.001), the model performance was evaluated on the test dataset, where we procured an accuracy and f1-score of 0.9657.
- The confusion matrix is as shown below:
[[493 17 4 0 0 0]
[ 0 454 10 2 0 0]
[ 3 0 406 0 0 0]
[ 0 0 0 436 12 0]
[ 0 0 0 53 520 0]
[ 0 0 0 0 0 537]]
4. Random Forest
- Again, we employ a Grid Search Cross Validation technique varying the tree-depth(‘max_depth’) values from 300,500,600 and the number of trees(‘n_estimators’) from 200,500,700.
- The best values, analogous to the algorithms described above, were selected based on the best model f1-score.
- A surface plot of the f1- scores along with the corresponding ‘max_depth’ and ‘n_estimator’ values are as shown below:

- Using the optimal value of max_depth(300) and n_estimators(700), the model performance was evaluated on the test dataset where we procured an accuracy of 0.931 and a f1-score of 0.929.
- The confusion matrix is as shown below:
[[483 31 19 0 0 0]
[ 4 433 43 0 0 0]
[ 9 7 358 0 0 0]
[ 0 0 0 444 43 0]
[ 0 0 0 47 489 0]
[ 0 0 0 0 0 537]]
In light of the Machine Learning algorithms exploited on the HAR dataset, the Support Vector Machine(SVM) algorithm with RBF Kernel hits home achieving an accuracy and f1-score of 0.9657 when used to predict labels of the test dataset. At second spot comes the Multi Class Logistic Regression algorithm achieving an accuracy and f1-score of 0.9528 and 0.9525 when used for prediction on the test dataset. The K Nearest Neighbour algorithm achieved the lowest performance of all the algorithms employed clocking an accuracy of 0.906 and an f1-score of 0.903.
Human Activity Recognition(HAR) focuses on understanding human behaviour by identifying actions carried out by a person given a list of observations about the person and the surrounding environment(A Public Domain Dataset for Human Activity
Recognition Using Smartphones, pg. 437). With HAR being used extensively in surveillance systems as an example, models need constant amendments to make better predictions on unseen data. As for the research paper used as a reference for this project, increasing the size of the dataset and invoking dimensionality reduction techniques such as PCA or Multidimensional Scaling(MDS) would enable better results across all the algorithms used.
The dataset can be retrieved using the following link:
The code would also be made available on Github soon.
NIKHIL MYSORE.