K-Nearest Neighbor — Transcript
Full transcript
- 0:00in this video we'll be talking about a
- 0:02type of machine learning algorithm
- 0:04called K nearest neighbor one of the
- 0:06reasons I love K nearest neighbor is
- 0:08because it's really easy to see visually
- 0:10and pretty easy to understand so let's
- 0:13get into it
- 0:14we'll be using the same example as we
- 0:16have in several of our other machine
- 0:17learning videos about trying to classify
- 0:19a mystery fish as a salmon or a tuna
- 0:22based on some properties about it here
- 0:25we'll be using the properties of weight
- 0:26and length so on this grid right here
- 0:29you see that I have lengths on the
- 0:31x-axis and weights on the y-axis and you
- 0:33see that I have these red circles
- 0:34representing tunas and these orange
- 0:37triangles representing salmon we already
- 0:39see some natural clustering right it
- 0:41seems like the tunas have a low length
- 0:44and a high weight and the salmon have a
- 0:45high length and a low weight in general
- 0:48of course there's some that are kind of
- 0:49on the border not sure about but that's
- 0:51the general trend here with K nearest
- 0:55neighbor basically how it works let me
- 0:56show you visually first before we go
- 0:58into the math because that really helps
- 1:00to solidify things if we have a mystery
- 1:02fish right so I'm gonna represent that
- 1:05with I'm gonna use grey good color for
- 1:07mystery I'm gonna use this great X to
- 1:10represent a mystery fish if I said
- 1:12here's a mystery fish with that length
- 1:13and that way would you probably say it's
- 1:16a tuna or a salmon you'd probably say
- 1:18it's a tuna right because it's literally
- 1:20close to other tuna its neighbors we're
- 1:23using that word neighbor its neighbors
- 1:25are other tunas and it has more distant
- 1:28neighbors that are other sentient so I'd
- 1:29say it's a tuna now a more ambiguous
- 1:33case would be something like if I put a
- 1:34grey X here it seems like it's kind of
- 1:36on the border but maybe leaning towards
- 1:38salmon because these two triangles these
- 1:40two salmon are really close whereas
- 1:42there's only one tuna kind of close to
- 1:44it so a little bit more ambiguous there
- 1:46let's assign some mathematical
- 1:48formulation to it to help us make these
- 1:50ideas a little more rigorous all right
- 1:53so given a mystery fish at sub high it
- 1:57only has two things about it it has a
- 1:59weight W sub I and a length L sub I now
- 2:02how do we give a mathematical notion of
- 2:05a similarity between this mystery fish
- 2:08and some other fish F sub J which maybe
- 2:11we do have data for
- 2:13for example if we have this guy this X
- 2:15is our mystery fish how do we use data
- 2:17about this tuna to help us make a
- 2:19decision so how we're gonna do that
- 2:22there's not a set way to do it that's
- 2:24one of the reasons I like K nearest
- 2:25neighbor is because you can really make
- 2:27up whatever distance function you want
- 2:28but we're gonna be using the literal
- 2:30distance function in the grid that is
- 2:32what's the Euclidean distance or
- 2:34literally the distance between that X
- 2:36and some other fish we do know details
- 2:38about so remember that is given by our
- 2:41distance formula which let me write out
- 2:43here square root of we take the
- 2:46difference between there X variables or
- 2:49the lengths so L sub I minus L sub J we
- 2:53square it and we add that the difference
- 2:56between their weights W sub I minus W
- 2:59sub J and we square it it's on a square
- 3:01root so that's basically your Euclidean
- 3:04distance between two points in the plane
- 3:06and we're using the same idea in this
- 3:08example so that's gonna be our
- 3:11similarity between mystery fish I and
- 3:15known fish J so if we calculate that
- 3:18distance between mystery fish here and
- 3:21this guy we're gonna find that distance
- 3:22is really small we find this distance is
- 3:24really small we find this distance is
- 3:25really small we can find that distance
- 3:27for every single other fish in here
- 3:30including all these salmon here and
- 3:32basically ask who are your three or five
- 3:36closest neighbors if we use the three
- 3:38closest neighbors we find out it's this
- 3:41guy this guy and this guy probably right
- 3:44so since of your three closest neighbors
- 3:47we figure out how many our salmon and
- 3:48how many our tuna in this case they're
- 3:50all tuna so we assign you as a tuna as
- 3:53well that's a very logical way to go
- 3:54about it right it basically says the
- 3:57idea of you are very similar to people
- 4:00who have similar characteristics to you
- 4:02right you have a similar outcome for
- 4:04example so that's basically how it works
- 4:07one caveat notice I said things like set
- 4:10your number of neighbors k equal to 3 or
- 4:12set it equal to 5 why did I pick odd
- 4:14numbers well if I picked 4 there's
- 4:16always the chance you're gonna have two
- 4:18salmon and 2 tuna and then you're kind
- 4:19of stuck in the water again so it's it's
- 4:23nice to pick an odd number so there's
- 4:24never a tie of course if you have
- 4:26multiple
- 4:26classes not just to then it gets a
- 4:29little bit more tricky but with two
- 4:30classes it's better to pick an odd
- 4:31number I believe okay so yeah as we have
- 4:35written in blue here you pick your K
- 4:37closest neighbors and you pick the
- 4:43majority okay so that becomes a little
- 4:47more interesting when we consider this X
- 4:49right here this mystery fish let me
- 4:50switch colors so that's not overlapping
- 4:54everything if I have this mystery fish
- 4:56here and I pick K equals three who are
- 4:58your three closest neighbors seems like
- 5:00it's this salmon this salmon and this
- 5:01tuna so in this case we have a majority
- 5:03ruling in terms of salmon so we assign
- 5:06this guy as a salmon okay
- 5:08that's how can your neighbor works
- 5:10before closing out this video I do want
- 5:12to talk about some possible obstacles
- 5:14you have here I didn't do this in a very
- 5:18careful way because the weight could be
- 5:20in any type of units right it could be
- 5:22in pounds for example length could be in
- 5:25any units it could be in inches or
- 5:26millimeters or feet or whatever so if
- 5:29weight is in pounds and lengths is in
- 5:31inches and all I'm doing is just this
- 5:34subtraction right here of lengths and
- 5:35subtraction of weights I'm basically
- 5:37implicitly
- 5:38saying that a one unit different
- 5:41difference in lengths is the same thing
- 5:43as a one unit different in weight but a
- 5:46one pound difference may not be as
- 5:48drastic or could be more drastic than a
- 5:50one inch difference or a one foot
- 5:52difference so what you really want to do
- 5:55before you carry out this process is to
- 5:58kind of normalize these variables in
- 6:01some way for example if I have my
- 6:03distribution of weights of all of my
- 6:05known fish then what I can do for a new
- 6:09way is so I have weight sub I is my new
- 6:13weight and I want to normalize it so let
- 6:15me just say prime it's going to be equal
- 6:18to the true value of that way minus the
- 6:21average this is a bar so average of all
- 6:23the weights I have so far
- 6:24divided by the standard deviation of the
- 6:27weights you're going to notice this is
- 6:28just a z-score or a normalization
- 6:33subtracting the mean and taking the
- 6:35standard dividing my standard deviation
- 6:36okay you can of course do different
- 6:39kinds of standardization on
- 6:40in max normalization if you want but the
- 6:43point is you want to somehow get all
- 6:44your variables on the same scale
- 6:46speaking of your variables here I just
- 6:48have two but you might have several more
- 6:50you can go ahead and just include those
- 6:52more terms in your Euclidean norm or
- 6:55whatever norm you want to use as your
- 6:57similarity distance maybe metric okay so
- 7:01that's a quick introduction to K nearest
- 7:04neighbor visual representation of how it
- 7:06works
- 7:06so we'll do some more videos about it in
- 7:08the future with some more considerations
- 7:10until next time
About this transcript
This page contains the full transcript of K-Nearest Neighbor by ritvikmath, generated from the public captions YouTube serves with the video. The transcript has 1,373 words across 189 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.