# Basic Understanding of KNN Algorithm

Nowadays, with Machine Learning and Artificial Intelligence being applied to more and more industries, people have various needs when they looked at different algorithms. K Nearest Neighbors is one of the most popular among these models, and in this post, I will discuss what is KNN algorithm and what situation this model works the best.

# Algorithm Overview

The k-nearest neighbors (KNN) algorithm is a simple, easy-to-implement supervised machine learning algorithm that can be used to solve both classification and regression problems.

# How does K-NN work?

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.

# What is k value and how to determine it?

If k is too small, then the outliers and noise data would make the result not accurate. If k is too large, it will lead to an oversimplified decision boundary. When k increases, the bias will increase. When k decrease, the variance will increase. We can use the Elbow method to determine the optimal k. Please see this link for a comprehensive tutorial of the Elbow method. We can also try multiple k values based on our choice to determine the best fit for the model.

1. This algorithm is simple, easy to implement, and no need to estimate parameters.
2. Suitable for classifying events, which can be broadly applied in real-world problems.
3. It can be used for both classification and regression.