# Explainable AI-Enhanced Human Activity Recognition for Human-Robot Collaboration in Agriculture
## 2 Materials and Methods
### Model Explainability Using SHapley Additive ExPlanations (SHAP)  
SHAP serves as a prominent technique for enhancing the interpretability of ML models by elucidating their predictive rationale across various applications, including healthcare [50; 51], the energy sector [52; 53], economics [54; 55], and HAR [32; 33], to mention but a few. Inspired by cooperative game theory, SHAP assigns each feature a contribution value that helps explain its impact on the final prediction. Specifically, SHAP values quantify each feature's contribution to a specific prediction by comparing the output of the model with and without the feature in question. These values are calculated by considering all possible combinations of features and evaluating the average effect of a given feature on the prediction [56]. The result is a set of feature importance scores that can be visualized to identify the most influential factors driving the model's predictions. Given a set of measurements \(x_{i}=x_{i,1}\ +x_{i,2}\ +\ldots+x_{i,F}\), the goal is to accurately classify the corresponding activity \(y_{i}\) from a set of potential activities \(Y=\{1,2,\ldots,m\}\), with \(m=10\) in our case. Each individual sample contains various measurements gathered from multiple sensing units \(S_{1},\ S_{2},\ldots,\ S_{K}\). SHAP values for the \(j^{th}\) feature (with \(j\in F\), where \(F\) is the set of all features in the model) are calculated for \(x_{i}\) as  
\[\varphi_{j}(x_{i})=\sum_{S\subseteq F\backslash\{j\}}\frac{|S|!(F-|S|-1)!}{F! }\Big{(}v\Big{(}x_{i,S\cup\{j\}}\Big{)}-v(x_{i,S})\Big{)} \tag{1}\]  
The summation in Equation (1) iterates over all possible subsets (\(S\)) of features excluding feature \(j\). It also considers the factorial (!) of the number of features: (a) in subset \(S\); (b) not in subset \(S\) and not including feature \(j\); and (c) in set \(F\). The term \(v\Big{(}x_{i,S\cup\{j\}}\Big{)}-v(x_{i,S})\) calculates the difference in the model's prediction when feature \(j\) is included in the set of considered features compared to when it is excluded [57]. Considering a multi-class classification problem such as the present one, a set of outputs is utilized, namely, \(v_{1},v_{2},\ldots v_{m}\), one for each class, while for each output, a separate SHAP score is calculated as \(\varphi_{j,1}\ (\ x_{i}),\ \varphi_{j,2}\ (\ x_{i}),\ldots\ \varphi_{j,m}\ (\ x_{i})\). Regarding the SHAP score, it can be positive or negative, reflecting whether a feature increases or decreases the model's output. However, its influence is evaluated based on its absolute value. The total SHAP value of a feature is determined by taking the average of the absolute SHAP scores across a validation dataset \(x_{1},\ x_{2},\ldots\ x_{n}\), and summing these values across all classes:  
\[\varphi_{j}=\frac{1}{n}\sum_{i=1}^{n}\sum_{l=1}^{m}\Big{|}\varphi_{j,l}\ (\ x_{i})\Big{|}. \tag{2}\]  
Considering the present study's focus on a tree-based XGBoost model for HAR, SHAP was selected as the most appropriate explainability method. Its unique advantages make it preferable over alternative techniques, such as Local Interpretable Model-agnostic Explanations (LIMEs), the Python library \"ELIS\", Layer-wise Relevance Propagation (LRP), CounterFactual Explanations (CFEs), and Anchor [58; 59; 60]. In short, LIMEs can suffer from instability and limited global interpretability (similar to Anchor). ELI5 is effective for simpler models but struggles with complex ones, like the one used in this study. LRP works well for neural networks but is less effective for tree-based models, while CFEs lack the granularity needed for detailed feature importance. In contrast, SHAP ensures reliable global interpretability and provides clear feature importance. The primary challenge in applying SHAP values in practice is their high computational cost [58]. In model-agnostic settings, calculating SHAP values has a complexity of \(O\big{(}2^{F}\big{)}\), meaning that the computation grows exponentially as the number of features increases.