ML Lecture#10
ML Lecture#10
MACHINE LEARNING
• K-NN algorithm 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 algorithm stores all the available data and classifies a new data point
based on the similarity. This means when new data appears then it can be
easily classified into a well suite category by using K- NN algorithm.
DR. HAIDER ALI Machine Learning 4
KNN
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.
KNN algorithm at the training phase just stores the dataset and when it gets new
data, then it classifies that data into a category that is much similar to the new data.
The K-NN working can be explained on the basis of the below algorithm:
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:
As we can see the 3 nearest neighbors are from category A, hence this new data point must belong to category A.
DR. HAIDER ALI Machine Learning 11
EXAMPLE
Name Age Gender Sport
Ahmed 32 M Football
Mark 40 M Neither
Sara 16 F Cricket
Zaira 34 F Cricket
Saleem 55 M Neither
Rahul 40 M Cricket
Saba 20 F Neither
Smith 15 M Cricket
Nazish 55 F Football
Michael 15 M Football
Mirha 5 F ??
DR. HAIDER ALI Machine Learning 12
EXAMPLE
Name Age Gender Sport
Ahmed 32 0 Football
Mark 40 0 Neither
Sara 16 1 Cricket
Zaira 34 1 Cricket
Saleem 55 0 Neither
Rahul 40 0 Cricket
Saba 20 1 Neither
Smith 15 0 Cricket
Nazish 55 1 Football
Michael 15 0 Football
Mirha 5 1 ?? K=3
= 1 − 2 + 1 − 2
= 1 − 2 + 1 − 2