K-Means Clustering

ritvikmath · Beginner ·📐 ML Fundamentals ·7y ago

Key Takeaways

The video introduces the K-Means Clustering method in machine learning, covering its basics, algorithm, and limitations, including handling complex cases and requirements for linear separation between clusters.

Full Transcript

well in this video we'll be talking about a type of machine learning algorithm called k-means clustering the goal of k-means clustering is to classify some mystery point based on some information about it into one of K classes k is a number of classes you want to separate your space into for example as in many of our other machine learning videos we'll be using the example of trying to classify a fish as a salmon or a tuna based on stuff about it in this case its weight and its length so you see in the space I have several points all these red circles represent tuna and all these orange triangles represent salmon so pretend now you've got this gray X a mystery fish with this following weight and height over length and you're trying to figure out should I assign it a tuna or a salmon naturally we would say tune up because it's you know in this area that tune are in but how does k-means clustering deal with this the best way to demonstrate this algorithm is literally just to show it in action I think so as said before the first thing you have to do is decide on your K that's one of the drawbacks of this algorithm is that you need to know how many groups your space is divided two into prior to running this algorithm and sometimes you just don't know but in this case we do know that there's two groups salmon and tuna so you take the number of groups you have and you assign that many initialized needs means in this case in terms of average you wanna think of it like that so let's just say I choose one initial value right here and I choose one initial value let's say right here we're gonna see that this is a pretty bad initial approximation but we're gonna see how K means fixes this so we get the right classification so once you have these two values this mean 1 mu 1 and u 2 you can think of it that way what you do for each point in the plane is figure out is it closer to the first mean or the second me and you go ahead and assign it to that point so for example if this top one is the one that represents salmon and the bottom one represents tuna then anytime a point of all these points we see is closer to the bottom one we say it's a tuna if it's closer to the top one we say it's a sin it turns out there's a really easy way to divide the plane if we have these two points basically you just put the line that's immediately between these two guys and I'm gonna shift this up a little bit okay so this is the line which cuts off whether it's closer to the bottom point or the top point okay so for example all these points up here are closer to the top mean and all of the points down here of which there's only three or four are closer to the bottom mean so that means all the points below the line get assigned as tuna all the points above get assigned as salmon we see that all the salmon are correctly classified by the tuna are split in a pretty bad way right now so the next thing that k-means clustering does is based on all the points that are assigned to one mean or the other it takes the average of all those points so it takes the average X average Y and if we have more dimensions it takes the average of all those other dimensions and it reinitialize --is these means to those average values so let me show you graphically what that means it's a little bit easier for the bottom points because there's only three or four of them so the average value seems like it's about here where I drew that little dot so what happens is this mean shifts up and its new location is right there okay now the same thing happens with these points we see that there's more salmon so it's gonna be pulled in that direction let's just say the average value is something like there okay so this mean right here moves to its new value and the algorithm basically just reinitialize is it reclassifies all the points in our space to now see if it's closer to this mean or this mean and what that does effectively as shifts are aligned so that it is now this line I didn't explicitly say it when you shift this down because I want to make one more iteration so I didn't explicitly say it but the way I get this line is basically just saying this is the line that is equidistant between these two points so every point on this line is equidistant to both points which means that if you're on this side of line you're closer to this point if you're on this side of the line you're closer to this point and you've probably seen me fudging around these points here I promise this is just for the explanation so now that we have our two new means and our new line we reclassify whole points so that means all these guy right here are classified as tuned up all these guys up here at classified us and so we're almost there we just have a couple of mistakes right here so we reinitialize so this guy gets set to the mean of this cluster so about right there and this guy gets set as the mean of this cluster so that's gonna be further in this direction and we reinitialize so this line looks more like this now right and now we reclassify so all these get set is tuned up all these get set is Sam and visually we see that we've succeeded but the algorithm doesn't know that yet so what does it do it really breaks the means so now it's perfectly in the middle of this tunic cluster this guy's perfectly in the middle of the salmon cluster and the line is honestly not going to shift that much so how does the algorithm know when to stop basically it knows when to stop it will stop now basically because there's been no change in the assignments which means that all these that were classified as salmon before are still classified as salmon all these classified is tuna in the last iteration are still classified as students so it can go ahead and stop that is literally how canyons clustering works it just keeps shifting around these means and I've drawn a simple example with two means you could have three or four and you wouldn't have a single line anymore you would have kind of triangle or four lines if you had more clusters which divide up your plane but basically the means keep getting real initialized until there's no more change in the labels or you can set some other stopping criteria like a hundred or a thousand iterations that's basically how keen Ian's clustering works now let's talk about some of the pros and cons of k-means clustering the main Pro I would say is its simplicity and that is in terms of simplicity of understanding it we saw that it's we went through it in just a couple minutes and also its simplicity in computation think about all you have to do each iteration once you have your means you have to calculate the Euclidean distance in the plane from each of your points which isn't a very expensive operation it's just a couple subtractions squaring some numbers so and then once you have those labels you basically just classify all your points and then you would see if the classification has changed from the previous iteration and if it has then you would just reinitialize and take the mean of your new cluster so there's not that much high-level computations going on that's the main thing it has going for it of course it's going to have its limitations usually when something is really simple there's cases it just can't account for so let's look at this case specifically again we have two clusters and as human beings we see that there's a very natural grouping to them what we would do is what we want to do is basically take the circle and use that as our divider and we'd have a pretty good classification here well let's see what happens with k-means let's say that we set one mean initially here I think I lost my other mean here okay well that's fine we said the other mean to be right here okay so what happens is it sets the middle point which is say that's right there and it classifies everything above as a salmon or Orange it classifies everything below as a tuna or red for example now of course what happens is it shifts the mean so these honestly aren't going to shift much maybe this moves down a little bit maybe this kind up a little bit let's say the line for that reason stays the same because all you've done is move these guys closer to each other due to the symmetry of the problem so let's say that in the second iteration nothing gets classified differently so it thinks it succeeded but obviously it's failed miserably right it's basically gotten half of them wrong because something like that it's classified everything above as orange even though several of them are red everything below as red even though several of them are orange so when there's not a good linear separation like up here or also if the cluster sizes are very very different like if you have for example a little cluster of points over here and then you have a massive cluster of points over here that's also going to cause some problems because k-means tends to pick about the same number of points in each of the K clusters in the end which you might not necessarily want right here it just it just so happens that this cluster is smaller than this cluster so you means we'll have a problem there too and of course another problem we noticed in the beginning of the video was you need to know this K beforehand this doesn't really find the K for you there are other algorithms that will find the K but this is not one of them so you need to know how many classes you want beforehand which isn't always practical so I found that a little dot here okay so that is k-means clustering in a nutshell so we'll be looking at ways to overcome this using other models going forward and we'll see how they do better than k-means clustering and the cons they have as a result okay so until next time

Original Description

Intro to the K-Means Clustering method in machine learning
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from ritvikmath · ritvikmath · 42 of 60

1 Math Team Update
Math Team Update
ritvikmath
2 Single Variable Calculus Volume of a Sphere - Proof 1
Single Variable Calculus Volume of a Sphere - Proof 1
ritvikmath
3 Single Variable Calculus Volume of a Sphere - Proof 2
Single Variable Calculus Volume of a Sphere - Proof 2
ritvikmath
4 Multivariable Calculus Volume of a Sphere Proof - Triple Integrals
Multivariable Calculus Volume of a Sphere Proof - Triple Integrals
ritvikmath
5 Multivariable Calculus Volume of a Sphere Proof - Double Integrals
Multivariable Calculus Volume of a Sphere Proof - Double Integrals
ritvikmath
6 The Euclidian Algorithm
The Euclidian Algorithm
ritvikmath
7 Proving the Chain Rule
Proving the Chain Rule
ritvikmath
8 Proving the Fundamental Theorem of Calculus Part 1
Proving the Fundamental Theorem of Calculus Part 1
ritvikmath
9 Proving the Fundamental Theorem of Calculus Part 2
Proving the Fundamental Theorem of Calculus Part 2
ritvikmath
10 Math Puzzle - Poison Perplexity
Math Puzzle - Poison Perplexity
ritvikmath
11 Math Puzzle - Poison Perplexity - Solution
Math Puzzle - Poison Perplexity - Solution
ritvikmath
12 Expected Value and Variance of Continuous Random Variables (Calculus)
Expected Value and Variance of Continuous Random Variables (Calculus)
ritvikmath
13 Expected Value and Variance of Discrete Random Variables (No Calculus)
Expected Value and Variance of Discrete Random Variables (No Calculus)
ritvikmath
14 Array Method
Array Method
ritvikmath
15 Complex Power Series and their Derivatives
Complex Power Series and their Derivatives
ritvikmath
16 Distributions - Intro
Distributions - Intro
ritvikmath
17 The Poisson Distribution
The Poisson Distribution
ritvikmath
18 The Bernoulli Distribution
The Bernoulli Distribution
ritvikmath
19 The Binomial Distribution
The Binomial Distribution
ritvikmath
20 The Continuous Uniform Distribution
The Continuous Uniform Distribution
ritvikmath
21 The Geometric Distribution
The Geometric Distribution
ritvikmath
22 The Triangular Distribution
The Triangular Distribution
ritvikmath
23 The Exponential Distribution
The Exponential Distribution
ritvikmath
24 The Borel Distribution + Notes on Poisson Distribution
The Borel Distribution + Notes on Poisson Distribution
ritvikmath
25 The Gamma Distribution
The Gamma Distribution
ritvikmath
26 The Normal Distribution
The Normal Distribution
ritvikmath
27 The Laplace Distribution
The Laplace Distribution
ritvikmath
28 The Chi - Squared Distribution
The Chi - Squared Distribution
ritvikmath
29 Overfitting
Overfitting
ritvikmath
30 Vector Norms
Vector Norms
ritvikmath
31 Truths Behind the Titanic : K-Nearest Neighbor
Truths Behind the Titanic : K-Nearest Neighbor
ritvikmath
32 The Mathematics of Breakups
The Mathematics of Breakups
ritvikmath
33 Sillyfish
Sillyfish
ritvikmath
34 Finding Optimal Paths - Dynamic Programming
Finding Optimal Paths - Dynamic Programming
ritvikmath
35 HowToDataScience : Scraping Twitter Data
HowToDataScience : Scraping Twitter Data
ritvikmath
36 Decision Trees
Decision Trees
ritvikmath
37 Perceptron
Perceptron
ritvikmath
38 Naive Bayes
Naive Bayes
ritvikmath
39 K-Nearest Neighbor
K-Nearest Neighbor
ritvikmath
40 Evaluating Machine Learning Models
Evaluating Machine Learning Models
ritvikmath
41 Decision Tree Pruning
Decision Tree Pruning
ritvikmath
K-Means Clustering
K-Means Clustering
ritvikmath
43 Gaussian Mixture Model
Gaussian Mixture Model
ritvikmath
44 Data Science - Fuzzy Record Matching
Data Science - Fuzzy Record Matching
ritvikmath
45 Time Series Talk : Autocorrelation and Partial Autocorrelation
Time Series Talk : Autocorrelation and Partial Autocorrelation
ritvikmath
46 Time Series Talk : Autoregressive Model
Time Series Talk : Autoregressive Model
ritvikmath
47 Time Series Talk : Moving Average Model
Time Series Talk : Moving Average Model
ritvikmath
48 Time Series Talk : ARMA Model
Time Series Talk : ARMA Model
ritvikmath
49 Time Series Talk : ARCH Model
Time Series Talk : ARCH Model
ritvikmath
50 Time Series Talk : White Noise
Time Series Talk : White Noise
ritvikmath
51 Time Series Talk : Stationarity
Time Series Talk : Stationarity
ritvikmath
52 Time Series Talk : ARIMA Model
Time Series Talk : ARIMA Model
ritvikmath
53 Time Series Talk : Lag Operator
Time Series Talk : Lag Operator
ritvikmath
54 Time Series Talk : What is Seasonality ?
Time Series Talk : What is Seasonality ?
ritvikmath
55 Time Series Talk : Seasonal ARIMA Model
Time Series Talk : Seasonal ARIMA Model
ritvikmath
56 So ... What Actually is a Matrix ? : Data Science Basics
So ... What Actually is a Matrix ? : Data Science Basics
ritvikmath
57 Derivative of a Matrix : Data Science Basics
Derivative of a Matrix : Data Science Basics
ritvikmath
58 Basics of PCA (Principal Component Analysis) : Data Science Concepts
Basics of PCA (Principal Component Analysis) : Data Science Concepts
ritvikmath
59 Eigenvalues & Eigenvectors : Data Science Basics
Eigenvalues & Eigenvectors : Data Science Basics
ritvikmath
60 The Covariance Matrix : Data Science Basics
The Covariance Matrix : Data Science Basics
ritvikmath

This video teaches the basics of K-Means Clustering, including its algorithm and limitations, and how to apply it to classify points into clusters. It's essential for understanding unsupervised learning in machine learning.

Key Takeaways
  1. Decide on the number of groups (K)
  2. Assign initialized means
  3. Reinitialize means to the average of the points assigned to each mean
  4. Reclassify all the points in the space based on the new means
  5. Calculate Euclidean distance from points to each mean
  6. Reclassify points based on closest mean
  7. Update means with new cluster centers
  8. Stop when no change in labels occurs or when stopping criteria is met
💡 K-Means Clustering requires knowing the number of clusters (K) beforehand and can fail when there's not a good linear separation between clusters or when cluster sizes are very different.

Related Reads

📰
The Mental Model of PyTorch: Build a Neural Network in 5 Phases
Learn the mental model of PyTorch by building a neural network in 5 phases to understand every line of code
Medium · Deep Learning
📰
Automatic Relevance Determination Regression for Time Series Forecasting
Learn how to apply Automatic Relevance Determination Regression for accurate time series forecasting
Medium · Data Science
📰
Deploying Multi-Turn RL Infrastructure for Amazon Nova on Amazon SageMaker HyperPod
Deploy a multi-turn RL infrastructure on Amazon SageMaker HyperPod using Amazon Nova Forge and create an event-driven pipeline for automated training
AWS Machine Learning
📰
Python for Data Science — Sampling and Why Your Conclusions Can Be Wrong
Learn how sampling affects data science conclusions and why understanding probability distributions is crucial
Medium · Machine Learning
Up next
Dropout in Deep Learning
AnuTech-CH
Watch →