0% found this document useful (0 votes)
34 views27 pages

K-Nearest Neighbor (KNN)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 27

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:

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.
Step-6: Our model is ready.
Suppose we have a new data point and we need to put it in the required category.
Consider the below image:
Firstly, we will choose the number of neighbors, so we will choose the k=5.
Next, we will calculate the Euclidean distance between the data points. The Euclidean
distance is the distance between two points, which we have already studied in geometry.
It can be calculated as:
By calculating the Euclidean distance we got the nearest neighbors, as three nearest neighbors in category A and two nearest neighbors in category B. Consider the below image:

As we can see the 3 nearest neighbors are from category A, hence this new data point must belong to category A.
Example:

Name Acid Durability Strength Class Distance Rank


Type-1 7 7 Bad 4 3
Type-2 7 4 Bad 5 4
Type-3 3 4 Good 3 1
Type-4 1 4 Good 3.6 2
Test Data 3 7 ?
The Distance Formula:

d=

d1= = 4
d2=
d3=
d4=

Based on two neighbors , Good


Example:
Height(cm) Weight(KG) Class Distance Rank

169 58 Normal 1.4 1

170 55 Normal 2 2

173 57 Normal 3 3

174 56 Underweight 4.1 4

167 51 Underweight 6.7 5

173 64 Normal 7.6 6

170 57 ?
The Distance Formula:

d=

d1= = 6.8
d2=
d3=
d4=

now lets calculate the nearest neighbor at K= 3

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.

Disadvantages of KNN Algorithm:


1.Always needs to determine the value of K which may be complex some time.
2.The computation cost is high because of calculating the distance between the data points for all the training samples.
Applications of KNN:

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.

Calculating Credit Ratings:


KNN algorithms can be used to find an individual’s credit rating by comparing with the persons having similar traits.
References:

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?

You might also like