# Enhanced K-Nearest Neighbor for Intelligent Fault Diagnosis of Rotating Machinery
## 3 Proposed Method
### Stage 3: Nearest Neighbor Searching  
With the training feature dataset, we can find the nearest neighbor search process. To find the nearest neighbors of each testing sample automatically, a new method based on sparse coding and L2 norm is developed. Suppose the training dataset is \(\mathbf{X}\in R^{N_{2}\times M}\) and testing dataset is \(\mathbf{Z}\in R^{N_{2}\times M_{1}}\), where \(N_{2}\), \(\mathbf{M}\) and \(M_{1}\) are the input dimension, number of training samples and number of testing samples, respectively.  
In the proposed method, the testing samples are reconstructed using the training samples to obtain a correlation matrix \(\mathbf{W}\in R^{M\times M_{1}}\). This minimizes the error between \(\mathbf{Z}\) and \(\mathbf{WX}\), namely, the reconstructed testing sample, where \(w^{i}\) is the column correlation vector between the \(i\)-th testing sample \(z^{i}\) and \(\mathbf{X}\in R^{N_{2}\times M}\). Here, the reconstruction error is evaluated by least square loss, so the reconstruction process can be formed as the first term of Equation (5).  
\[\min_{W}\|\mathbf{Z}-\mathbf{WX}\|_{F}^{2}+\rho_{1}\sum_{i=1}^{M_{1}}\Bigl{|}w^{i} \Bigr{|}+\rho_{2}\Bigr{\|}(\mathbf{WX})^{\mathsf{T}}\mathbf{WX}-\mathbf{L}\Bigr{\|}_{2} \tag{5}\]  
Meanwhile, in this reconstruction process, as the training dataset is always overcomplete, the sparsity of each correlation vector \(w^{i}\) is also constrained by minimizing the L1 norm of each correlation vector in the second term of Equation (5). Consequently, some elements of the correlation vector can also be constrained to zero, which is convenient for further nearest neighbor location, where \(\rho_{1}\) is the regular parameter for importance weight between testing sample reconstruction and the sparsity of \(\mathbf{W}\).  
Furthermore, it is also found in our experiments that for nearest neighbor location, we need to maximize the correlation between each testing sample and training samples from the same health condition. Hence, similarity correlations between samples are also constrained in the third term of Equation (5), where \(\rho_{2}\) is also a regular parameter similar to \(\rho_{1}\), and \(\mathbf{L}\) is the similarity matrix between samples, which is calculated by common heat kernel.  
In optimization, this algorithm can be optimized via the common L-BFGS method [36], and finally a correlation matrix \(\mathbf{W}\) is obtained, where \(w^{i}\) is the correlation vector between the training samples and the \(j\)-th testing sample, and \(w^{ij}\) is \(j\)-th element of \(w^{i}\). Correspondingly, the nearest neighbors of testing sample \(z^{i}\) can be located by listing the elements of \(w^{i}\) in descending order, and the training samples falling on their first \(c\) elements are the nearest neighbors, where \(c\) is the health condition number.  
Through this process, the nearest neighbors can be determined adaptively for different testing samples. Meanwhile, as the sparsity of correlation vectors is also taken into consideration by fusing L1 norm into this process, its nearest neighbor location is more precise.