K Nearest Neighbor using Python
In the previous article, we studied the Naive Bayes.
One thing that I believe is that if we can correlate anything with us or
our lives, there are greater chances of understanding the concept. So
I will try to explain everything by relating it to humans.
What is K nearest neighbor used for?
KNN can be used for both classification and regression predictive problems. However, it is more widely used in classification problems in the industry. k-NN is often used in search applications where you are looking for “similar” items; that is when your task is some form of “find items similar to this one”. We use k-NN when we have fewer than 20 features (attributes) per instance, typically normalized or we have a lot of training data.
What is KNN?
KNN is a non-parametric, lazy learning algorithm; i.e. it does not make any assumptions on the underlying data and also there is no explicit training phase or it is very minimal.
k-NN is all about finding the next data point(s) which is at the minimum distance from the current data point and to club all into one class.
k is the max value of data point(s) that can be clubbed under a given class.
To find the distance we use employ various algorithms like Euclidean Distance, Hamming Distance, Manhattan Distance, Minkowski Distance, etc.
For example, 1-NN means that we have to generate a model that will have classes based on the data point which is at the least distance. Similarily, 2-NN means that we have to generate a model that will have classes based on the 2 data points with the least distances.
The algorithm of k-NN or K-Nearest Neighbors is:
- Computes the distance between the new data point with every training example.
- For computing, distance measures such as Euclidean distance, Hamming distance or Manhattan distance will be used.
- The model picks K entries in the database which are closest to the new data point.
- Then it does the majority vote i.e the most common class/label among those K entries will be the class of the new data point.
Steps involved in the processing and generating a model
- Decide on your similarity or distance metric.
- Split the original labeled dataset into training and test data.
- Pick an evaluation metric.
- Decide upon the value of k. Here k refers to the number of closest neighbors we will consider while doing the majority voting of target labels.
- Run k-NN a few times, changing k and checking the evaluation measure.
- In each iteration, k neighbors vote, majority vote wins and becomes the ultimate prediction
- Optimize k by picking the one with the best evaluation measure.
- Once you’ve chosen k, use the same training set and now create a new test set with the people’s wages and incomes that you have no labels for, and want to predict.
Steps involved in selecting the value for K
- Determined experimentally
- Start with k=1 and use a test set to validate the error rate of the classifier
- Repeat with k=k+2
- Choose the value of k for which the error rate is minimum
Note: k should be an odd number to avoid ties
1-NN

3-NN
7-NN
KNN vs K-Means Clustering
| k-NN | k-Means | |
| Types |
Supervised |
Unsupervised |
| What is K? |
Number of closest neighbors to look at |
Number of centroids |
| Calculation of prediction error possible |
Yes |
No |
| Optimization is done using what? |
Cross-validation and confusion matrix |
Elbow method and the silhouette method |
| Algorithm convergences |
When all observations classified at the desired accuracy |
When cluster membership doesn't change anymore |
| Complexity |
Training: O(Dimensions/Features) Test: O (Number of Observations) |
O (Number of points * Number of Clusters * Number of iterations * number of attributes) |
Curse of Dimensionality
Imagine instances described by 20 features (attributes) but only 3 are relevant to the target function. Curse of dimensionality means that nearest neighbor can be easily misled when instance space is high-dimensional and is dominated by a large number of irrelevant features.
A solution to this can be
- Stretch j-th axis by weight zj, where z1,…,zn chosen to minimize prediction error (weight different features differently)
- Use cross-validation to automatically choose weights z1,…,zn
- Note setting zj to zero eliminates this dimension altogether (feature subset selection)
- PCA
Advantages/Features of KNN
- K-NN is pretty intuitive and simple
The K-NN algorithm is easy to implement and very simple to understand. It reads through the whole dataset to classify the new data point and to find out K nearest neighbors. - K-NN has fewer assumptions
K-NN is a non-parametric algorithm which means there are fewer assumptions to be met to implement K-NN. Parametric models like linear regression have lots of assumptions to be met by data before it can be implemented which is not the case with K-NN. - No Training Step
K-NN does not explicitly build any model and simply tags the new data entry based learning from historical data, hence no training step is required. - It constantly evolves
The classifier immediately adapts as we collect new training data and respond quickly to changes in the input during real-time use. - Very easy to implement for the multi-class problem
K-NN adjusts to multi-class without any extra efforts as compared to other algorithms. - Can be used both for Classification and Regression
One of the biggest advantages of K-NN is that K-NN can be used both for classification and regression problems. - One Hyper Parameter
K-NN might take some time while selecting the first hyperparameter but after that rest of the parameters are aligned to it. - KNN stores the entire training dataset which it uses as its representation.
- KNN does not learn any model.
- KNN makes predictions just-in-time by calculating the similarity between an input sample and each training instance.
- A variety of distance criteria to choose from the K-NN algorithm gives the user the flexibility to choose distance while building a K-NN model.
- Euclidean Distance
- Hamming Distance
- Manhattan Distance
- Minkowski Distance
Disadvantages/Shortcomings of KNN
- K-NN is a slow algorithm
K-NN might be very easy to implement except for the fact that as the dataset grows efficiency/speed of algorithm declines very fast. - Curse of Dimensionality
KNN predicting efficiency decreases as the numbers of variables grow works.
- K-NN needs homogeneous features
If you decide to build k-NN using a common distance, like Euclidean or Manhattan distances, it is completely necessary that features have the same scale, since absolute differences in features weight the same, i.e., a given distance in feature 1 must mean the same for feature 2. - An optimal number of neighbors
One of the biggest and most crucial issues with K-NN is to choose the optimal number of neighbors to be considered while classifying the new data entry. - Imbalanced data causes problems
k-NN doesn’t perform well on imbalanced data. - Outlier sensitivity
K-NN algorithm is very sensitive to outliers as it simply chose the neighbors based on distance criteria. - Missing Value treatment
K-NN inherently has no capability of dealing with missing value problems.
Assumptions of KNN
- k-NN performs much better if all of the data have the same scale
- k-NN works well with a small number of input variables (p) but struggles when the number of inputs is very large
- k-NN makes no assumptions about the functional form of the problem is solved
Euclidean distance
Manhattan Distance
Chi-Square Distance
Correlation Distance
Hamming Distance
Minkowsky Distance
KNN using an example
Let us understand how k-NN really works. For that let's take a dummy dataset.
data = [
[65.75, 112.99],
[71.52, 136.49],
[69.40, 153.03],
[68.22, 142.34],
[67.79, 144.30],
[68.70, 123.30],
[69.80, 141.49],
[70.01, 136.46],
[67.90, 112.37],
[66.49, 127.45],
]
[65.75, 112.99],
[71.52, 136.49],
[69.40, 153.03],
[68.22, 142.34],
[67.79, 144.30],
[68.70, 123.30],
[69.80, 141.49],
[70.01, 136.46],
[67.90, 112.37],
[66.49, 127.45],
]
In the above example, the data is of the form [feature, target].
Assuming the value of k to be 3 i.e. 3-NN, let's try to find the prediction for feature value "60".
So the 3 nearest neighbors would be
a. 65.75 with a distance value 5.75
b. 66.49 with a distance value 6.49
c. 67.79 with a distance value 7.79
And hence the predicted value will be 128.25.
Similarly, let's take another dataset
data = [
[22, 1],
[23, 1],
[21, 1],
[18, 1],
[19, 1],
[25, 0],
[27, 0],
[29, 0],
[31, 0],
[45, 0],
]
[22, 1],
[23, 1],
[21, 1],
[18, 1],
[19, 1],
[25, 0],
[27, 0],
[29, 0],
[31, 0],
[45, 0],
]
In the above example, the data is of the form [feature, target].
Assuming the value of k to be 3 i.e. 3-NN, let's try to find the prediction for feature value "33".
So the 3 nearest neighbors would be
a. 31 with a distance value 2.0
b. 29 with a distance value 4.0
c. 27 with a distance value 6.0
And hence the predicted value will be 0.

Sourav Kumar DasPosted Dec 19, 2019, 5:10 AM
Nice and useful article Sir.