K-Nearest Neighbor (KNN)
K-Nearest Neighbor (KNN)
K-Nearest Neighbor (KNN)
Contents
What is KNN?
Why do we need KNN?
How do we choose the factor ‘K’?
When do we use KNN?
How does KNN algorithm work?
Examples
Advantages & Disadvantages
Applications
What is KNN Algorithm?
What is KNN Algorithm?
K-NN algorithm
1. Finding the most similar data points in the training data, and attempt to make an educated guess based on
their classification
2. assumes the similarity between the new case/data and available cases and put the new case into the category
that is most similar to the available categories. K-NN is a non-parametric algorithm, which means it does not
make any assumption on underlying data. It is also called a lazy learner algorithm because it does not learn from
the training set immediately instead it stores the dataset and at the time of classification, it performs an action
on the dataset.
Run the
majority
Calculate code
the among the
Euclidian selected
distance Pick the k data
between closest points, the
Select a
the point to data points dominating
value for k
be (points classificatio repeat
(eg. 1,
classified with the k n is the
2,3…)
and every smallest winner.
other point distances) Point is
in the classified
training based on
data set the
dominant
class
Why do we need a K-NN Algorithm?
Why do we need a K-NN Algorithm?
Suppose there are two categories. Category A and Category B, and we have a new data point x1, so this data point will lie in which of
these categories. To solve this type of problem, we need a K-NN algorithm. With the help of K-NN, we can easily identify the
category or class of a particular dataset. Consider the below diagram:
How do we choose the factor ‘K’?
KNN Algorithm is based on feature similarity: Choosing the right value of k is a process
called parameter tuning, and is important for better accuracy.
How do we choose the factor ‘K’?
How do we choose the factor ‘K’?
When Do We Use the KNN Algorithm?
KNN can be used for both classification and regression predictive problems. However, it is
more widely used in classification problems in the industry.
How does K-NN work?
The K-NN working can be explained on the basis of the below algorithm:
As we can see the 3 nearest neighbors are from category A, hence this new data point must belong to category A.
Example:
d=
d1= = 4
d2=
d3=
d4=
170 55 Normal 2 2
173 57 Normal 3 3
170 57 ?
The Distance Formula:
d=
d1= = 6.8
d2=
d3=
d4=
170 cm 57 kg = Normal
Advantages of KNN Algorithm:
1.It is simple to implement.
2.It is robust to the noisy training data
3.It can be more effective if the training data is large.
The following are some of the areas in which KNN can be applied successfully −
Banking System:
KNN can be used in banking system to predict weather an individual is fit for loan approval? Does that individual have the characteristics similar to the defaulters one or not.
https://www.javatpoint.com/k-nearest-neighbor-algorithm-for-machine-learning
https://www.tutorialspoint.com/machine_learning_with_python/machine_learning_with_python_knn_algorithm_finding_nearest_neighbors.htm
https://www.youtube.com/watch?v=Vk9lGGODaJA
https://www.youtube.com/watch?v=HZT0lxD5h6k
Thank You
Any Questions?