Introduction to KNN | K-nearest neighbor classification algorithm using Examples
KNNor K-nearest neighbor classification algorithm is used as supervised and pattern classification learning algorithm which helps us to find which class the new input(test value) belongs to when
Knearest neighbors are chosen using distance measure.
It attempts to estimate the conditional distribution of
X, and classify a given observation(test value) to the class with highest estimated probability.
2. Ways to calculate the distance in KNN
3. The process of KNN with Example
4. KneighborsClassifier: KNN Python Example
5. Evaluating the KNN model
6. Benefits of using KNN algorithm
7. Disadvantages of KNN algorithm
Knearest neighbours. Once those
Knearest neighbours are found, the new
test valueis assigned to the category having the largest number of elements out of those
Probability of classification of test value in KNNIt calculates the probability of test value to be in class
jusing this function
Ways to calculate the distance in KNNAs already discussed that we have to calculate the distance between different points, we have a number of ways in which the distance can be calculated, the most common being the
Euclideanone, which I believe most of us have studied in high school.
- Euclidean Method
- Manhattan Method
- Minkowski Method
metricparameter to the KNN object. Here is an answer on Stack Overflow which will help. You can even use some random distance metric. Also read this answer as well if you want to use your own method for distance calculation. Using different distance metric can have a different outcome on the performance of your model.
The process of KNN with ExampleLet’s consider that we have a dataset containing heights and weights of dogs and horses marked properly. We will create a plot using weight and height of all the entries. Now whenever a new entry comes in from the test dataset, we will choose a value of
k. For the sake of this example, let’s assume that we choose 4 as the value of
k. We will use the distance measure to find the
Knearest neighbour and the
test valuewill belong to the one having more number of entities out of those
KneighborsClassifier: KNN Python ExampleGitHub Repo: KNN GitHub Repo Data source used: GitHub of Data Source In K-nearest neighbors algorithm most of the time you don’t really know about the meaning of the input parameters or the classification classes available. In case of interviews, you will get such data to hide the identity of the customer. You can use the following code to load it into a
# Import everything import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns %matplotlib inline # Create a DataFrame df = pd.read_csv('KNN_Project_Data') # Print the head of the data. df.head()
Why normalize/ standardize the variables for KNNAs we can already see that the data in the data frame is not standardized, if we don’t standardize it, the outcome will be fairly different and we won’t be able to get the correct results. This happens because some feature has a good amount of deviation in them (values range from 1-1000 vs values ranging from 1-10). This will lead to a bias in the model. We can understand this concept in more detail if we think in terms of neural networks. Let’s say we have a dataset and we are trying to find the salary of the employees given some features like, years of experience, grades in high school, university and salary in last organization different other factors. Now if we keep the data as it is, some features having higher values will get higher importance. So, to give a fair chance to every feature to contribute equally toward the model initially( with fixed weights), we normalize the distribution. A standard way to normalize a distribution is to apply this formula on each and every column.
from sklearn.preprocessing import StandardScaler scaler = StandardScaler() scaler.fit(df.drop('TARGET CLASS', axis=1)) sc_transform = scaler.transform(df.drop('TARGET CLASS', axis=1)) sc_df = pd.DataFrame(sc_transform) # Now you can safely use sc_df as your input features. sc_df.head()
min-maxapproach we discussed earlier. This is better because it will account for the deviation in the data.
Test/Train split using sklearnWe can simply split the data using sklearn.
from sklearn.model_selection import train_test_split X = sc_transform y = df['TARGET CLASS'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
Using KNN and finding an optimal k valueChoosing a good value of
kcan be a daunting task. We are going to automate this task using Python. We were able to find a good value of
kwhich can minimize the error rate in the model.
# Initialize an array that stores the error rates. from sklearn.neighbors import KNeighborsClassifier error_rates =  for a in range(1, 40): k = a knn = KNeighborsClassifier(n_neighbors=k) knn.fit(X_train, y_train) preds = knn.predict(X_test) error_rates.append(np.mean(y_test - preds)) plt.figure(figsize=(10, 7)) plt.plot(range(1,40),error_rates,color='blue', linestyle='dashed', marker='o', markerfacecolor='red', markersize=10) plt.title('Error Rate vs. K Value') plt.xlabel('K') plt.ylabel('Error Rate')
k=30gives a very optimal value of error rate.
k = 30 knn = KNeighborsClassifier(n_neighbors=k) knn.fit(X_train, y_train) preds = knn.predict(X_test)
Evaluating the KNN modelRead the following post to learn more about evaluating a machine learning model.
from sklearn.metrics import confusion_matrix, classification_report print(confusion_matrix(y_test, preds)) print(classification_report(y_test, preds))
Benefits of using KNN algorithm
- KNN algorithm is widely used for different kinds of learnings because of its uncomplicated and easy to apply nature.
- There are only two metrics to provide in the algorithm. value of
- Work with any number of classes not just binary classifiers.
- It is fairly easy to add new data to algorithm.
Disadvantages of KNN algorithm
- The cost of predicting the
knearest neighbors is very high.
- Doesn’t work as expected when working with a big number of features/parameters.
- Hard to work with categorical features.
Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!