## 4 KNN in Wireless Sensor Networks  
K-Nearest Neighbour is a popular classification tools used to test sets in the network. K-Nearest Neighbour is an example of a lazy learner algorithm because it only builds a model using the training set once the data set is performed and generates a model of the data set beforehand. K-Nearest Neighbour is a supervised learning technique that classifies new query results based on K-Nearest Neighbour groups [36]. The KNN approach is used in numerous areas: artificial intelligence, pattern recognition, statistical estimation, feature selection, and classification challenges. The advantages of this algorithm are its speed of implementation and simplicity. KNN performs well with substantial training data sets and is unaffected by noise. An example of the node classification is shown in Figure 7. The algorithm is looking for one node on a grid to choose if the node is in class 1, 2, 3, or 4. The KNN algorithm attempts to predict which class the new node should belong to. The new node should join class \(1\) rather than other classes. K-Nearest Neighbour is a supervised learning algorithm where the results are classified depending on most K-Nearest Neighbour categories. The main idea of this method is to classify a new object depending on attributes and training samples. The algorithm uses a short distance from the query instance to the training samples to determine K-Nearest Neighbours [13] and to calculate the distance between two nodes \(D(x,y)\), where \(x,y\) are coordinates of a set of nodes, such as \(x=\{x_{1},\,x_{2},\,\ldots\,.\,x_{n}\}\), \(y=\{y_{1},\,y_{2},\,\ldots\,.\,y_{n}\}\). This distance between two nodes can be calculated according to one of the following popular distance measures:  
* Euclidean distance function: Calculate the distance between binary vectors. \[D_{(x,y)}=\sqrt[2]{\sum_{i=1}^{K}x_{i}^{2}-y_{i}^{2}}\] (7)
* Absolute distance function: Calculate the distance between the real vectors depending on the sum of their absolute difference. Figure 7: Classification of nodes in the KNN algorithm. \[D_{(x,y)}=\sum_{i=1}^{K}\lvert x_{i}-y_{i}\rvert \tag{8}\]
* Minkowski distance function: Generalise Euclidean distance functions from Equation (7) and absolute distance functions from Equation (8). \[D_{(x,y)}=\left(\sum_{i=1}^{K}(\lvert x_{i}-y_{i}\rvert)^{q}\right)^{\frac{1}{q}}\] (9)  
When K = 1, it is assigned to the class of its nearest neighbour. The distance between two nodes or sensors depends on intervals. Therefore, the distance result should be based on the arithmetic mean across dataset 0 and standard deviation 1. This can be achieved using Equation (10) by replacing x,y with x\({}^{\prime}\),y\({}^{\prime}\):  
\[x^{\prime}=\frac{x-\overline{x}}{Sd\ (x)} \tag{10}\]  
where \(x\) is an unscaled value, the arithmetic mean of which can be calculated from Equation (11), \(Sd(x)\) is a standard deviation which can be calculated from Equation (12), and the result of the scaled value is \(x^{\prime}\). \[\overline{x}=\frac{1}{K}\sum_{l=1}^{K}x_{i} \tag{11}\]  
\[Sd(x)=\sqrt{\frac{1}{K}\sum_{l=1}^{K}(x_{i}\ -\overline{x})^{2}} \tag{12}\]  
The most popular distance measure to check which of the K instances in the dataset are most similar to a distance measure is the _Euclidean distance function_, which is calculated as the square root of the sum of the squared differences between a new point \(x\) and existing point \(y\):  
\[D_{(x,y)}=\sqrt{(x_{1}-y_{1})^{2}+(x_{2}-y_{2})^{2}} \tag{13}\]  
This type of distance measure is used when the input variable is similar in type; however, if we have different variables, absolute distance functions are recommended. The proposed approach uses Euclidean distance as the primary metric for clustering and cluster head (CH) selection in the Wireless Sensor Network (WSN). While Euclidean distance is a widely used and straightforward metric, it could be a more efficient or effective choice depending on the network topology, node distribution, and application requirements.