# K-nearest Neighbors Algorithm with Examples in R (Simply Explained knn)

Now, if someone give us a new object with new attributes and ask us to predict as to which category does that new object belongs to, that means we are given the dimension and asked to predict if it is a chair, bed or table, then we would have to use knn algorithm to determine that.Therefore, attributes means the property of each object, and each object can be considered as a category..Our job is to check how closely are the properties of the new object is related to any one of the already known categories..Now, I am going to show you many kinds of scenarios and I will try to explain those as simply possible as I can.When we are given the attributes, we try and plot them on graph..The graphical representation of those attributes will help us to calculate Euclidian distance between the new value and the ones that we already know we have..By doing so, we can be sure as to which category does the new object is closest to.Euclidean distanceDon’t get intimidated by the name, it just simply means the distance between two points in a plane..By simple using this formula you can calculate distance between two points no matter how many attributes or properties you are given like height, breadth, width, weight and so on upto n where n could be the last property of the object you have..The formula is √(x2−x1)²+(y2−y1)²+(z2−z1)² …… (n2-n1)²Two category and one attribute (1D)We have two categories called male and female with their respective heights given in the table below..Then, if a new member shows up and you are asked to determine if it is a male or female given you already know his/her height, then you can plot that height in 1D plane and the check the proximity of that new height with other already drawn heights..Ideally speaking, we would calculate the Euclidean distance on that graph to determine the closest heights to height of new member..We see that if we draw 180cm in height graph, it lies closer to the male heights than it is from female height..That is how we determine it is a male..(a) shows the table (b) shows the representation on graph (c) shows introduction of new member (d) shows the predicted value to be male.Two category and two attributes (2D)Let’s say we bring up a new attribute called weight that can also describe the characteristic of male and female as shown in the table below.. More details