Hey everyone
My name is Aastha Chauhan. Welcome to the tutorials point in the previous video
We have learned all about the supervised learning and in this video we are going to talk about the KNN algorithm
So let's see what's been for you in this video. We are going to look at why KNN how do we choose the factor K
How does KNN algorithm work? When do we use KNN algorithm and what are the limitations of the KNN
So let's first see why KNN K nearest neighbor algorithm is a simple yet powerful machine learning algorithm
and we can use this in two types of problems that is classification and regression
Now there is a factor K on the basis of which we do all the predictions in KNN algorithm
So it's very important to choose the right value of K. How let's understand this with this example
Consider this question mark is the new variable. Now we have to predict
whether this belongs to the rectangle category or triangle category. So if I consider the value of K as 3
when there are two rectangles in one triangle, so in this case
it will belong to the rectangle category. And if I consider the value of K as 7
then there are three rectangles and four triangles. That means in this case
it will belong to the triangle category. I hope now you are able to understand
why choosing the right value of K is so important and the process of choosing the right value of K
is called parameter tuning. Generally we take the square root of n
where n is the total number of data points and we always consider the odd value of K
to avoid the tiebreakers. Now, let's understand how does the KNN algorithm work with this example
Consider there are two categories category A and category B. Now we have to determine
whether this new data point belongs to the category A or category B
Now we need the value of K. So assume for this case the value of K is 5
Now we have to calculate the distance between this new data point and the given data points
So the next question is how do we calculate the distance between two data points
So for that we use Euclidean distance which says the distance between the point A X1 Y1
and point B X2 Y2 is given as under root X2 minus X1 to the whole square
plus Y2 minus Y1 to the whole square. So that's how we are going to calculate the distance
between the new data point and rest of the given data points
So after calculating the distance between the new data point and the other given data points
we can observe that we have three neighbors from category A and two neighbors from category B
That means this new data point belongs to the category A. Now, let's see when do we use K in an algorithm
So we use K in an algorithm when data is labeled data is noise-free
and data set is a small K in an algorithm is a lazy algorithm
So it works well when the data set is small. Now, let's see
What are the limitations of KNN? KNN does not perform well on huge data set
It is sensitive to arc layers and it is also sensitive to the missing values
So that was it for this video. We have already covered the supervised learning algorithm
KNN algorithm in this video. And in the next video, we are going to discuss about the decision tree
and all the machine learning algorithms in further videos. So stay tuned with tutorials point
Thanks for watching and have a nice day