## 4 Feature Compression  
Given that a smartphone includes a multitude of relevant sensors for the classification task, the number of features is inevitably very high. To avoid numerical problems and make the adaptation more straightforward it is necessary to ignore redundant and irrelevant features. In order to reduce the dimensionality of the feature space, we use a method based on a transformation with two stages [16; 17].  
First, all the training features are concatenated in a \(k-by-n\) matrix \(G\), where \(k\) is the number of features and \(n\) the number of feature vectors. In the following, by covariance matrix \(\Sigma\) we mean the sample covariance matrix obtained from the training data \(G\).  
The averaged within-class covariance matrix is defined as  
\[S_{W}=\frac{1}{C}\sum_{j=1}^{C}\Sigma_{j} \tag{1}\]where \(C\) is the number of classes (in this work we have multiple sets of classes \(C\), e.g., the activities and environments have separate training data sets and classifiers. Nevertheless, for notational simplicity here we only use a single set of classes \(C\)). and \(\Sigma_{j}\) is the covariance matrix of class \(j\). The required transformation matrix \(R_{1}\) is obtained using the eigenvectors (in the matrix \(U\)) and the rank of \(S_{W}\) (\(r\)):  
\[R_{1}=\Lambda_{r}^{-\frac{1}{2}}U_{r}^{T} \tag{2}\]  
where the diagonal matrix \(\Lambda\) contains the \(r\) eigenvalues of \(S_{W}\), and thus \(R_{1}S_{W}R_{1}^{T}=I\), an \(r-by-r\) identity matrix. This transformation scales the raw features conveniently. The dimension reduction removes redundant features, for example those that are constant all the time.  
The next phase is to compress the class mean information. The between-class covariance matrix is defined as  
\[S_{B}=\frac{1}{C}\sum_{j=1}^{C}(\mathbf{\mu}_{j}-\mathbf{\mu})(\mathbf{\mu}_{j}-\mathbf{\mu})^ {T} \tag{3}\]  
where \(\mathbf{\mu}\) is the mean of the within-class means \(\mathbf{\mu}_{j}\). \(S_{B}\) describes how far apart the class means are from each other. In the second phase, we seek for a transformation \(R_{2}=V_{\
u}^{T}\) that diagonalizes \(S_{B}\):  
\[V_{\
u}^{T}(R_{1}S_{B}R_{1}^{T})V_{\
u}=\tilde{\Lambda}_{\
u} \tag{4}\]  
where \(\
u\) is the rank of \(R_{1}S_{B}R_{1}^{T}\) and the matrix \(V_{\
u}=[\mathbf{v}_{1},\ldots,\mathbf{v}_{\
u}]\) consists of \(\
u\) eigenvectors of \(R_{1}S_{B}R_{1}^{T}\) (_i.e._, eigenvectors of \(S_{B}\)_after_ the first transformation \(R_{1}\)). Similarly, \(\tilde{\Lambda}_{\
u}\) is a diagonal matrix containing \(\
u\) eigenvalues.  
There are at most \(C-1\) non-zero eigenvalues (after the transformation the number of features is reduced at maximum to \(C-1\)) and after the transformation \(R_{2}R_{1}\) the first feature vector can be considered as the most important for the classification purposes. This geometric interpretation of linear discriminant analysis (LDA) is very useful in displaying the data, and it is straightforward to implement. The matrix \(R=R_{2}R_{1}\) can be computed off-line, and the software in the mobile phone only needs to perform a matrix-vector multiplication for the raw features. In addition, some features clearly do not follow a Gaussian distribution (number of WLAN APs, for example), but after the feature compression we can quite safely work with Gaussian models.