YouTube2Text

K-Means Clustering Algorithm in Python | Practical Example | Student Clustering Example | sklearn — Transcript

by CampusX · 1,454 words · 204 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hello guys, my name is Nitesh. Welcome
  2. 0:02to my YouTube channel. We've been
  3. 0:03studying K-means clustering. In the
  4. 0:05last video, we covered its geometric
  5. 0:07intuition. In this video, I'll show you
  6. 0:09how to apply K-means to a practical
  7. 0:11data set in Python. Okay? So, let's
  8. 0:13start the video. So, guys, let's start
  9. 0:16with the coding part. I've already
  10. 0:20created a notebook in Jupyter and
  11. 0:22loaded the data set. And here's my data
  12. 0:26set. So, let me tell you about the data
  13. 0:28. First of all, it's a toy data set. I
  14. 0:31mean, I generated this data set myself.
  15. 0:33This is data from some students. If you
  16. 0:35want to think of it as a problem
  17. 0:37statement, this is data from a group of
  18. 0:39students. Data from final year students
  19. 0:42of any college. Data from 200 students.
  20. 0:45They have their CGP and IQ values. And
  21. 0:49we have to cluster these students based
  22. 0:51on these two values. Okay? Now, if I
  23. 0:55were to print out and show you what
  24. 0:59this data looks like, we'd need to
  25. 1:01import Matplotlib. Pipe Plot Edge PLT
  26. 1:11PLT Dot Scatter. I'm plotting CGPA on
  27. 1:16the x-axis and I'm plotting IQ on the
  28. 1:20y-axis. And you can see, here's my data
  29. 1:25. Now, obviously, this is a toy data
  30. 1:27set. I generated it, so you can see
  31. 1:29very clear-cut clusters here. In
  32. 1:31real-world data sets, you won't have
  33. 1:33such clear-cut clusters defined. But
  34. 1:36you can see that I have four different
  35. 1:39types of students. Now, I'll give this
  36. 1:43same data to my algorithm and see if it
  37. 1:45can correctly classify or cluster this
  38. 1:48data. Okay? So, first, we need to
  39. 1:52determine the number of clusters that
  40. 1:54will be formed. So, clearly, with the
  41. 1:56naked eye, we can see that four
  42. 1:57clusters will be formed. But let's try
  43. 1:59using the elbow method to see if that
  44. 2:01also produces four outputs. So, for the
  45. 2:06elbow method, we first need to import '
  46. 2:10means' from '
  47. 2:11sk-learn.cluster.import.kmeans'. Then,
  48. 2:19I'm creating a list called 'wcss', and
  49. 2:23'i in the range of' from 1 to 11 (you
  50. 2:27can loop up to 20). I'm creating an
  51. 2:32object called 'km', which has a
  52. 2:34hyperparameter inside it: the number of
  53. 2:37clusters. I'm defining it as 'i'. And
  54. 2:41there's a function called
  55. 2:43km.fit.predict. I'm passing my entire
  56. 2:45data frame into it. Because my entire
  57. 2:48data frame is what I need as input. And
  58. 2:51once the data is trained, there's an
  59. 2:54attribute inside KM called inertia. We
  60. 2:58need its value, and we'll keep
  61. 3:00inserting this value. Running this code
  62. 3:08like this in WCSS, here's our WCSS. You
  63. 3:17can see it starting with a very large
  64. 3:19value and then decreasing very rapidly.
  65. 3:22Now, if we print this out using a plt (
  66. 3:25) dot plot, we plotted the X-axis from
  67. 3:281 to 21, and our Y-axis shows WSS, and
  68. 3:34it wasn't 21, sorry, it was 11. So,
  69. 3:38guys, and you can see that it's
  70. 3:41decreasing very rapidly, and four is
  71. 3:44the elbow point where our stiffness
  72. 3:48rate suddenly decreases. Okay, so the
  73. 3:52elbow curve is also telling us what the
  74. 3:54number of clusters should be, four.
  75. 3:56Okay, so what do we do now? We'll
  76. 3:59create another KM object, with the
  77. 4:01number of clusters equal to four, and
  78. 4:05we'll write KM; before that, let's do
  79. 4:08one more thing. We'll extract a
  80. 4:11measured array of x. All rows, all
  81. 4:16columns, dot values. This measured
  82. 4:19array will be a little easier to deal
  83. 4:20with. That's why I did this. And now
  84. 4:23I'll write KM dot fit predict. What
  85. 4:28Predict does is train you and return
  86. 4:30the cluster values. Here, I'll pass in
  87. 4:34x and you'll see that all of my data is
  88. 4:37assigned a cluster. Two for the first
  89. 4:40row, one for the next, and so on. I can
  90. 4:43call this y-means. Okay? And here's the
  91. 4:48y-means. Now, what do I need to do?
  92. 4:51Plot a graph. And how will that graph
  93. 4:54be plotted? It's very simple. Here's my
  94. 4:57x, okay? All the rows are here. What do
  95. 5:00I need to do? Do Boolean indexing here,
  96. 5:03using y-means. If I give y-means equal
  97. 5:07to 0, then these are all the rows that
  98. 5:11fall into cluster number zero. These
  99. 5:15are all the rows that fall into cluster
  100. 5:17number one. These are the rows that
  101. 5:19fall into cluster number three. And
  102. 5:22these are the rows that fall into
  103. 5:23cluster number three. Okay? Now, if I
  104. 5:26want to extract the first value of all,
  105. 5:28I'll write here comma 0 (the first
  106. 5:30value has been extracted). And if I
  107. 5:33want to extract the second value (113,
  108. 5:36110, 109), I'll write here comma 1.
  109. 5:39Okay, so this is the logic I'm using to
  110. 5:41plot the graph. Look carefully. So if I
  111. 5:45type plt dot scatter, first I'll focus
  112. 5:50on cluster zero, so I'll send zero and
  113. 5:54zero here. So what did I do in this
  114. 5:57step? I took the x coordinate of all my
  115. 6:00points falling in cluster number zero.
  116. 6:02Okay? And then copied it and took the y
  117. 6:06coordinate of all the points falling in
  118. 6:10cluster number zero. And we assigned it
  119. 6:14a color: blue. Okay? And copied it.
  120. 6:20Pasted it four times. Next time, we
  121. 6:23picked cluster one and assigned it red.
  122. 6:29The next one, green. This is the value
  123. 6:34of cluster two. After that, three. And
  124. 6:38this last one, we can assign yellow.
  125. 6:42And here's our output, guys, and you
  126. 6:44can see that our clustering algorithm
  127. 6:46is able to cluster all the students. So
  128. 6:49, if you want to look at or understand
  129. 6:52this data as a data analyst, the
  130. 6:54students in red are those who have both
  131. 6:57a high CGPA and a high IQ. This means
  132. 7:01they are both intelligent and
  133. 7:03hardworking. Okay? If you talk about
  134. 7:08the students in blue, these are
  135. 7:09students whose IQ isn't very high, but
  136. 7:12their CGPA is quite high. So, these are
  137. 7:15the students who are very hardworking.
  138. 7:17Okay? Next is the yellow one. The
  139. 7:20yellow ones are those students who have
  140. 7:22a high IQ but a low CGPA. This means
  141. 7:24these are the students who are a little
  142. 7:25more laid-back. They live a relaxed
  143. 7:27life. They are smart. Okay? And finally
  144. 7:30, you have students who have neither a
  145. 7:32high CGPA nor a high IQ. So, obviously,
  146. 7:35you have to work hard on these students
  147. 7:37. So, if you're the head of a placement
  148. 7:40training and placement cell and you
  149. 7:42need to plan training for these kids,
  150. 7:44the Reds don't need much guidance. You
  151. 7:48don't actually need much guidance for
  152. 7:49the Blues either, because they're
  153. 7:50sincere. Do you understand? You need to
  154. 7:54motivate the Yellows because they're
  155. 7:56already intelligent, and the Greens
  156. 7:58need a lot of teaching. Okay? So, this
  157. 8:02way, you can plan your approach, based
  158. 8:04on your data. Similarly, you can
  159. 8:08cluster customers, songs, movies, and
  160. 8:11images. Okay? So, I'll show you one
  161. 8:15more thing in this video. Mostly,
  162. 8:18whenever you see practical
  163. 8:19implementations of means on YouTube,
  164. 8:21they demonstrate it in 2D. I'll also
  165. 8:24demonstrate this on 3D data to show you
  166. 8:26how this entire algorithm looks and
  167. 8:28feels in 3D. So, I've already written
  168. 8:31the code for Guice 3D. I generated a
  169. 8:35data set myself using the make blobs
  170. 8:37function, where you can specify the
  171. 8:40type of clustering you want. I
  172. 8:44generated a data set where my data for
  173. 8:47x has three columns. You can see,
  174. 8:50here's x. So this time there are three
  175. 8:52different columns instead of two. And
  176. 8:55when I plotted it using Plotly, here's
  177. 8:58the data. Okay? You can see it in 3D.
  178. 9:03And this is what our data looks like.
  179. 9:04Okay? So clearly, we have four clusters
  180. 9:07here too. And we have to do everything
  181. 9:10exactly the same way. This code is
  182. 9:12exactly the same as what we did a
  183. 9:14little while ago above. We ran it, and
  184. 9:17then when we plotted the elbow curve,
  185. 9:19the graph was again very similar. We
  186. 9:23have an elbow point coming up at four.
  187. 9:26So I trained a new model with the
  188. 9:28number of clusters being four. I took
  189. 9:31the Y mean and created a data frame
  190. 9:34with X values of 0, 1, and 2, and a
  191. 9:36random number of values. And when I put
  192. 9:40it back into Plotly, here's the output
  193. 9:42of the Guice clustering. And you can
  194. 9:45see that whatever you were able to do
  195. 9:47in 2D, as it is, you can do the same in
  196. 9:493D, and the same thing will remain true
  197. 9:51in higher dimensions. So that's why
  198. 9:55CMEans is such a powerful algorithm
  199. 9:57that the same piece of code will work
  200. 10:00in any dimension. Okay? So, once you've
  201. 10:03worked on these data sets, develop your
  202. 10:05understanding a little. And if you
  203. 10:08liked this video, please like, share,
  204. 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.