This article was published as a part of the Data Science Blogathon.
KNN stands for K-Nearest Neighbors, the supervised machine learning algorithm that can operate with both classification and regression tasks. KNN is often one of the hot topics in the interviews when the panel is interested in someone who can deal with sparse dataset situations or even a curse of dimensionality (we will discuss these things too) while working with the KNN model as creating a model is not a tedious task but dealing with its drawback and coming back with a solution is!
In this article, we will discuss some of the most asked and tricky questions so that one can answer not only specific questions but also cross-questions from the panel member. So let’s get started!
Suppose one is choosing KNN as their primary model. In that case, one needs to have sufficient domain knowledge of the problem statement he/she is working on, as the KNN algorithm can give us a high-accuracy model, but the same is not human-readable. Other than that, KNN can work accurately for classification problems where we need to find the data point (say X1) from two categories in the sample space.
Along with classification problems, KNN also fits well with regression tasks. Make sure that this model is not preferred when we have a way too large a dataset to deal with, as KNN is a distance-based algorithm which makes it high in cost when it comes to calculating the distance between two data points.
K-NN follows up a well-structured method to complete the assigned task, and I have tried to break it down into a few steps:
First, we need to know what exactly is K value is; K is the numeric value that keeps the count of nearest neighbors, and we can’t have the hit, and trial method for multiple k values as the cost of calculation is pretty expensive hence we need to have certain guidelines to choose the optimal “K.”
Choosing KNN over another classification algorithm solely depends on our requirements. Suppose we are working on a task that requires flexibility in the model then we can go for KNN, whereas if efficiency is the priority then we can go for other algorithms like Gradient descent or Logistic Regression.
Note: Above answer can backfire on another question, so be prepared for it: How KNN is more flexible?
The main logic behind KNN flexibility is that it is a non-parametric algorithm so it won’t have to make any assumption on the underlying dataset. Still, at the same time, it is expensive in computation, unlike other classification algorithms.
Both decision trees and KNN is non-parametric algorithms but are different in their way of delivering the results; some are as follows:
For calculating distances in KNN, we have multiple options available like Chi-square, Minkowsky, cosine similarity measure, and so on. But Euclidean distance is a widely preferred method for calculating the distance as it returns us the shortest distance between two data points.
Before hitting the nail, let’s first understand which nail to hit i.e. what is normalization?
Normalization is the process where the whole dataset is scaled down within a specific range, mostly between 0 and 1. This turned out to be a necessary step while dealing with the KNN algorithm as it is a distance-based algorithm, so if the data points are not within a specific range, then different magnitudes can misclassify the data points in the testing phase.
The curse of dimensionality is considered to be checked frequently while working with the KNN model, whereas the dimension tends to increase, then the data turns to be more sparse, i.e., we often find tons of space in the datasets, which leads to the state of overfitting also which makes algorithm incapable to find the nearest neighbors. Ideally, as the number of dimensions increases, the space in the dataset should also increase exponentially (both should positively complement each other).
Linearization is one of the best techniques to break the curse of dimensionality – This is a bonus point I’ll suggest having more understanding of this term.
There are a few conditions where KNN will not perform based on our expectations, listing them below:
Here we are in the last section of the article, by far we have discussed the Top 9 questions that are most frequently asked in an interview related to KNN, and this section will help you in revision and let you know what things to cover in brief regarding this hyper extensive algorithm.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.