K-Means Clustering Algorithm in Python | Practical Example | Student Clustering Example | sklearn — Transcript
Full transcript
- 0:00Hello guys, my name is Nitesh. Welcome
- 0:02to my YouTube channel. We've been
- 0:03studying K-means clustering. In the
- 0:05last video, we covered its geometric
- 0:07intuition. In this video, I'll show you
- 0:09how to apply K-means to a practical
- 0:11data set in Python. Okay? So, let's
- 0:13start the video. So, guys, let's start
- 0:16with the coding part. I've already
- 0:20created a notebook in Jupyter and
- 0:22loaded the data set. And here's my data
- 0:26set. So, let me tell you about the data
- 0:28. First of all, it's a toy data set. I
- 0:31mean, I generated this data set myself.
- 0:33This is data from some students. If you
- 0:35want to think of it as a problem
- 0:37statement, this is data from a group of
- 0:39students. Data from final year students
- 0:42of any college. Data from 200 students.
- 0:45They have their CGP and IQ values. And
- 0:49we have to cluster these students based
- 0:51on these two values. Okay? Now, if I
- 0:55were to print out and show you what
- 0:59this data looks like, we'd need to
- 1:01import Matplotlib. Pipe Plot Edge PLT
- 1:11PLT Dot Scatter. I'm plotting CGPA on
- 1:16the x-axis and I'm plotting IQ on the
- 1:20y-axis. And you can see, here's my data
- 1:25. Now, obviously, this is a toy data
- 1:27set. I generated it, so you can see
- 1:29very clear-cut clusters here. In
- 1:31real-world data sets, you won't have
- 1:33such clear-cut clusters defined. But
- 1:36you can see that I have four different
- 1:39types of students. Now, I'll give this
- 1:43same data to my algorithm and see if it
- 1:45can correctly classify or cluster this
- 1:48data. Okay? So, first, we need to
- 1:52determine the number of clusters that
- 1:54will be formed. So, clearly, with the
- 1:56naked eye, we can see that four
- 1:57clusters will be formed. But let's try
- 1:59using the elbow method to see if that
- 2:01also produces four outputs. So, for the
- 2:06elbow method, we first need to import '
- 2:10means' from '
- 2:11sk-learn.cluster.import.kmeans'. Then,
- 2:19I'm creating a list called 'wcss', and
- 2:23'i in the range of' from 1 to 11 (you
- 2:27can loop up to 20). I'm creating an
- 2:32object called 'km', which has a
- 2:34hyperparameter inside it: the number of
- 2:37clusters. I'm defining it as 'i'. And
- 2:41there's a function called
- 2:43km.fit.predict. I'm passing my entire
- 2:45data frame into it. Because my entire
- 2:48data frame is what I need as input. And
- 2:51once the data is trained, there's an
- 2:54attribute inside KM called inertia. We
- 2:58need its value, and we'll keep
- 3:00inserting this value. Running this code
- 3:08like this in WCSS, here's our WCSS. You
- 3:17can see it starting with a very large
- 3:19value and then decreasing very rapidly.
- 3:22Now, if we print this out using a plt (
- 3:25) dot plot, we plotted the X-axis from
- 3:281 to 21, and our Y-axis shows WSS, and
- 3:34it wasn't 21, sorry, it was 11. So,
- 3:38guys, and you can see that it's
- 3:41decreasing very rapidly, and four is
- 3:44the elbow point where our stiffness
- 3:48rate suddenly decreases. Okay, so the
- 3:52elbow curve is also telling us what the
- 3:54number of clusters should be, four.
- 3:56Okay, so what do we do now? We'll
- 3:59create another KM object, with the
- 4:01number of clusters equal to four, and
- 4:05we'll write KM; before that, let's do
- 4:08one more thing. We'll extract a
- 4:11measured array of x. All rows, all
- 4:16columns, dot values. This measured
- 4:19array will be a little easier to deal
- 4:20with. That's why I did this. And now
- 4:23I'll write KM dot fit predict. What
- 4:28Predict does is train you and return
- 4:30the cluster values. Here, I'll pass in
- 4:34x and you'll see that all of my data is
- 4:37assigned a cluster. Two for the first
- 4:40row, one for the next, and so on. I can
- 4:43call this y-means. Okay? And here's the
- 4:48y-means. Now, what do I need to do?
- 4:51Plot a graph. And how will that graph
- 4:54be plotted? It's very simple. Here's my
- 4:57x, okay? All the rows are here. What do
- 5:00I need to do? Do Boolean indexing here,
- 5:03using y-means. If I give y-means equal
- 5:07to 0, then these are all the rows that
- 5:11fall into cluster number zero. These
- 5:15are all the rows that fall into cluster
- 5:17number one. These are the rows that
- 5:19fall into cluster number three. And
- 5:22these are the rows that fall into
- 5:23cluster number three. Okay? Now, if I
- 5:26want to extract the first value of all,
- 5:28I'll write here comma 0 (the first
- 5:30value has been extracted). And if I
- 5:33want to extract the second value (113,
- 5:36110, 109), I'll write here comma 1.
- 5:39Okay, so this is the logic I'm using to
- 5:41plot the graph. Look carefully. So if I
- 5:45type plt dot scatter, first I'll focus
- 5:50on cluster zero, so I'll send zero and
- 5:54zero here. So what did I do in this
- 5:57step? I took the x coordinate of all my
- 6:00points falling in cluster number zero.
- 6:02Okay? And then copied it and took the y
- 6:06coordinate of all the points falling in
- 6:10cluster number zero. And we assigned it
- 6:14a color: blue. Okay? And copied it.
- 6:20Pasted it four times. Next time, we
- 6:23picked cluster one and assigned it red.
- 6:29The next one, green. This is the value
- 6:34of cluster two. After that, three. And
- 6:38this last one, we can assign yellow.
- 6:42And here's our output, guys, and you
- 6:44can see that our clustering algorithm
- 6:46is able to cluster all the students. So
- 6:49, if you want to look at or understand
- 6:52this data as a data analyst, the
- 6:54students in red are those who have both
- 6:57a high CGPA and a high IQ. This means
- 7:01they are both intelligent and
- 7:03hardworking. Okay? If you talk about
- 7:08the students in blue, these are
- 7:09students whose IQ isn't very high, but
- 7:12their CGPA is quite high. So, these are
- 7:15the students who are very hardworking.
- 7:17Okay? Next is the yellow one. The
- 7:20yellow ones are those students who have
- 7:22a high IQ but a low CGPA. This means
- 7:24these are the students who are a little
- 7:25more laid-back. They live a relaxed
- 7:27life. They are smart. Okay? And finally
- 7:30, you have students who have neither a
- 7:32high CGPA nor a high IQ. So, obviously,
- 7:35you have to work hard on these students
- 7:37. So, if you're the head of a placement
- 7:40training and placement cell and you
- 7:42need to plan training for these kids,
- 7:44the Reds don't need much guidance. You
- 7:48don't actually need much guidance for
- 7:49the Blues either, because they're
- 7:50sincere. Do you understand? You need to
- 7:54motivate the Yellows because they're
- 7:56already intelligent, and the Greens
- 7:58need a lot of teaching. Okay? So, this
- 8:02way, you can plan your approach, based
- 8:04on your data. Similarly, you can
- 8:08cluster customers, songs, movies, and
- 8:11images. Okay? So, I'll show you one
- 8:15more thing in this video. Mostly,
- 8:18whenever you see practical
- 8:19implementations of means on YouTube,
- 8:21they demonstrate it in 2D. I'll also
- 8:24demonstrate this on 3D data to show you
- 8:26how this entire algorithm looks and
- 8:28feels in 3D. So, I've already written
- 8:31the code for Guice 3D. I generated a
- 8:35data set myself using the make blobs
- 8:37function, where you can specify the
- 8:40type of clustering you want. I
- 8:44generated a data set where my data for
- 8:47x has three columns. You can see,
- 8:50here's x. So this time there are three
- 8:52different columns instead of two. And
- 8:55when I plotted it using Plotly, here's
- 8:58the data. Okay? You can see it in 3D.
- 9:03And this is what our data looks like.
- 9:04Okay? So clearly, we have four clusters
- 9:07here too. And we have to do everything
- 9:10exactly the same way. This code is
- 9:12exactly the same as what we did a
- 9:14little while ago above. We ran it, and
- 9:17then when we plotted the elbow curve,
- 9:19the graph was again very similar. We
- 9:23have an elbow point coming up at four.
- 9:26So I trained a new model with the
- 9:28number of clusters being four. I took
- 9:31the Y mean and created a data frame
- 9:34with X values of 0, 1, and 2, and a
- 9:36random number of values. And when I put
- 9:40it back into Plotly, here's the output
- 9:42of the Guice clustering. And you can
- 9:45see that whatever you were able to do
- 9:47in 2D, as it is, you can do the same in
- 9:493D, and the same thing will remain true
- 9:51in higher dimensions. So that's why
- 9:55CMEans is such a powerful algorithm
- 9:57that the same piece of code will work
- 10:00in any dimension. Okay? So, once you've
- 10:03worked on these data sets, develop your
- 10:05understanding a little. And if you
- 10:08liked this video, please like, share,
- 10:10and subscribe. Thanks for watching.
About this transcript
This page contains the full transcript of K-Means Clustering Algorithm in Python | Practical Example | Student Clustering Example | sklearn by CampusX, generated from the public captions YouTube serves with the video. The transcript has 1,454 words across 204 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 choose a caption track available on the video.
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, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.