YouTube2Text

DBSCAN Clustering Algorithms | Density Based Clustering | How DBSCAN Works | CampusX — Transcript

by CampusX · 5,140 words · 740 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hi guys, my name is Nitesh and you are
  2. 0:02welcome to my YouTube channel. In this
  3. 0:05video, we are going to cover a very
  4. 0:06important clustering algorithm called
  5. 0:08DBSCAN. I hope you must have heard of
  6. 0:11it. So, my plan for this video is to
  7. 0:14first give you the geometric intuition
  8. 0:16of this algorithm. After that, I will
  9. 0:18explain a small Python code and lastly,
  10. 0:20I will share a visualization tool with
  11. 0:22you so that your intuition becomes very
  12. 0:25solid. So this algorithm is pretty
  13. 0:26important. It is asked a lot in
  14. 0:28interviews. So I would recommend that
  15. 0:29you please watch this video until the
  16. 0:31end. Let's start the video. So before
  17. 0:33understanding DBSCAN, you need to know
  18. 0:35why there is a need to study DBSCAN. We
  19. 0:38had already studied a clustering
  20. 0:40algorithm, K-Means. It was a pretty
  21. 0:43good algorithm. Then why do we need to
  22. 0:45study a new clustering algorithm? We
  23. 0:48will address this first. So K-Means,
  24. 0:52our clustering algorithm, was very good
  25. 0:55but it had some flaws, and those flaws
  26. 0:59were such that they needed to be solved
  27. 1:02. I will tell you two or three flaws of
  28. 1:05K-Means so you understand why a new
  29. 1:07clustering algorithm was needed. So the
  30. 1:11first and the biggest flaw that comes
  31. 1:14with K-Means is that you have to
  32. 1:17specify or tell the algorithm how many
  33. 1:21clusters should be formed. This is a
  34. 1:26huge problem with K-Means, that before
  35. 1:28starting the clustering, you have to
  36. 1:29tell it how many clusters should be
  37. 1:31formed. Now imagine the data you have
  38. 1:35is in high dimensions. Let's say it is
  39. 1:3710-dimensional data. So you cannot plot
  40. 1:42it. Then basically, you won't even
  41. 1:44understand how many clusters should be
  42. 1:46formed. So we studied all these
  43. 1:49techniques where we tried to figure out
  44. 1:52the number of clusters by plotting the
  45. 1:54elbow method. But the problem with that
  46. 1:59is sometimes the elbow curve is very
  47. 2:01ambiguous. It's hard to understand what
  48. 2:04the correct value is. So for all these
  49. 2:07reasons, it becomes a bit difficult to
  50. 2:09work with K-Means. So this is reason
  51. 2:12number one. Reason number two is that
  52. 2:16K-Means is very, very sensitive to
  53. 2:19outliers. Because you are constantly
  54. 2:23calculating distances of every centroid
  55. 2:25with every data point. Now, if a data
  56. 2:29point turns out to be an outlier, like
  57. 2:31this particular data point. Then what
  58. 2:33will it do? It will shift all your
  59. 2:36centroids from their positions. Right?
  60. 2:40You can see here that all these
  61. 2:41centroids should have been at the
  62. 2:43center of their respective clusters.
  63. 2:44But because of this data point,
  64. 2:46everything shifted. Again, definitely
  65. 2:49not a good scenario. Right? Reason
  66. 2:53number three is the most important
  67. 2:55reason, because K-Means is a
  68. 2:57centroid-based clustering algorithm.
  69. 3:01Meaning, its entire mechanics revolve
  70. 3:03around centroid calculation. So, if you
  71. 3:07give K-Means some weirdly shaped data,
  72. 3:10K-Means starts struggling. And you can
  73. 3:14see this is one example where we gave
  74. 3:17it a dataset that was non-spherical,
  75. 3:20and K-Means clustered it completely
  76. 3:23incorrectly. You can see that. And this
  77. 3:27is true for many other types of
  78. 3:29datasets. Where it doesn't get
  79. 3:32spherical-type data. So, because of
  80. 3:35these three reasons, the machine
  81. 3:37learning community realized that
  82. 3:39K-Means alone wouldn't suffice. There
  83. 3:42are certain scenarios and datasets
  84. 3:45where K-Means performs well. But at the
  85. 3:48same time, there are many other
  86. 3:50scenarios where K-Means fails. And this
  87. 3:53is where a new requirement was created.
  88. 3:56And to solve that, who enters the
  89. 3:58picture? DBSCAN. There is one more
  90. 4:01thing you should know before studying
  91. 4:02DBSCAN, and that is what density-based
  92. 4:04clustering algorithms are. So, as you
  93. 4:08know, you can call K-Means a
  94. 4:10centroid-based clustering algorithm.
  95. 4:15Because here, the entire logic behind
  96. 4:17clustering comes through centroids. You
  97. 4:21create centroids, calculate distances,
  98. 4:23move points, and create new clusters.
  99. 4:26That is how K-Means works. So we call
  100. 4:28this centroid-based clustering. But
  101. 4:30DBSCAN follows a completely different
  102. 4:33style of clustering. And that style is
  103. 4:36called density-based clustering. In
  104. 4:39density-based clustering algorithms,
  105. 4:42you cluster your entire data with the
  106. 4:44help of density between data points.
  107. 4:47Let me try to explain it to you. Here
  108. 4:51you see some data, and you can see that
  109. 4:53roughly two clusters are forming in
  110. 4:55this data. But how do density-based
  111. 4:58clustering algorithms understand this?
  112. 5:01They see that this is a dense region.
  113. 5:05Meaning, in this particular region,
  114. 5:07there are a lot of data points. This is
  115. 5:10a slightly sparse region. Meaning,
  116. 5:14there is a lower number of data points
  117. 5:16here. And this is again a dense region.
  118. 5:21So, with the help of this sparse region
  119. 5:24, what can DBSCAN do? It can separate
  120. 5:28out the two dense regions. can create
  121. 5:30different clusters. So, this is the
  122. 5:32main idea behind density-based
  123. 5:34clustering. That what are you doing?
  124. 5:37You are performing clustering based on
  125. 5:39the density of data points. So, one
  126. 5:42good example is obviously, as I have
  127. 5:44told you so far, DBSCAN. In fact, if
  128. 5:48you want to know the full name of
  129. 5:49DBSCAN, the full name is Density-Based.
  130. 5:55Spatial Clustering of Applications with
  131. 6:01Noise, this is the full name. And trust
  132. 6:08me, by the end of this video, you will
  133. 6:10understand exactly why this is the name
  134. 6:12. Okay? Another good algorithm is
  135. 6:15OPTICS, which does density-based
  136. 6:17clustering. So, it was necessary to
  137. 6:20give you this introduction. Because if
  138. 6:22you don't even know how density-based
  139. 6:24clustering works? Then you won't be
  140. 6:26able to understand DBSCAN that well.
  141. 6:29Let me explain this to you quickly with
  142. 6:30an example. Suppose we have this
  143. 6:33dataset, and for a while, we assume our
  144. 6:36Epsilon value is 1 unit. 1 unit could
  145. 6:40mean 1 centimeter, it could be 1
  146. 6:42millimeter, or 1 kilometer. It does not
  147. 6:45matter. And we have assumed MinPts to
  148. 6:50be three. Okay? If it is three or more
  149. 6:55than three, only then will we call it
  150. 6:57dense. Otherwise, we will call it
  151. 6:59sparse. So, what do we have to do?
  152. 7:02Suppose we want to find out for this
  153. 7:04particular point if it is dense or
  154. 7:07sparse at this point. Then we first
  155. 7:10have to draw this 1-unit circle. So, I
  156. 7:14will quickly draw a circle. So, let's
  157. 7:16say this is the circle. Obviously, I am
  158. 7:18not drawing this with exact
  159. 7:20measurements. But let's say this is the
  160. 7:22circle. Okay? Now, in this circle, we
  161. 7:25will count how many points there are.
  162. 7:27So, we have 1, 2, 3, and four. In total
  163. 7:31, there are four points, which is
  164. 7:33greater than three. So, I will say that
  165. 7:36the density around this point is what I
  166. 7:39will call dense. Okay? Let's take
  167. 7:43another example. Let's talk about this
  168. 7:45point. So, for this point as well, we
  169. 7:48have to draw a circle. So, let's say we
  170. 7:51drew this circle. What is its radius as
  171. 7:56well? Which is 1 unit. Okay? Now, here
  172. 8:00too, we will count how many points are
  173. 8:03in this neighborhood, in this circle.
  174. 8:05So, here there is one point and two
  175. 8:07points. Since this is less than three,
  176. 8:10I can say that the density here is
  177. 8:13sparse. And in this way, I can
  178. 8:16calculate the density around any point.
  179. 8:21So basically, these min points and this
  180. 8:23epsilon help us calculate the density
  181. 8:26around any point. And fun fact, these
  182. 8:30are your two hyperparameters that you
  183. 8:34will need to tune while using this
  184. 8:37algorithm. That is why I told you that
  185. 8:40learning both of these is super
  186. 8:42important. Next, we will learn about
  187. 8:44three types of points that you
  188. 8:46encounter in DBSCAN. Which three points
  189. 8:48? The first is core points, the second
  190. 8:51is border points, and the third is
  191. 8:53noise points. And trust me, all three
  192. 8:55are very important with respect to
  193. 8:57DBSCAN. So you should know what is
  194. 8:59called a core point, what is a border
  195. 9:01point, and what is a noise point. The
  196. 9:04good part is that the concept is pretty
  197. 9:06simple. Especially if you have learned
  198. 9:09the concept of min points and the
  199. 9:11concept of epsilon. Okay? So with the
  200. 9:15help of this diagram, we will learn how
  201. 9:17to identify these three points. Let's
  202. 9:20talk about the core point first. So a
  203. 9:24core point is a point that has, in its
  204. 9:27epsilon neighborhood, more than the min
  205. 9:33points number of data points. I am
  206. 9:36repeating it once again. A core point
  207. 9:39is a data point whose epsilon
  208. 9:42neighborhood—epsilon neighborhood
  209. 9:45means this circle whose radius is
  210. 9:48epsilon, and the value of epsilon you
  211. 9:51provide as a hyperparameter—is a data
  212. 9:55point such that the number of data
  213. 9:58points in its epsilon neighborhood is
  214. 10:01greater than or equal to min points.
  215. 10:05Okay? So let's look at this diagram.
  216. 10:07Suppose we have taken the value of min
  217. 10:09points as five and the value of epsilon
  218. 10:11is some epsilon. So focus on this point
  219. 10:15. If you take this point and draw a
  220. 10:18circle of epsilon radius, you will get
  221. 10:21this red-colored circle. Now, what you
  222. 10:24need to do is count how many data
  223. 10:26points are in this red-colored circle.
  224. 10:28You have to count them. So you can see
  225. 10:311, 2, 3, 4, 5; since it is equal to the
  226. 10:36min points—not more, but equal—then
  227. 10:41this point becomes a core point. It is
  228. 10:46a simple concept. If the number of data
  229. 10:50points in the epsilon neighborhood of
  230. 10:52any data point is equal to or greater
  231. 10:54than the min points, you will call it a
  232. 10:56core point. I hope you understood. Now
  233. 11:01let's move on to the second type of
  234. 11:04point that you will see in DBSCAN, and
  235. 11:07we call that a border point. So a
  236. 11:12border point is a data point whose
  237. 11:15epsilon neighborhood has fewer than the
  238. 11:21min points number of data points. But
  239. 11:25it has at least one core point in its
  240. 11:28epsilon neighborhood. Listen carefully.
  241. 11:31It is very simple. There are two
  242. 11:33conditions. A border point is a data
  243. 11:36point that has fewer data points than
  244. 11:39min points in its epsilon neighborhood.
  245. 11:43But it must have at least one core
  246. 11:47point. For example, if you focus on
  247. 11:50this. You draw a circle of epsilon
  248. 11:53radius for this as well, which we call
  249. 11:55an epsilon neighborhood. And now you
  250. 11:58count how many data points are there. 1
  251. 12:01, 2, 3, which is less than five. So, it
  252. 12:06is not a core point. But in this entire
  253. 12:10neighborhood, there is this point which
  254. 12:12is a core point itself. So, both
  255. 12:15criteria are satisfied. And this data
  256. 12:17point becomes your border point. So,
  257. 12:20let me summarize it once again. A point
  258. 12:23that has fewer than min points in its
  259. 12:26epsilon neighborhood but has at least
  260. 12:30one core point is called a border point
  261. 12:34. Simple. Now let's talk about the
  262. 12:37noise point, which is the third type of
  263. 12:39point. So, the definition is very
  264. 12:41simple. If a data point is neither a
  265. 12:44core point nor a border point, then you
  266. 12:48call it a noise point. For example, for
  267. 12:53this data point, you drew a circle of
  268. 12:56epsilon radius which we call an epsilon
  269. 12:59neighborhood, and we counted how many
  270. 13:02data points are here. One, two, which
  271. 13:06is less than five. So clearly, it
  272. 13:10cannot be a core point. Now, it is not
  273. 13:14a core point, and there is no other
  274. 13:16core point in its epsilon neighborhood,
  275. 13:18so it won't be a border point either.
  276. 13:20So if it's neither a core point nor a
  277. 13:22border point, what is left? A noise
  278. 13:24point. And that’s the concept. If you
  279. 13:27talk in the context of a real dataset.
  280. 13:30Suppose you have a dataset like this,
  281. 13:33then you can easily identify the core
  282. 13:35points, border points, and noise points
  283. 13:37in this dataset. So, the points you see
  284. 13:42in the main area are your core points,
  285. 13:45which basically form the pattern of
  286. 13:48your data. The blue points on the
  287. 13:52periphery or the border are our border
  288. 13:55points. And the outliers in the distant
  289. 13:59areas are our noise points. So,
  290. 14:03tomorrow when you perform clustering on
  291. 14:05any dataset, you will see this kind of
  292. 14:08categorization between core points,
  293. 14:10border points, and noise points. Now,
  294. 14:13we just have to learn one final concept
  295. 14:16before learning DBSCAN, and that
  296. 14:18concept is density-connected points.
  297. 14:21Now, what are density-connected points?
  298. 14:23I will tell you in very simple words.
  299. 14:27If two points A and B are
  300. 14:29density-connected, it simply means that
  301. 14:33you can take both of them and put them
  302. 14:36into one cluster. That's it. Listen
  303. 14:41again. If two points A and B are
  304. 14:45density-connected, it simply means that
  305. 14:48you can put them both into one cluster.
  306. 14:53And that's the meaning of
  307. 14:54density-connected points. But the
  308. 14:58bigger question is, how do we know if
  309. 15:00these two points are density-connected?
  310. 15:04The answer to that is also very simple.
  311. 15:06Suppose you have two points, A and B.
  312. 15:13Now, if they are indirectly connected
  313. 15:17to each other via a lot of core points.
  314. 15:24And all of the adjacent pairs of core
  315. 15:28points are at a distance of less than
  316. 15:32or equal to Epsilon, then A and B have
  317. 15:36become density-connected points. I know
  318. 15:41it can be a bit confusing, but
  319. 15:43understand this. If A and B are
  320. 15:46indirectly connected to each other with
  321. 15:49the help of a lot of core points. And
  322. 16:00the specialty of all these core points
  323. 16:03is that you pick any two adjacent pairs
  324. 16:05among them. The distance between them
  325. 16:09will always be less than or equal to
  326. 16:12Epsilon. If this criteria matches
  327. 16:16throughout, then we will call A and B
  328. 16:22density-connected. When will this
  329. 16:25criteria fail? There are two cases. The
  330. 16:28first case is that any point in between
  331. 16:31is not a core point. It becomes a
  332. 16:33border point or a noise point. Then
  333. 16:36this path will break, and then A and B
  334. 16:39will not be density-connected. There
  335. 16:43could be a second case where they are
  336. 16:46both core points, but the distance
  337. 16:49between them is greater than Epsilon,
  338. 16:52so the path will break again, and A and
  339. 16:55B will not remain density-connected.
  340. 17:00You will have to put them into separate
  341. 17:02clusters. And that's the concept of
  342. 17:06density-connected points. So, I will
  343. 17:08summarize it once again. If two points
  344. 17:11A and B are density-connected, it
  345. 17:12simply means that you can put both of
  346. 17:14them into one cluster. How do we know
  347. 17:17if two points are density-connected?
  348. 17:19It's very simple. If those two points
  349. 17:22are connected by a sequence of core
  350. 17:24points and the distance between any
  351. 17:27adjacent pair in that sequence is less
  352. 17:29than or equal to Epsilon, then the two
  353. 17:32points we have are density-connected.
  354. 17:35If even one of these criteria fails,
  355. 17:37then those two points are not
  356. 17:39density-connected. So now we have
  357. 17:42studied all the concepts required to
  358. 17:44understand DBSCAN. We know Min Points.
  359. 17:46We know Epsilon. We know about three
  360. 17:49types of points. Border point, noise
  361. 17:51point, core point. And we also know the
  362. 17:54concept of density-connected points. So
  363. 17:56now we are ready to understand the
  364. 17:58DBSCAN algorithm. So guys, now I will
  365. 18:01explain the entire DBSCAN algorithm to
  366. 18:03you very well. This entire algorithm
  367. 18:06executes in a total of four steps. So I
  368. 18:09will first tell you these four steps.
  369. 18:11And then I have created a data set here
  370. 18:13. A small toy data set. I will also
  371. 18:16show you how to run all those steps on
  372. 18:18it. So that you develop a visual
  373. 18:20understanding. Okay? Let's start. First
  374. 18:24of all, let's talk about step one.
  375. 18:26Before step one, in fact, let's talk
  376. 18:28about step zero. In step zero, you
  377. 18:31decide the value for min points and you
  378. 18:34decide the value for epsilon. Because
  379. 18:38these are both hyperparameters. Without
  380. 18:40these, our algorithm won't even be able
  381. 18:41to start. So, for a while, let's assume
  382. 18:44that our epsilon is this big. Like
  383. 18:47whatever this distance is. Okay? And
  384. 18:49let's assume min points is three. Okay?
  385. 18:53It is based on an assumption. Now let's
  386. 18:55move to step one. We have the value of
  387. 18:57min points. We have the value of
  388. 18:59epsilon. What do you have to do in step
  389. 19:01one? You have to identify all the
  390. 19:05points you have as either a core point,
  391. 19:09border point, or noise point. First of
  392. 19:13all, you have to do this. So, we cannot
  393. 19:15do it exactly, but try to do it in this
  394. 19:18data set by pausing the video. You have
  395. 19:21to label every point as a core point,
  396. 19:23border point, or noise point. So, if I
  397. 19:26talk about this one, I guess you can
  398. 19:29understand that since it is in very
  399. 19:31close proximity to the others, this
  400. 19:34seems like a core point; same for this
  401. 19:37one, same for this one, same for this
  402. 19:39one, and same for this one—these all
  403. 19:42look like core points. If I come here,
  404. 19:45this also looks like a core point. This
  405. 19:47also looks like a core point. This also
  406. 19:49looks like a core point. Okay? Now, if
  407. 19:53I talk about this point, this point
  408. 19:56seems like a border point, same for
  409. 19:59this one, and same for this one. And if
  410. 20:05I talk about these two points, this one
  411. 20:08and this one, it is very clear that
  412. 20:10these two are noise points. So, in this
  413. 20:14way, we have calculated our step one.
  414. 20:16Obviously, the algorithm will have to
  415. 20:17perform quite a few calculations to do
  416. 20:19this whole thing. But we did it based
  417. 20:22on intuition, and now each of our
  418. 20:24points is labeled as either a core
  419. 20:27point, a border point, or a noise point
  420. 20:29. Now let's move on to step number two.
  421. 20:33There are multiple steps in step number
  422. 20:35two. Okay? So how to do it? Look, what
  423. 20:39do we need to do? First of all, we need
  424. 20:43to pick up all the unclustered core
  425. 20:45points. Okay? Currently, nothing is
  426. 20:49clustered yet. All of them are
  427. 20:50unclustered. So let's assume this for a
  428. 20:52moment. Core points 1, 2, 3, 4, 5, 6, 7
  429. 20:56, 8, so we have to go to the
  430. 21:00unclustered core points. So let's say
  431. 21:03we went to one. And what do we have to
  432. 21:06do? We have to create a new cluster. So
  433. 21:08we will create a cluster around one.
  434. 21:12Okay? Now look, what is next? This is
  435. 21:15the main step. Now we have to add all
  436. 21:18the points that are unclustered and not
  437. 21:21part of any cluster. And are
  438. 21:24density-connected to the current point
  439. 21:26into this cluster. So one is the
  440. 21:29current point. We are moving to the
  441. 21:31next unclustered point, two. And we are
  442. 21:34just asking this question: are these
  443. 21:36two density-connected? Since both are
  444. 21:38core points and the distance between
  445. 21:40them is less than eps. So actually,
  446. 21:43these two are density-connected. So
  447. 21:45what will I do? I will take two and add
  448. 21:47it to this cluster. Then we will move
  449. 21:50on towards three. And we are asking
  450. 21:52this question. Are one and three
  451. 21:54density-connected? The answer is yes.
  452. 21:57Because two comes in between. Two is
  453. 21:58also a core point. And this distance is
  454. 22:01also less than eps. So we will extend
  455. 22:05this cluster. Then we will ask if one
  456. 22:08and four are density-connected. One and
  457. 22:11two are, two and three are. Three and
  458. 22:14four are too. Because three and four
  459. 22:16are both core points and the distance
  460. 22:18between them is less than eps. So we
  461. 22:20will make this a part of this cluster
  462. 22:22as well. Then we will ask if one and
  463. 22:25five are density-connected. Similarly,
  464. 22:27the distance between five and four is
  465. 22:29less than eps. So we will extend this
  466. 22:32cluster. Then we will ask what? Are one
  467. 22:35and six density-connected? Here is six.
  468. 22:38So the distance between five and six is
  469. 22:42greater than eps. So this connection
  470. 22:45will not be formed. And because of this
  471. 22:49, your loop will stop here. And this
  472. 22:53much will come into one cluster. Okay?
  473. 22:59Now we will run the loop again. What is
  474. 23:01being said in the loop again? For all
  475. 23:03the unclustered core points. Which ones
  476. 23:05are unclustered? Six, seven, and eight.
  477. 23:07So we will pick six and we will create
  478. 23:10a new cluster. In fact, let me use a
  479. 23:13different color. So this became the
  480. 23:16cluster for six. Now what will we do
  481. 23:18again? Add all the points that are
  482. 23:20unclustered and density connected. We
  483. 23:22will move towards seven. Guess what?
  484. 23:24Six and seven are both core points and
  485. 23:27are at a distance less than eps. So you
  486. 23:29will put both of them in the same
  487. 23:31cluster. Then I will ask, are six and
  488. 23:34eight density connected? The answer is
  489. 23:38again yes, eight and six are both core
  490. 23:40points and the distance between them is
  491. 23:42less than eps, so we will extend it.
  492. 23:45After this, there is no unclustered
  493. 23:47core point left, so the loop will end
  494. 23:49and this became my second cluster. Now
  495. 23:54let us come to step three. Since all
  496. 23:57our core points are exhausted. Now we
  497. 23:59will talk about border points. So it is
  498. 24:02written here, for each unclustered
  499. 24:04border point, assign it to the cluster
  500. 24:06of the nearest core point. This means
  501. 24:09that let's assume this is our first
  502. 24:12border point. This is our second border
  503. 24:14point, this is our third border point.
  504. 24:16We will find the nearest core point for
  505. 24:18every border point. And the cluster
  506. 24:21that core point is in, we will put our
  507. 24:23border point in that same cluster. So
  508. 24:25basically, this will happen. Okay?
  509. 24:30Similarly for two, you can see two is
  510. 24:33also closed by three, so we will do
  511. 24:35this. Okay? For three, it is closed by
  512. 24:41eight. Hence, we will put it in the
  513. 24:44green cluster. And in this way, all our
  514. 24:47border points are also exhausted. Now
  515. 24:49what is left? Noise points. What do we
  516. 24:52have to do with noise points? We have
  517. 24:54to leave it as it is. We have to leave
  518. 24:56them. We will let them be as they are.
  519. 24:58And this is the result of our
  520. 24:59clustering. We have two clusters formed
  521. 25:01. This is one, this is two, and there
  522. 25:04are two noise points. Okay? And you can
  523. 25:06see that this entire clustering that
  524. 25:08happened, on what basis did it happen?
  525. 25:11It happened based on density. And that
  526. 25:14is how DBSCAN works. Simple, elegant,
  527. 25:19beautiful. So, let's discuss how you
  528. 25:22can use and implement DBSCAN in sklearn
  529. 25:26. It is very simple. Anyway, sklearn
  530. 25:29makes everything easy. So, this example
  531. 25:31that I have picked has been taken
  532. 25:33directly from the sklearn website. Here
  533. 25:35we have taken some data points. Not
  534. 25:38many. There are about six data points.
  535. 25:40And I will plot them to show you. So
  536. 25:43here are those three points. Here are
  537. 25:45two and here is one. So I guess you are
  538. 25:47understanding. Roughly this will form a
  539. 25:49cluster. This will form a cluster and
  540. 25:51this is a noise point. Okay? So here
  541. 25:53you just don't have to do much. Create
  542. 25:55a DBSCAN object. You just have to
  543. 25:58provide two hyperparameters in it.
  544. 26:00First is epsilon which we discussed.
  545. 26:02Second is min samples which is your min
  546. 26:04points. So I have kept it three here,
  547. 26:08called db.fit and passed my X here. The
  548. 26:12good part about DBSCAN is that as soon
  549. 26:15as you train your model. After that,
  550. 26:19the labels attribute contains all your
  551. 26:23cluster results. So you can see when
  552. 26:27you are running this, just one second,
  553. 26:30as soon as you ran it. Then you are
  554. 26:35getting this. So there are six points.
  555. 26:38What did it do? It put the first three
  556. 26:40points in one cluster. It put these two
  557. 26:43points in another cluster. And what
  558. 26:45does DBSCAN do? Whenever there is a
  559. 26:48noise point, it assigns -1 to it. I
  560. 26:50hope you are understanding. I
  561. 26:52deliberately took a small example
  562. 26:53because it becomes easier to understand
  563. 26:55. Let me ask you a quick question. What
  564. 26:58if I change min samples from two to
  565. 27:01three here. What do you think will
  566. 27:03happen? Pause the video for a moment
  567. 27:05and try to think. I will show you by
  568. 27:07running the cell. This is the output.
  569. 27:10You can see only these three were added
  570. 27:13to one cluster. The remaining three
  571. 27:16points, both of these and this one too,
  572. 27:18were labeled as noise by our algorithm.
  573. 27:21Why? Because if you talk about these
  574. 27:23two points, they cannot have three
  575. 27:25points in their epsilon neighborhood
  576. 27:27because there are only two points here.
  577. 27:29So that is why these became noise
  578. 27:31points. They couldn't even become
  579. 27:33border points and they are definitely
  580. 27:35not core points. Okay? Similarly, what
  581. 27:39if I do this, that I change three to
  582. 27:42ten. Increase epsilon. Increasing
  583. 27:45epsilon means you are increasing the
  584. 27:47radius of that circle. Making the
  585. 27:49neighborhood larger. So what will
  586. 27:50happen? I guess again you would be able
  587. 27:52to understand. We added all five of
  588. 27:54these points into a single cluster.
  589. 27:57Because obviously, the cluster epsilon
  590. 28:00neighborhood of one point became so
  591. 28:03large that it reached here. You can see
  592. 28:05the scale is written here from zero to
  593. 28:0710. So you can vary epsilon and min
  594. 28:10samples a bit to check the results.
  595. 28:13Because these are hyper-parameters. So
  596. 28:15the control is in your hands. And here
  597. 28:17is a proper example where we took a
  598. 28:19dataset, which is a concentric circles
  599. 28:22dataset. And we applied DBSCAN on this
  600. 28:26dataset. And this is the result. I
  601. 28:29guess you can understand that it is
  602. 28:32impossible for K-means to cluster this
  603. 28:35kind of data. Right? It would
  604. 28:37definitely make a mistake. But the
  605. 28:38specialty of DBSCAN is that it doesn't
  606. 28:40care what shape and size of data you
  607. 28:42give it. It only cares about density
  608. 28:45and it is able to cluster. So, I will
  609. 28:48just give you this code in the
  610. 28:49description part. Go there and explore
  611. 28:52it. Change the parameters. Apply a
  612. 28:54different dataset and you would learn a
  613. 28:56lot. So guys, we have understood the
  614. 28:57DBSCAN algorithm. We have also seen it
  615. 28:59running through code. Let me summarize
  616. 29:01for you what the advantages and
  617. 29:03limitations of using DBSCAN are. So, if
  618. 29:06I talk about the advantages first, you
  619. 29:09must have noticed many advantages.
  620. 29:11Let's quickly discuss them. The very
  621. 29:15first advantage is that this algorithm
  622. 29:17is robust to outliers. Meaning this
  623. 29:20algorithm has a built-in capability to
  624. 29:23identify outliers. And you must have
  625. 29:26noticed that it can separately detect
  626. 29:27noise points. So, because of that,
  627. 29:30sometimes you also use it in anomaly
  628. 29:33detection type scenarios. So, a very
  629. 29:35good feature; K-means wasn't good at
  630. 29:38this. Another great thing and a
  631. 29:40headache that is reduced is that we
  632. 29:42don't have to specify how many clusters
  633. 29:44there should be. This algorithm figures
  634. 29:46it out by itself. In the example I gave
  635. 29:48you, two clusters were supposed to be
  636. 29:50formed, and DBSCAN figured out on its
  637. 29:52own that those two clusters would be
  638. 29:54formed. We didn't have to tell it like
  639. 29:57in K-means. Third is that it can find
  640. 30:00arbitrarily shaped clusters. You can
  641. 30:02give it any type of data. Give it this
  642. 30:05type of data. Or this type of data. It
  643. 30:07is able to perform clustering on it.
  644. 30:09Shape does not matter because its main
  645. 30:12idea is based on density. So that is
  646. 30:15why shape does not matter. And the best
  647. 30:18part is that, unlike some other machine
  648. 30:21learning algorithms where you have to
  649. 30:23remember many hyperparameters. Here you
  650. 30:27have to learn just two hyperparameters,
  651. 30:30and those are min points and epsilon.
  652. 30:33So, these are some of the best
  653. 30:35advantages I found for this algorithm.
  654. 30:38But it's not like it is the absolute
  655. 30:40best clustering algorithm. It has some
  656. 30:43flaws too, let's discuss those as well.
  657. 30:46So, a major weakness of it is that it
  658. 30:49is very sensitive to its
  659. 30:51hyperparameters. If you change the
  660. 30:54value of min points or epsilon even
  661. 30:56slightly, you will notice that your
  662. 30:58clustering starts to become very
  663. 30:59different. Very different clustering is
  664. 31:02formed. So this is one big problem,
  665. 31:05that you have to be super careful while
  666. 31:08putting in values for min points and
  667. 31:11epsilon. Right? The second problem is
  668. 31:15that if you give it data where you have
  669. 31:17two clusters. One like this and one
  670. 31:20like this. But the problem in both is
  671. 31:24that here it is a bit sparse. Meaning
  672. 31:26it is a bit spread out. Here it is a
  673. 31:29bit dense. Tightly packed. So, DBSCAN
  674. 31:32does not work very well on such
  675. 31:34clusters. Why? Because what have you
  676. 31:37done? You have created a single value
  677. 31:39for epsilon. Which you will apply to
  678. 31:41this and you will also apply to this.
  679. 31:44Now think for yourself, how will a
  680. 31:45single epsilon value suit these two
  681. 31:47clusters of different densities? So
  682. 31:50that is the problem. So if there is a
  683. 31:53scenario where the clusters you have
  684. 31:55have different densities, it doesn't
  685. 31:57perform very well. And the last
  686. 32:00drawback is that this algorithm cannot
  687. 32:03make predictions. In fact, there is no
  688. 32:07predict function here at all. It only
  689. 32:11performs clustering by running on the
  690. 32:14training data. It is only useful for
  691. 32:16clustering. It is not useful for
  692. 32:18prediction. Meaning if a new point
  693. 32:20comes tomorrow, it cannot tell if this
  694. 32:23new point will go here or there. And
  695. 32:25why does that happen? I guess you are
  696. 32:27able to understand. To understand where
  697. 32:29this new point will go. You would have
  698. 32:32to redo all the calculations again. You
  699. 32:34would have to create clusters all over
  700. 32:35again. You would have to train again,
  701. 32:37and that is why there is no prediction
  702. 32:40case here. Now, before ending the video
  703. 32:42, I want to show you a great resource.
  704. 32:45This is a website where you can see how
  705. 32:48DBSCAN works through visualization.
  706. 32:52There are many different types of
  707. 32:53datasets provided here. So, let's say I
  708. 32:55choose this one, the smiley face, and
  709. 32:58here you have set an epsilon value. You
  710. 33:01have set a min points value, which you
  711. 33:03can increase or decrease using these
  712. 33:04sliders. And once you click on go, it
  713. 33:07will actually show you the DBScan
  714. 33:09performance. You would actually be able
  715. 33:12to see how the DBScan algorithm works.
  716. 33:15How these circles move forward, add
  717. 33:18points to the cluster, how they stop,
  718. 33:21and how a new cluster is formed. You
  719. 33:24can visualize all of these things. And
  720. 33:27the best part is that you can change
  721. 33:29the epsilon and min points values to
  722. 33:32see what impact they have on how this
  723. 33:34algorithm works. So, when I was
  724. 33:38learning DBScan, I used this website
  725. 33:40quite a lot. In fact, whatever datasets
  726. 33:42they provided, they have given around 9
  727. 33:45or 10 datasets. I tested all of them. I
  728. 33:48wrote down my observations in a
  729. 33:50notebook and trust me, it helped me a
  730. 33:52lot to understand how DBScan works. So,
  731. 33:54this is a cool resource. I don't know
  732. 33:56how they made it, using JavaScript or
  733. 33:58whatever. But this is really, really
  734. 34:01awesome. So, that was the video, guys.
  735. 34:04I hope you liked the video. If you
  736. 34:06liked the video, please like it. Let me
  737. 34:08know in the comments how you found it.
  738. 34:10And if you haven't subscribed to the
  739. 34:12channel, please do subscribe. See you
  740. 34:14in the next video. Bye.

About this transcript

This page contains the full transcript of DBSCAN Clustering Algorithms | Density Based Clustering | How DBSCAN Works | CampusX by CampusX, generated from the public captions YouTube serves with the video. The transcript has 5,140 words across 740 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.