Classification
K-Nearest Neighbors, KNN
2023-12-20
In general, machine learning modeling involves two steps: defining a parameterized model based on domain knowledge and estimating its parameters from the available data.
On the other hand, there is an alternative method of performing predictions using the available data or experience itself instead of abstract modeling, and this is called "lazy learner". Probabilistic Neural Network, KNN, Gaussian Process Regression, etc. belong to this category.
KNN is understandable without much difficulty in a similar way that we could predict nearest neighborhood demography based on the address without other detail information.
The term "nearest" here involves the concept of distance, and depending on the nature of the problem, various notions of distance (such as simple distance, absolute distance, statistical distance, etc.) can be employed. Moreover, depending on the physical nature of the variables to be compared (i.e., annual income and physical height are completely different concepts), preprocessing of data may be necessary.
In KNN, the results can vary depending on how many nearest neighbors are used for prediction. For example, in the illustration given below, the identity of the new blue data point (among possible neighbors indicated by red and green) can differ based on the value of K, which represents the number of nearest neighbors to consider (in this case, whether it is 3 or 7). The K is referred to as a hyperparameter, and it is important to choose the most stable value for K through the process of cross-validation to obtain statistically reliable results.

When a specific number of people, denoted as K, gather together, their majority vote is used to make predictions. In this case, to give more weight to those who are closer, the weight is assigned to proportional to the inverse of the distance. The total sum of these weights becomes the result of the vote.
The execution of the algorithm is as follows.
(step 1) Normalize the entire data using min-max
\(x=100\dfrac{x-\min (x)}{\max (x)-\min (x)}\)
(step 2) Calculate the distance from the new sample, x, to the all of the individual training dataset, xi.
For all training data xi, i=1,n
\(d_i=\sqrt{(x-x_i)^2}\)
(step 3) Calculate the reciprocal and calculate the weight by normalizing the reciprocal by the sum of the reciprocals of all the training data.
\(\text{reciprocal}_i=\dfrac{1}{1+d_i}\)
\(\text{weight}_i=\dfrac{\text{reciprocal}_i}{\sum _i^{\ }\text{reciprocal}_i}=\dfrac {\dfrac{1}{1+d_i}}{\sum_i^{\ }\dfrac{1}{1+d_i}}\)
(step 4) The predicted value is the Class with the highest vote among the candidate group K.
\(\text{vote}_j = \sum_{i\in j} \text{reciprocal}_i, \text {where } i \subset K\)
\(\text{Prediction} = \arg\max_j \text{vote}_j\)
Next, we will use Iris dataset to demonstrate KNN in Excel worksheets
(step 1) Normalize all training data including any new sample using min-max.

(step 2) Calculate distances from the new sample to all training data. Calculate reciprocals using the distances.
distance = SQRT(SUMXMY2(new sample, individual training data))
reciprocal = 1/(1+distance)
weight = reciprocal / sum(reciprocal)

(Step 3) Calculate the Kth distance for the given K, and determine the weights of the data points belonging to each Class that are situated within a distance smaller than that.
Kth distance =SMALL(J$12:J$161,K)
Per Class the sum of all weights = SUMIFS(weight, Class list Column, Target Class, distance, "<="&$F4), where $F4 is the cell address of the Kth distance.
(step 4) Select the Class with the maximum weight among the Classes.
Max Weight = max(G4:I4)
Prediction = INDEX(G3:I3,1,MATCH(J4,G4:I4))

<Note>
MATCH(J4, G4:I4) returns the position of the column in the range G4:I4 that has the same value as J4.
INDEX(G3:I3, 1, 2) returns the value located at row=1, column=2 in the range G3:I3