Python Machine Learning Tutorial #6 - KNN p.2 - How does K Nearest Neighbors Work?

Tech With Tim · Beginner ·📐 ML Fundamentals ·7y ago

Key Takeaways

The video demonstrates the K-Nearest Neighbors (KNN) algorithm, a classification technique in machine learning, using Python, and explains how it works, including the concept of Euclidean distance and the importance of the hyperparameter K. The KNN algorithm is implemented and its pros and cons are discussed, including its sensitivity to the choice of K and computational complexity.

Full Transcript

hello and welcome back to another machine learning tutorial so in today's video we're gonna be going through implementing the k-nearest now neighbors algorithm but before we do that I'm gonna talk about exactly how it works like in mathematics and yeah so if you guys are interested in learning about how this works please watch the entire video and then near the end is where I'm gonna implement it because it won't take that long to implement it but if you guys are more interested in the code you can either go look at the text-based version at Tech with Tim net where I'm also gonna have like descriptions of what's going on and all the code that I write or you can just skip forward into the video until when you see that I'm done drawing I don't know how long it's gonna take me to explain because it is a bit more complicated than linear regression but again it's not that crazy difficult okay so let's talk about the K nearest neighbors algorithm and how does this work well as we already know K nearest neighbors is a classification algorithm and the way that it works is given a data point so likes may be something like this it attempts to classify this data point with one of the classes that it knows so in this case our three classes would be well red we have green and we have blue okay now obviously these would most likely represent something but for our cases we're just gonna use colors because it's kind of easy to see and this black is gonna be well our question mark because we don't know what classicism we're trying to predict it so the way that we predict this point essentially is we look for groupings of data points right so you can see and I'm just this is not what the algorithm does this is what we're kind of doing as humans okay we see kind of groups right so we see this group of red we see this group of blue we see this group of green and if I asked you well where does this black point belong most likely you would probably say red or blue right because it's closest to blue or red I would say it's closer to red and that is where you might say that this belongs to you might say belongs to red because well it's closest to that so it would make sense if it's a red color right and that is exactly what our algorithm does now he works a bit more advanced than this but essentially it looks for kind of groupings of data points right and I'll show I'll tell you how it works but and then it will pick the closest point to this black right here and say okay so it is gonna be that class now what does this this K me so I keep saying K nearest neighbors well K is actually known as a hyper parameter and it stands for the amount of neighbors that we are going to look for so I'm just gonna do an example with K equals three okay and go through exactly what happens so given our black data point here alright and you know what actually let's just move it for the first example to be kind of easier to understand okay I'm just gonna move it right here if we have this parameter as K equals three it means we are actually gonna look for three neighbors to this black point so we're gonna find the three closest points out of all of this data to this black point so I would argue that that those points would be probably these three points right like the ones I just put the black dots on okay these three red points now once it finds these three points these points are gonna have a vote and essentially our program is gonna look at what class these points are so you can see like this one's red they're all red and they're gonna vote now since these points are red they're gonna vote for red so we're gonna get a vote of red is three green is zero and blue is zero now because red is the Heintz highest a current occurrence of that vote we are going to classify this point as being red and that is essentially the way that it works if I picked K equals five what we would look for the five closest data points now let's just I know this might not be the closest point but I just wanna do it for example let's say we have that other red point and let's say this blue point is close to this black dog maybe it's moved like over here okay what's gonna happen now is the exact same thing we're gonna look at these five data points that are the closest to this black dot and we're going to say okay we have four red so this is four I should probably have just erased that and so I'm trying to write over it and we have blue which is one okay so red four and blue one and because well red is greater than one and is the highest occurrence we are going to classify this point as red and obviously I think we would all say that is probably an appropriate classification so that is essentially how that works now let's go more into some more detailed math okay so let's say I plot my data point here okay kind of in between two clusters of data so again how does this work right so actually let's talk about why we're gonna pick an even value for K or sorry an odd value for K so let's say k equals five let's let's do this example with K equals five then we'll talk more about the math so I would say the closest data points are probably this one this one this one this one and you know what I want to say it's this one as well okay so we have two blue data points and three green data points now we see that we since we have three green and two blue we're gonna classify this as green but what if I said K equals four and instead of five so cross that out k equals four well now this data point I'm gonna scratch that out just assume that's not there now we have two green points and two blue points so how do we pick which data point that we're going to classify this as well that is it obviously why we need to pick K at to be a odd number so one three five seven nine right so that no matter what we always have a winner and we can decide on a class because right now we don't know which class this is because we'll we had a tie in terms of the voting okay so that's why we pick K to be a odd number all right so now let's go into the math so how does it actually do this what what are the mathematical methods well um you know what let's just actually scrap or I don't know what I just did there what the heck okay can I undo that okay one second how do you make this fullscreen exit out of that there we go okay sorry I don't know what I did there so let's uh scrap all this and let's actually just talk about how we get distance so remember we were saying here like if this is I blocked at a point and we had a maybe a green data point here and we had like another green data point here and another green data point here well we could probably tell this one's the closest but the computer needs to do some math to determine this right and so how does it actually know how which point is closer to which point well it's gonna draw a line from one point to the other point and it's going to find what's known as the magnitude of this line okay um you can just say that's em whatever any value for the magnitude so how can we actually determine the magnitude of this line and then based on that what are we gonna do with that so essentially let's just draw our data point again and let's call this p1 okay and in 2-space it's gonna have coordinates of x1 and y1 now let's draw another point and let's just make it a little orange point here and let's call this data point p2 okay it's gonna have coordinates x-two and y-two so based on these values just say it's a line here how do we find the magnitude of this line well there's a bunch of different ways that we can actually do this but the way that I'm gonna do it is called Euclidean distance now this is I believe the default one that our k-nearest a breeze uses it's probably one of the simplest ones and all the other ones kind of work similarly to this so essentially Euclidean distance is the absolute distance from here to here and how do we get that so we're inside D which stands for distance so you can change this to do K is equal to the square root all right it's a big square root of x2 minus x1 squared plus y2 minus y1 squared and this will apply again to any space coordinates so if I have like an X Y Z coordinate so maybe in three dimensions and we have another coordinate you're gonna do the exact same thing except what you'd add here is you'd add z2 minus z1 squared okay and you just continue the square root like that but we don't need that portion because we're not in three space so essentially what this is gonna do is will give us that absolute position now if we want to prove this on like some standard numbers just to give you an idea if we say like this is zero and this is zero and we say this is zero and this is four well what is the distance here right what's the distance between x1 y1 to x2 y2 well we're just far away on the y axis so it should be four so let's plug this into our formula and see if we get this so we x2 - x1 well that's zero squared okay plus y2 minus y1 what that's 4 so we have 4 squared so we can actually just cross out this 0 and this plus cos well that's nothing so we get the square root of 4 squared which is equal to 4 or you could say it's equal to the root 16 which is equal to 4 right and that gives us the distance for our line so Wow I just did a lot of math on here ok so now that we know that we can find the distance between all these different points here okay I'm just trying to do that to show you where I'm actually writing and based on those distances we can determine where which the closest neighbors are okay so let's do an example in 3-space to wrap it up and then let's implement our algorithm so I always draw my grids kind of weird for three sites okay so this is our three dimensional grid now the reason I'm doing this is because obviously well we have like six attributes or six features that we're going to be using to determine our data points and that means we have to plot them in three dimensions so this is the exact same thing as two dimensions except our data points are just gonna have three coordinates so instead of XY we're gonna have X Y Z right and this is I just want to show you how this works kind of in theory - okay so again we have points let's do one more points here and let's do our data point right maybe here okay now what we have to do is we have to determine like what the XY value of these this is XY value of this and XY value of this and then we have to compare based on how many neighbors we're looking for which point is which now this is where I want to get into that hyper parameter of K so what if I say K equals 9 so right now you would probably say that we want to classify this point as red because it's really close to the red data points but if I put a value of K equals 9 and we start looking for the closest points - Takai well we get these four okay but we also get like one two three four five let's imagine these move over a bit okay so what's happening now well since we picked our K to be way too high what's happening is we're now looking at points way side of the range of our data point right and that means we've found these points over here and we've said that this is going to be blowing into the purple group because well we pick too many values for K so this is just showing you an error that you can run into if you pick too many values okay so I think that is almost about it for what do you call it all this stuff I think the last thing I'm going to talk about is just some limitations to this algorithm and then maybe in the next video we're gonna implement it because it won't take very long and I realize I'm already at 11 minutes but I needed to explain this to you guys ok so some limitations well you may have already noticed that saving this model is not going to do anything for us meaning that's a like it is very computationally heavy so right now I'm only doing I don't know what is 5 10 14 data points and 15 if you include this one okay and every time that I want to classify this point I actually have to find the distance between this point and every other point right I have to I have to figure out what the distance is between every other data point that exists on this grid now this doesn't seem like a lot when you're only doing it 14 times but you can imagine that computation on tens of thousands of data points takes a long time and the reason we have to do that is because we have to know the distance to every data point so we can figure out which ones are the closest to our point right so when we save this model like you can save it if you want to but essentially it has to save every single piece of training data that we've given it every single piece because it has to look at every single data point every time we make a prediction and that means our predictions take a long time and this algorithm is essentially it's useless to train beforehand because it has to constantly keep looking at every data point before it can make a prediction on whatever data point we give it so that means that our time is going up linearly rather than being constant which would be what e-codes constant is an example of like linear regression where it doesn't matter how many times we try to predict using our models since we just created a function we can just use that function right and it takes constant time okay so I think I'm going to wrap the video up here as always if you guys have any questions please feel free to leave one in the comment down below and I'll try to help you out as best as I can and if you guys enjoyed the video please make sure you leave a like I will see you again in another one [Music] you

Original Description

So how does k nearest neighbors work? In this python machine learning tutorial I explain and illustrate how the K-nearest neighbors algorithm works. I explain euclidean distance and how points are classified and talk about the pros and cons of this algorithm. ⭐ Kite is a free AI-powered coding assistant for Python that will help you code smarter and faster. Integrates with Atom, PyCharm, VS Code, Sublime, Vim, and Spyder. I've been using Kite for 6 months and I love it! https://kite.com/download/?utm_medium=referral&utm_source=youtube&utm_campaign=techwithtim&utm_content=description-only Text-Based Tutorial & Code: https://techwithtim.net/tutorials/machine-learning-python/k-nearest-neighbors-2/ ************************************************************** WEBSITE: https://techwithtim.net proXPN VPN: https://secure.proxpn.com/?a_aid=5c34b30d44d9d Use the Code "SAVE6144" For 50% Off! One-Time Donations: https://goo.gl/pbCE9J Support the Channel: https://www.patreon.com/techwithtim Twitter: https://twitter.com/TechWithTimm Join my discord server: https://discord.gg/pr2k55t ************************************************************** Please leave a LIKE and SUBSCRIBE for more content! Tags: - Tech With Tim - Python Tutorials - Machine learning tutorial - Python machine learning - Python machine learning tutorial - Pickle python - Saving models - Matplotlib tutorial
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video teaches the basics of the K-Nearest Neighbors (KNN) algorithm, including how it works, its pros and cons, and how to implement it in Python. The KNN algorithm is a classification technique that predicts a data point's class by looking for groupings of data points and voting for the most common class.

Key Takeaways
  1. Draw a line from one point to another to find the magnitude of the line
  2. Use the Euclidean distance to measure the similarity between points
  3. Calculate the Euclidean distance between two points in 2D or 3D space using the formula sqrt((x2-x1)^2 + (y2-y1)^2) + (z2-z1)^2 for 3D coordinates
  4. Determine the number of closest neighbors to consider when making a prediction using the K-Nearest Neighbors (KNN) algorithm
  5. Classify a data point based on the majority vote of its K nearest neighbors
💡 The value of K in the KNN algorithm determines the number of closest neighbors to consider when making a prediction, and a high value of K can lead to considering points that are far away from the data point being classified, resulting in incorrect classification

Related Reads

Up next
Part 2 | MLOps On GitHub | Deploy and Automate ML Workflow |Using GitHub Actions and CML for CI & CD
Abonia Sojasingarayar
Watch →