K Nearest Neighbour Easily Explained With Implementation
K Nearest Neighbour Easily Explained With Implementation
K Nearest Neighbour Easily Explained With Implementation
0:03
be discussing about K nearest neighbor
0:05
both intention and the implementation
0:08
intuition will be understanding about
0:11
classification use is and for
0:13
reservations
0:14
how can a neighbor algorithm actually
0:16
works to begin with case let us just
0:20
consider a data set and telling a
0:22
nearest neighbor is wonderful classified
0:27
data points if that basically means that
0:29
if your data point is distributed in a
0:31
nonlinear manner
0:32
you cannot just draw straight line and
0:34
classified nearest people you can
0:38
basically use it so let us just go ahead
0:40
and try to see with the help of an
0:42
example how does it a means so first of
0:45
all I am able to consider it for a
0:46
classification problem statement so if I
0:49
have a classification use kids
0:51
so basically classification use case now
0:55
suppose I can say with that I have some
0:57
data points and in this particular data
1:00
points I have some data points and in
1:03
this data point I have business so this
1:06
is one of my category the other category
1:08
I'll draw it by using points now how to
1:14
scale nearest neighbor work we will try
1:16
to understand suppose we consider in K
1:20
nearest neighbor we have to select a
1:21
cave and this value basically indicates
1:23
that how many nearest neighbor we are
1:26
going to consider okay now suppose if I
1:28
consider T is to fun and suppose
1:30
tomorrow after my model is getting
1:33
trained and suppose I get my new data
1:36
points from make you my new data point
1:39
is somewhere here okay at this
1:42
particular data point what I will do is
1:44
that first of all I just take what is my
1:46
came here a even that basically means
1:49
that how many nearest neighbor are to
1:51
consider in terms of distance and for
1:56
this they are how you can calculate the
1:59
distance one is by using Euclidean
2:01
distance and the other one is by using
2:04
something called as Manhattan distance
2:06
now what I am saying is that we try to
2:09
find out which is my five nearest
2:11
neighbor from this new point okay
2:13
suppose I found out and I found out that
2:16
from this point okay let me just draw a
2:19
new one points so that you get more
2:21
clear understanding about it suppose my
2:24
new point is somebody okay now from this
2:26
new point what I do is that I have taken
2:28
my kids / - five as my near neighbor
2:31
value so what I'm going to do is that
2:33
I'm going to see that which are the five
2:34
nearest point from this point by using
2:36
Euclidean distance again to distance
2:39
parameter values Euclidean and Manhattan
2:41
discuss about you needed in America very
2:43
so my first point suppose this is my
2:46
nearest point this is my second nearest
2:48
point third nearest point for ps1 and
2:51
fifth music okay so this clip I'm near
2:54
this point I have in terms of Euclid
2:56
industry
2:56
so let us must understand what is unit
2:58
in this suppose I have two points like P
3:02
x1 comma y1 at this particular location
3:05
and I have second point that is people
3:08
x2 comma y2
3:10
now in order to find out a distance
3:12
between this point the Euclidean
3:14
distance formula is basically I just
3:16
write it on IDI I'll say does who dot X
3:20
2 minus X 1 whole square plus y 2 minus
3:25
y 1 holster so this is the formula that
3:28
we basically used for calculating the
3:30
Euclidean distance
3:31
similarly Manhattan distance works in a
3:34
different way you Manhattan distance
3:37
instead of directly calculate the point
3:39
what we do is that we create and we try
3:43
to calculate the distance first we
3:44
calculate this distance Plus this
3:47
distance the addition of this a and B
3:50
will actually give us with respect to
3:52
Manhattan distance but currently what we
3:54
are doing is that here we have shared
3:56
with respect we played in this you can
3:59
also use Manhattan distance
4:00
so from this distal parameter what I saw
4:04
that my five nearest neighbor from this
4:07
I have found out that three points
4:09
belong to one kind of category that is
4:12
mapping
4:12
category so I'll just write it all down
4:15
spin category okay then my two points by
4:21
two points actually move my blue gonna
4:25
look
4:27
now what I do is that in this particular
4:30
algorithm it says that whichever will be
4:33
having the highest category with tones
4:35
to the neighboring point okay so this
4:38
new point will actually become the pink
4:42
point because the maximum number of
4:44
categories that I have got is basically
4:46
being over here right the neighbor
4:48
so what it happens is that this new
4:50
point it reads it adds based on the
4:53
distance whatever point it is getting it
4:55
behaves in that way and it gets
4:56
classified to that particular group it's
4:58
I hope it is pretty much so in this
5:01
particular use case whenever we get a
5:03
new point and whenever we it does is
5:08
that based on the K value it will try to
5:10
find out which is the K nearest name
5:12
okay
5:13
and suppose I found out that form and
5:15
always remember this K value should be
5:17
odd if it is even then there will be a
5:20
scenario where you make it an equal
5:22
number of one equal number of categories
5:25
on both the side that is basically on
5:26
this side you may get two categories and
5:29
Aceros we make it two categories if I
5:31
select K values so always make sure you
5:33
select a value of odd and I also said
5:36
tell I'll also show you how to select
5:38
the scale of how you can come up with
5:40
that you have to make the K back - fine
5:43
okay that will check it out as we do the
5:45
practical implementation so pretty
5:48
simple algorithm in this what we do is
5:50
that we find the nearest neighbor okay
5:52
based on the K rounded up we have
5:55
selected then whichever category is
5:57
having the near maximum number of
5:59
nearest neighbor it belongs to belongs
6:00
to that particular category now this is
6:03
with respect the classification use case
6:05
okay now what about regression in
6:08
regression use case what we do is that
6:10
everything is similar okay everything is
6:13
similar okay we here also we select a K
6:16
value suppose my K value is five again
6:18
here I have my data points displayed
6:23
over here then my next two data points
6:26
are somewhere here
6:29
now whenever I get my new point whenever
6:33
I get my new point somewhere here then
6:36
what happens we will go and calculate
6:38
okay nearest neighbor
6:40
five points I will be getting will be
6:41
here okay then instead of seeing and
6:46
again this will not be of two different
6:48
categories
6:49
I'm extremely sorry about that let me
6:51
just draw a diagram once again in the
6:53
regulation from use case I will not have
6:56
classification right it will just be one
6:57
similar point right all will be
7:00
continuous values over here okay now
7:04
suppose I want to check what will be the
7:06
value with respect to this particular
7:08
okay then what it will do it will try to
7:11
find out the K nearest neighbor that is
7:13
five over here with my K value is five
7:15
then what did he do is that try to find
7:19
out the average of all this point
7:20
average mean of all this one mean of all
7:24
the nearest neighbor that it has found
7:26
out although just try to find out the
7:32
mean of all the nearest neighbor and
7:34
this particular new point will get that
7:37
particular value for then whatever mean
7:39
of this particular thing that particular
7:42
point will get that particular value as
7:44
its predicted point so this is with
7:47
respect to licorice so this was about
7:51
two different things again always
7:53
remember and K nearest neighbor will
7:55
also get impacted by outliers because if
7:59
they are lot of outliers then it will
8:01
vary it will be very difficult for a
8:03
classification is you know it may get
8:05
created in a you know it will not be
8:11
able to identify a predict the correct
8:13
category so I - outlier it
8:17
impacted with respect so make sure that
8:33
it fits all both decisions and then you
8:36
try to now this was about K nearest
8:40
neighbor now we'll go ahead and try to
8:41
understand the implementation part and
8:44
the humilation is also very very simple
8:46
and make sure that I'll upload this and
8:50
I provided link
8:52
of the github URL and miss the video
8:54
begin with guys we have some classified
8:58
data set from a company ok what we'll
9:00
try to do is that so this is this is the
9:03
data set and we I really no one will be
9:06
able to understand I have downloaded
9:07
this data set from cattle and this
9:10
particular data set has some independent
9:12
features and the output feature is the
9:13
octave plus either you have to classify
9:15
to one in zero okay so as usual I will
9:18
be importing all these libraries which
9:21
I'm basically required which is my tools
9:23
basically to solve any problems in
9:26
machine running your scripts or then I
9:29
will read the classified data then this
9:32
is how my dataset looks like now the
9:34
problem with all this feature is that
9:36
this be computer the value may be of
9:39
different different so I'll make sure
9:41
that I standardize all this variable so
9:43
follow for this what I do is that I
9:45
apply a standard scaler and apart from
9:49
the target plus I will apply the
9:51
standard scale on all these features so
9:53
here it is I've applied it and you can
9:56
see that here is my scaled feature I've
9:58
inputted standard scaler moosh like
10:00
standard scalar then I have done fit
10:02
after dropping the target plus then I
10:04
have done transform as soon as I do
10:07
transform and always remember the
10:08
standard scale or basically all the
10:11
values will get transform based on the
10:13
standard normal distribution of that
10:15
vector data okay so after doing that and
10:18
after I convert that into a date of
10:19
doing my data looks like this okay and
10:22
this is how my data looks like after the
10:25
standard scalar jump line next is that I
10:28
have so many features so let me just see
10:30
the distribution of this data by using
10:31
not so bad lot again this is a very
10:35
important technique I can just you see
10:37
bond and try to see that how my data is
10:39
actually distributed so I will be taking
10:41
dear with respect to Hui as my target
10:44
plus so that the classification point
10:45
can be shown very few now after
10:48
executing this now you can see that this
10:50
is how my data is actually distributed
10:51
now you can see that a whole lot of
10:55
distribution is there is like
10:57
intermingled lot there is lot of
11:00
overlapping so definitely I can't just
11:02
apply a large stipulation into decision
11:05
for the last time factor for using K
11:08
nearest neighbor for this okay so after
11:11
this I don't have to do train test split
11:13
also because today I am going to show
11:15
you with the help us cross validation
11:17
but just for first term I'll just be
11:19
using brain tests but later on I will be
11:21
showing you to the end of
11:22
cross-validation now we train test plate
11:25
what I do is the after doing train test
11:26
it every time everybody's familiar with
11:28
this I think they should not be across
11:30
okay now I will apply Kania equals
11:34
classifier it is getting imported from a
11:36
scale and articles and inside this I am
11:39
just passing and understood me but at
11:41
one so instead of taking it is by 2/5 of
11:44
K is equal to 6 I'm just taking a stage
11:47
but to 1 the reason I am taking it
11:49
because we have to derive this value we
11:51
have to derive the K value and that
11:53
derivation is I'll be using a simple
11:56
technique to find what should be the K
11:58
value by doing a number of iteration
12:00
which I will just show you in a wife
12:02
effects but let us just think that the K
12:05
value is 1 and try to do the fit and
12:07
after doing the spec I am doing the
12:09
Freddie after doing the predict I can
12:11
also find out the confusion matrix from
12:14
this confusion matrix you can see that
12:15
the precision and recall value I am
12:17
getting somewhere around 91% if my K
12:19
value is 1 but still it will lead to
12:22
underfitting because my cave and is just
12:24
1 and if my K value is 1 it is very
12:27
difficult to just based on just one
12:31
nearest neighbor so what I do I have to
12:33
do is that I choose a KA value now for
12:36
choosing a K value what I do is that I
12:38
have made a list called as the KC bit
12:40
then I will run a loop from 1 to 40 now
12:44
just think what this loop is all about
12:46
this one 240 values I am going to
12:49
consider the K value suppose for K is
12:51
equal to 1 I try to find out what is the
12:53
accuracy that I got with it
12:55
okay is equal to what is the accuracy
12:57
for K is equal to 3 what is they go till
13:02
40 and then I try to plot that aquiles
13:05
abate and try to see that where the
13:07
accuracy rate is quite stable and then
13:10
with respect to that K value I can
13:12
select the K value where they is quite
13:14
stable okay I can either do with
13:17
accuracy rate or error
13:19
Here I am doing with accuracy rate so
13:21
here are written for I in range of 1
13:23
comma for T so cami�n respect if I have
13:26
given him and it's Ponyville I have used
13:28
across bad school this is what I was
13:30
actually saying about crosswalks so in
13:33
this I am going to give my
13:35
knn and my dear feature which is my
13:38
standardized scale of feature all my
13:40
independent features and this is my
13:42
target plus and here the
13:44
cross-validation value is 10 okay so ten
13:48
different experiments you know how
13:49
cross-validation works train intestine
13:51
it will be internally happening for 10
13:54
different experiments the brain and test
13:56
dataset will all these beautiful then
13:57
finally I am going to append the
13:59
equation basically okay now see this
14:01
after I append this accuracy rate
14:03
similarly what I have done instead of
14:05
calculating this equivalency rate I can
14:07
also calculate error rate all I have to
14:09
do is that I just have to write indices
14:12
in the accuracy rate I just have to
14:15
write instead of this poor or mean I'll
14:17
write one - poor dog so when I do one -
14:20
poor dot mean that is given that
14:22
whatever mean of the school that I have
14:23
got with respect for accuracy the
14:25
remaining a will be the error rate so
14:27
I've basically done
14:28
so both the visualization part will
14:30
happen it so first this is my error rate
14:34
or you can also use something like NP
14:38
don't mean prediction of the spot any of
14:41
this particular method even means please
14:43
so now let us go and plot the error rate
14:47
initially I'm plotting the error rate
14:49
and trying to find out what should be
14:54
now let us go down okay as soon as I
14:58
plot always remember guys this x axis
15:00
basically might be a valid range from 1
15:03
to 40 and that means time to learn I'm
15:05
actually running like 40 different
15:07
iterations I'm running with respect to
15:09
40 different K values yeah
15:11
so here it is and here you can see that
15:13
the error is reducing the error rate is
15:16
basically reduced and the K value is 1
15:18
their rate was quite high then it came
15:21
down came down came down suddenly came
15:23
down at K 13 and it again went off but
15:26
and after this point you can see that
15:28
your K value is going down and it has
15:32
never come up again
15:33
below this particular point so let us
15:35
just consider that the K value over here
15:37
I'm going to consider it as 23 you know
15:40
because after this you can see that
15:42
after this by it is reducing okay so
15:44
here I've written a comment here you can
15:46
see that K around K is going to be K is
15:49
greater than 30 23 sorry the error rate
15:51
just tends to over over 100 point zero
15:54
six of point zero five okay so because
15:56
of that what I'm going to do is that I
15:58
am going to translate to the K I can
16:00
also check it for error rate so for that
16:02
I just comment down this particular line
16:04
and I just
16:11
for the error rate also you see that it
16:14
is similar right because the error rate
16:16
is 1 minus that crazy rate sorry this is
16:18
for my curiosity not for the energy now
16:21
this values you can see that it is going
16:24
up or then coming down but after K is
16:26
equal to 23 after this particular point
16:28
this is just going out this is actually
16:31
running within this 93% accuracy okay
16:34
oh I'll suggest that K is equal to 23
16:37
would be a very good value for selecting
16:39
the K value this is how you have to
16:41
select the K value have to see that from
16:43
where the error rate or the or the
16:45
accuracy rate was continuously stable at
16:48
a specific point and it was not like
16:50
Jake Jacqueline like how we have over
16:51
okay
16:52
so now what are you can do is that I can
16:55
just select my K nearest neighbor as 1
16:57
over here and let us go ahead and select
16:59
K neighbors 23 because I've selected
17:01
McCain Abel - 23 I do
17:03
the predict I'll find out the confusion
17:05
matrix and find out the cost now this is
17:08
what I was getting of 95 post with K is
17:12
equal to 23 so this was about the K
17:15
nearest neighbor guys are very simple
17:18
very simple algorithm wonderful already
17:22
for you all you have to do is that
17:24
download it from the github and start
17:26
working on use case there's a whole lot
17:28
of a bunch of use cases in kaggle which
17:30
you can apply with the help of cannabis
17:32
Kiba I hope you liked this particular
17:33
videos guys are please give us a thumbs
17:36
up like this particular video please do
17:38
subscribe the channel if you have not
17:39
subscribe and I'll see you all in the
17:42
next video or make sure after
17:44
subscribing you put the subscription
17:46
button notification you press the
17:48
subscription notification icon so that
17:49
you get to know whenever I upload the
17:52
next video and so I'll I'll see you all
17:54
in the next video have a great day head
17:56
or never give up keep on learning plot
17:59
test your