KNN
KNN
Dr. Adven
K-Nearest Neighbor(KNN) Algorithm
• The K-NN working can be explained on the basis of the below algorithm:
• Step-1: Select the number K of the neighbors
• Step-2: Calculate the Euclidean distance of K number of neighbors
• Step-3: Take the K nearest neighbors as per the calculated Euclidean
distance.
• Step-4: Among these k neighbors, count the number of the data points in
each category.
• Step-5: Assign the new data points to that category for which the number
of the neighbor is maximum.
• Step-6: Our model is ready.
Selecting the value of K in KNN
algorithm
1. Cross-Validation:
• Use k-fold cross-validation to test different values of K and choose the one that provides the best performance on
validation data. This helps to avoid overfitting and underfitting.
2. Error Analysis:
• Plot the error rate for different values of K and select the K with the lowest error rate. Typically, this is done by
plotting the error rate on the y-axis and KKK values on the x-axis.
• The plot often shows a U-shaped curve where the error initially decreases, reaches a minimum, and then starts to
increase. Select K at the point where the error is minimized.
3. Domain Knowledge:
• Use domain-specific knowledge to guide the selection of K. For example, in some cases, a small KKK (like 3 or 5) might
be more appropriate for capturing local patterns, while in others, a larger K might be needed to account for noise.
4. Heuristic Methods:
• A common heuristic is to set K as the square root of the number of data points, but this is just a rule of thumb and
may not always provide the best result.
5. Empirical Testing:
• Test multiple values of K empirically on the training set and observe which one provides the best balance between
bias and variance.
6. Even vs. Odd Values:
• Use odd values of K when the number of classes is even to avoid ties.
Advantages of KNN Algorithm
• It is simple to implement.
• It is robust to the noisy training data
• It can be more effective if the training data is large.
Disadvantages of KNN Algorithm