DBSCAN Clustering Algorithms | Density Based Clustering | How DBSCAN Works | CampusX — Transcript
Full transcript
- 0:00Hi guys, my name is Nitesh and you are
- 0:02welcome to my YouTube channel. In this
- 0:05video, we are going to cover a very
- 0:06important clustering algorithm called
- 0:08DBSCAN. I hope you must have heard of
- 0:11it. So, my plan for this video is to
- 0:14first give you the geometric intuition
- 0:16of this algorithm. After that, I will
- 0:18explain a small Python code and lastly,
- 0:20I will share a visualization tool with
- 0:22you so that your intuition becomes very
- 0:25solid. So this algorithm is pretty
- 0:26important. It is asked a lot in
- 0:28interviews. So I would recommend that
- 0:29you please watch this video until the
- 0:31end. Let's start the video. So before
- 0:33understanding DBSCAN, you need to know
- 0:35why there is a need to study DBSCAN. We
- 0:38had already studied a clustering
- 0:40algorithm, K-Means. It was a pretty
- 0:43good algorithm. Then why do we need to
- 0:45study a new clustering algorithm? We
- 0:48will address this first. So K-Means,
- 0:52our clustering algorithm, was very good
- 0:55but it had some flaws, and those flaws
- 0:59were such that they needed to be solved
- 1:02. I will tell you two or three flaws of
- 1:05K-Means so you understand why a new
- 1:07clustering algorithm was needed. So the
- 1:11first and the biggest flaw that comes
- 1:14with K-Means is that you have to
- 1:17specify or tell the algorithm how many
- 1:21clusters should be formed. This is a
- 1:26huge problem with K-Means, that before
- 1:28starting the clustering, you have to
- 1:29tell it how many clusters should be
- 1:31formed. Now imagine the data you have
- 1:35is in high dimensions. Let's say it is
- 1:3710-dimensional data. So you cannot plot
- 1:42it. Then basically, you won't even
- 1:44understand how many clusters should be
- 1:46formed. So we studied all these
- 1:49techniques where we tried to figure out
- 1:52the number of clusters by plotting the
- 1:54elbow method. But the problem with that
- 1:59is sometimes the elbow curve is very
- 2:01ambiguous. It's hard to understand what
- 2:04the correct value is. So for all these
- 2:07reasons, it becomes a bit difficult to
- 2:09work with K-Means. So this is reason
- 2:12number one. Reason number two is that
- 2:16K-Means is very, very sensitive to
- 2:19outliers. Because you are constantly
- 2:23calculating distances of every centroid
- 2:25with every data point. Now, if a data
- 2:29point turns out to be an outlier, like
- 2:31this particular data point. Then what
- 2:33will it do? It will shift all your
- 2:36centroids from their positions. Right?
- 2:40You can see here that all these
- 2:41centroids should have been at the
- 2:43center of their respective clusters.
- 2:44But because of this data point,
- 2:46everything shifted. Again, definitely
- 2:49not a good scenario. Right? Reason
- 2:53number three is the most important
- 2:55reason, because K-Means is a
- 2:57centroid-based clustering algorithm.
- 3:01Meaning, its entire mechanics revolve
- 3:03around centroid calculation. So, if you
- 3:07give K-Means some weirdly shaped data,
- 3:10K-Means starts struggling. And you can
- 3:14see this is one example where we gave
- 3:17it a dataset that was non-spherical,
- 3:20and K-Means clustered it completely
- 3:23incorrectly. You can see that. And this
- 3:27is true for many other types of
- 3:29datasets. Where it doesn't get
- 3:32spherical-type data. So, because of
- 3:35these three reasons, the machine
- 3:37learning community realized that
- 3:39K-Means alone wouldn't suffice. There
- 3:42are certain scenarios and datasets
- 3:45where K-Means performs well. But at the
- 3:48same time, there are many other
- 3:50scenarios where K-Means fails. And this
- 3:53is where a new requirement was created.
- 3:56And to solve that, who enters the
- 3:58picture? DBSCAN. There is one more
- 4:01thing you should know before studying
- 4:02DBSCAN, and that is what density-based
- 4:04clustering algorithms are. So, as you
- 4:08know, you can call K-Means a
- 4:10centroid-based clustering algorithm.
- 4:15Because here, the entire logic behind
- 4:17clustering comes through centroids. You
- 4:21create centroids, calculate distances,
- 4:23move points, and create new clusters.
- 4:26That is how K-Means works. So we call
- 4:28this centroid-based clustering. But
- 4:30DBSCAN follows a completely different
- 4:33style of clustering. And that style is
- 4:36called density-based clustering. In
- 4:39density-based clustering algorithms,
- 4:42you cluster your entire data with the
- 4:44help of density between data points.
- 4:47Let me try to explain it to you. Here
- 4:51you see some data, and you can see that
- 4:53roughly two clusters are forming in
- 4:55this data. But how do density-based
- 4:58clustering algorithms understand this?
- 5:01They see that this is a dense region.
- 5:05Meaning, in this particular region,
- 5:07there are a lot of data points. This is
- 5:10a slightly sparse region. Meaning,
- 5:14there is a lower number of data points
- 5:16here. And this is again a dense region.
- 5:21So, with the help of this sparse region
- 5:24, what can DBSCAN do? It can separate
- 5:28out the two dense regions. can create
- 5:30different clusters. So, this is the
- 5:32main idea behind density-based
- 5:34clustering. That what are you doing?
- 5:37You are performing clustering based on
- 5:39the density of data points. So, one
- 5:42good example is obviously, as I have
- 5:44told you so far, DBSCAN. In fact, if
- 5:48you want to know the full name of
- 5:49DBSCAN, the full name is Density-Based.
- 5:55Spatial Clustering of Applications with
- 6:01Noise, this is the full name. And trust
- 6:08me, by the end of this video, you will
- 6:10understand exactly why this is the name
- 6:12. Okay? Another good algorithm is
- 6:15OPTICS, which does density-based
- 6:17clustering. So, it was necessary to
- 6:20give you this introduction. Because if
- 6:22you don't even know how density-based
- 6:24clustering works? Then you won't be
- 6:26able to understand DBSCAN that well.
- 6:29Let me explain this to you quickly with
- 6:30an example. Suppose we have this
- 6:33dataset, and for a while, we assume our
- 6:36Epsilon value is 1 unit. 1 unit could
- 6:40mean 1 centimeter, it could be 1
- 6:42millimeter, or 1 kilometer. It does not
- 6:45matter. And we have assumed MinPts to
- 6:50be three. Okay? If it is three or more
- 6:55than three, only then will we call it
- 6:57dense. Otherwise, we will call it
- 6:59sparse. So, what do we have to do?
- 7:02Suppose we want to find out for this
- 7:04particular point if it is dense or
- 7:07sparse at this point. Then we first
- 7:10have to draw this 1-unit circle. So, I
- 7:14will quickly draw a circle. So, let's
- 7:16say this is the circle. Obviously, I am
- 7:18not drawing this with exact
- 7:20measurements. But let's say this is the
- 7:22circle. Okay? Now, in this circle, we
- 7:25will count how many points there are.
- 7:27So, we have 1, 2, 3, and four. In total
- 7:31, there are four points, which is
- 7:33greater than three. So, I will say that
- 7:36the density around this point is what I
- 7:39will call dense. Okay? Let's take
- 7:43another example. Let's talk about this
- 7:45point. So, for this point as well, we
- 7:48have to draw a circle. So, let's say we
- 7:51drew this circle. What is its radius as
- 7:56well? Which is 1 unit. Okay? Now, here
- 8:00too, we will count how many points are
- 8:03in this neighborhood, in this circle.
- 8:05So, here there is one point and two
- 8:07points. Since this is less than three,
- 8:10I can say that the density here is
- 8:13sparse. And in this way, I can
- 8:16calculate the density around any point.
- 8:21So basically, these min points and this
- 8:23epsilon help us calculate the density
- 8:26around any point. And fun fact, these
- 8:30are your two hyperparameters that you
- 8:34will need to tune while using this
- 8:37algorithm. That is why I told you that
- 8:40learning both of these is super
- 8:42important. Next, we will learn about
- 8:44three types of points that you
- 8:46encounter in DBSCAN. Which three points
- 8:48? The first is core points, the second
- 8:51is border points, and the third is
- 8:53noise points. And trust me, all three
- 8:55are very important with respect to
- 8:57DBSCAN. So you should know what is
- 8:59called a core point, what is a border
- 9:01point, and what is a noise point. The
- 9:04good part is that the concept is pretty
- 9:06simple. Especially if you have learned
- 9:09the concept of min points and the
- 9:11concept of epsilon. Okay? So with the
- 9:15help of this diagram, we will learn how
- 9:17to identify these three points. Let's
- 9:20talk about the core point first. So a
- 9:24core point is a point that has, in its
- 9:27epsilon neighborhood, more than the min
- 9:33points number of data points. I am
- 9:36repeating it once again. A core point
- 9:39is a data point whose epsilon
- 9:42neighborhood—epsilon neighborhood
- 9:45means this circle whose radius is
- 9:48epsilon, and the value of epsilon you
- 9:51provide as a hyperparameter—is a data
- 9:55point such that the number of data
- 9:58points in its epsilon neighborhood is
- 10:01greater than or equal to min points.
- 10:05Okay? So let's look at this diagram.
- 10:07Suppose we have taken the value of min
- 10:09points as five and the value of epsilon
- 10:11is some epsilon. So focus on this point
- 10:15. If you take this point and draw a
- 10:18circle of epsilon radius, you will get
- 10:21this red-colored circle. Now, what you
- 10:24need to do is count how many data
- 10:26points are in this red-colored circle.
- 10:28You have to count them. So you can see
- 10:311, 2, 3, 4, 5; since it is equal to the
- 10:36min points—not more, but equal—then
- 10:41this point becomes a core point. It is
- 10:46a simple concept. If the number of data
- 10:50points in the epsilon neighborhood of
- 10:52any data point is equal to or greater
- 10:54than the min points, you will call it a
- 10:56core point. I hope you understood. Now
- 11:01let's move on to the second type of
- 11:04point that you will see in DBSCAN, and
- 11:07we call that a border point. So a
- 11:12border point is a data point whose
- 11:15epsilon neighborhood has fewer than the
- 11:21min points number of data points. But
- 11:25it has at least one core point in its
- 11:28epsilon neighborhood. Listen carefully.
- 11:31It is very simple. There are two
- 11:33conditions. A border point is a data
- 11:36point that has fewer data points than
- 11:39min points in its epsilon neighborhood.
- 11:43But it must have at least one core
- 11:47point. For example, if you focus on
- 11:50this. You draw a circle of epsilon
- 11:53radius for this as well, which we call
- 11:55an epsilon neighborhood. And now you
- 11:58count how many data points are there. 1
- 12:01, 2, 3, which is less than five. So, it
- 12:06is not a core point. But in this entire
- 12:10neighborhood, there is this point which
- 12:12is a core point itself. So, both
- 12:15criteria are satisfied. And this data
- 12:17point becomes your border point. So,
- 12:20let me summarize it once again. A point
- 12:23that has fewer than min points in its
- 12:26epsilon neighborhood but has at least
- 12:30one core point is called a border point
- 12:34. Simple. Now let's talk about the
- 12:37noise point, which is the third type of
- 12:39point. So, the definition is very
- 12:41simple. If a data point is neither a
- 12:44core point nor a border point, then you
- 12:48call it a noise point. For example, for
- 12:53this data point, you drew a circle of
- 12:56epsilon radius which we call an epsilon
- 12:59neighborhood, and we counted how many
- 13:02data points are here. One, two, which
- 13:06is less than five. So clearly, it
- 13:10cannot be a core point. Now, it is not
- 13:14a core point, and there is no other
- 13:16core point in its epsilon neighborhood,
- 13:18so it won't be a border point either.
- 13:20So if it's neither a core point nor a
- 13:22border point, what is left? A noise
- 13:24point. And that’s the concept. If you
- 13:27talk in the context of a real dataset.
- 13:30Suppose you have a dataset like this,
- 13:33then you can easily identify the core
- 13:35points, border points, and noise points
- 13:37in this dataset. So, the points you see
- 13:42in the main area are your core points,
- 13:45which basically form the pattern of
- 13:48your data. The blue points on the
- 13:52periphery or the border are our border
- 13:55points. And the outliers in the distant
- 13:59areas are our noise points. So,
- 14:03tomorrow when you perform clustering on
- 14:05any dataset, you will see this kind of
- 14:08categorization between core points,
- 14:10border points, and noise points. Now,
- 14:13we just have to learn one final concept
- 14:16before learning DBSCAN, and that
- 14:18concept is density-connected points.
- 14:21Now, what are density-connected points?
- 14:23I will tell you in very simple words.
- 14:27If two points A and B are
- 14:29density-connected, it simply means that
- 14:33you can take both of them and put them
- 14:36into one cluster. That's it. Listen
- 14:41again. If two points A and B are
- 14:45density-connected, it simply means that
- 14:48you can put them both into one cluster.
- 14:53And that's the meaning of
- 14:54density-connected points. But the
- 14:58bigger question is, how do we know if
- 15:00these two points are density-connected?
- 15:04The answer to that is also very simple.
- 15:06Suppose you have two points, A and B.
- 15:13Now, if they are indirectly connected
- 15:17to each other via a lot of core points.
- 15:24And all of the adjacent pairs of core
- 15:28points are at a distance of less than
- 15:32or equal to Epsilon, then A and B have
- 15:36become density-connected points. I know
- 15:41it can be a bit confusing, but
- 15:43understand this. If A and B are
- 15:46indirectly connected to each other with
- 15:49the help of a lot of core points. And
- 16:00the specialty of all these core points
- 16:03is that you pick any two adjacent pairs
- 16:05among them. The distance between them
- 16:09will always be less than or equal to
- 16:12Epsilon. If this criteria matches
- 16:16throughout, then we will call A and B
- 16:22density-connected. When will this
- 16:25criteria fail? There are two cases. The
- 16:28first case is that any point in between
- 16:31is not a core point. It becomes a
- 16:33border point or a noise point. Then
- 16:36this path will break, and then A and B
- 16:39will not be density-connected. There
- 16:43could be a second case where they are
- 16:46both core points, but the distance
- 16:49between them is greater than Epsilon,
- 16:52so the path will break again, and A and
- 16:55B will not remain density-connected.
- 17:00You will have to put them into separate
- 17:02clusters. And that's the concept of
- 17:06density-connected points. So, I will
- 17:08summarize it once again. If two points
- 17:11A and B are density-connected, it
- 17:12simply means that you can put both of
- 17:14them into one cluster. How do we know
- 17:17if two points are density-connected?
- 17:19It's very simple. If those two points
- 17:22are connected by a sequence of core
- 17:24points and the distance between any
- 17:27adjacent pair in that sequence is less
- 17:29than or equal to Epsilon, then the two
- 17:32points we have are density-connected.
- 17:35If even one of these criteria fails,
- 17:37then those two points are not
- 17:39density-connected. So now we have
- 17:42studied all the concepts required to
- 17:44understand DBSCAN. We know Min Points.
- 17:46We know Epsilon. We know about three
- 17:49types of points. Border point, noise
- 17:51point, core point. And we also know the
- 17:54concept of density-connected points. So
- 17:56now we are ready to understand the
- 17:58DBSCAN algorithm. So guys, now I will
- 18:01explain the entire DBSCAN algorithm to
- 18:03you very well. This entire algorithm
- 18:06executes in a total of four steps. So I
- 18:09will first tell you these four steps.
- 18:11And then I have created a data set here
- 18:13. A small toy data set. I will also
- 18:16show you how to run all those steps on
- 18:18it. So that you develop a visual
- 18:20understanding. Okay? Let's start. First
- 18:24of all, let's talk about step one.
- 18:26Before step one, in fact, let's talk
- 18:28about step zero. In step zero, you
- 18:31decide the value for min points and you
- 18:34decide the value for epsilon. Because
- 18:38these are both hyperparameters. Without
- 18:40these, our algorithm won't even be able
- 18:41to start. So, for a while, let's assume
- 18:44that our epsilon is this big. Like
- 18:47whatever this distance is. Okay? And
- 18:49let's assume min points is three. Okay?
- 18:53It is based on an assumption. Now let's
- 18:55move to step one. We have the value of
- 18:57min points. We have the value of
- 18:59epsilon. What do you have to do in step
- 19:01one? You have to identify all the
- 19:05points you have as either a core point,
- 19:09border point, or noise point. First of
- 19:13all, you have to do this. So, we cannot
- 19:15do it exactly, but try to do it in this
- 19:18data set by pausing the video. You have
- 19:21to label every point as a core point,
- 19:23border point, or noise point. So, if I
- 19:26talk about this one, I guess you can
- 19:29understand that since it is in very
- 19:31close proximity to the others, this
- 19:34seems like a core point; same for this
- 19:37one, same for this one, same for this
- 19:39one, and same for this one—these all
- 19:42look like core points. If I come here,
- 19:45this also looks like a core point. This
- 19:47also looks like a core point. This also
- 19:49looks like a core point. Okay? Now, if
- 19:53I talk about this point, this point
- 19:56seems like a border point, same for
- 19:59this one, and same for this one. And if
- 20:05I talk about these two points, this one
- 20:08and this one, it is very clear that
- 20:10these two are noise points. So, in this
- 20:14way, we have calculated our step one.
- 20:16Obviously, the algorithm will have to
- 20:17perform quite a few calculations to do
- 20:19this whole thing. But we did it based
- 20:22on intuition, and now each of our
- 20:24points is labeled as either a core
- 20:27point, a border point, or a noise point
- 20:29. Now let's move on to step number two.
- 20:33There are multiple steps in step number
- 20:35two. Okay? So how to do it? Look, what
- 20:39do we need to do? First of all, we need
- 20:43to pick up all the unclustered core
- 20:45points. Okay? Currently, nothing is
- 20:49clustered yet. All of them are
- 20:50unclustered. So let's assume this for a
- 20:52moment. Core points 1, 2, 3, 4, 5, 6, 7
- 20:56, 8, so we have to go to the
- 21:00unclustered core points. So let's say
- 21:03we went to one. And what do we have to
- 21:06do? We have to create a new cluster. So
- 21:08we will create a cluster around one.
- 21:12Okay? Now look, what is next? This is
- 21:15the main step. Now we have to add all
- 21:18the points that are unclustered and not
- 21:21part of any cluster. And are
- 21:24density-connected to the current point
- 21:26into this cluster. So one is the
- 21:29current point. We are moving to the
- 21:31next unclustered point, two. And we are
- 21:34just asking this question: are these
- 21:36two density-connected? Since both are
- 21:38core points and the distance between
- 21:40them is less than eps. So actually,
- 21:43these two are density-connected. So
- 21:45what will I do? I will take two and add
- 21:47it to this cluster. Then we will move
- 21:50on towards three. And we are asking
- 21:52this question. Are one and three
- 21:54density-connected? The answer is yes.
- 21:57Because two comes in between. Two is
- 21:58also a core point. And this distance is
- 22:01also less than eps. So we will extend
- 22:05this cluster. Then we will ask if one
- 22:08and four are density-connected. One and
- 22:11two are, two and three are. Three and
- 22:14four are too. Because three and four
- 22:16are both core points and the distance
- 22:18between them is less than eps. So we
- 22:20will make this a part of this cluster
- 22:22as well. Then we will ask if one and
- 22:25five are density-connected. Similarly,
- 22:27the distance between five and four is
- 22:29less than eps. So we will extend this
- 22:32cluster. Then we will ask what? Are one
- 22:35and six density-connected? Here is six.
- 22:38So the distance between five and six is
- 22:42greater than eps. So this connection
- 22:45will not be formed. And because of this
- 22:49, your loop will stop here. And this
- 22:53much will come into one cluster. Okay?
- 22:59Now we will run the loop again. What is
- 23:01being said in the loop again? For all
- 23:03the unclustered core points. Which ones
- 23:05are unclustered? Six, seven, and eight.
- 23:07So we will pick six and we will create
- 23:10a new cluster. In fact, let me use a
- 23:13different color. So this became the
- 23:16cluster for six. Now what will we do
- 23:18again? Add all the points that are
- 23:20unclustered and density connected. We
- 23:22will move towards seven. Guess what?
- 23:24Six and seven are both core points and
- 23:27are at a distance less than eps. So you
- 23:29will put both of them in the same
- 23:31cluster. Then I will ask, are six and
- 23:34eight density connected? The answer is
- 23:38again yes, eight and six are both core
- 23:40points and the distance between them is
- 23:42less than eps, so we will extend it.
- 23:45After this, there is no unclustered
- 23:47core point left, so the loop will end
- 23:49and this became my second cluster. Now
- 23:54let us come to step three. Since all
- 23:57our core points are exhausted. Now we
- 23:59will talk about border points. So it is
- 24:02written here, for each unclustered
- 24:04border point, assign it to the cluster
- 24:06of the nearest core point. This means
- 24:09that let's assume this is our first
- 24:12border point. This is our second border
- 24:14point, this is our third border point.
- 24:16We will find the nearest core point for
- 24:18every border point. And the cluster
- 24:21that core point is in, we will put our
- 24:23border point in that same cluster. So
- 24:25basically, this will happen. Okay?
- 24:30Similarly for two, you can see two is
- 24:33also closed by three, so we will do
- 24:35this. Okay? For three, it is closed by
- 24:41eight. Hence, we will put it in the
- 24:44green cluster. And in this way, all our
- 24:47border points are also exhausted. Now
- 24:49what is left? Noise points. What do we
- 24:52have to do with noise points? We have
- 24:54to leave it as it is. We have to leave
- 24:56them. We will let them be as they are.
- 24:58And this is the result of our
- 24:59clustering. We have two clusters formed
- 25:01. This is one, this is two, and there
- 25:04are two noise points. Okay? And you can
- 25:06see that this entire clustering that
- 25:08happened, on what basis did it happen?
- 25:11It happened based on density. And that
- 25:14is how DBSCAN works. Simple, elegant,
- 25:19beautiful. So, let's discuss how you
- 25:22can use and implement DBSCAN in sklearn
- 25:26. It is very simple. Anyway, sklearn
- 25:29makes everything easy. So, this example
- 25:31that I have picked has been taken
- 25:33directly from the sklearn website. Here
- 25:35we have taken some data points. Not
- 25:38many. There are about six data points.
- 25:40And I will plot them to show you. So
- 25:43here are those three points. Here are
- 25:45two and here is one. So I guess you are
- 25:47understanding. Roughly this will form a
- 25:49cluster. This will form a cluster and
- 25:51this is a noise point. Okay? So here
- 25:53you just don't have to do much. Create
- 25:55a DBSCAN object. You just have to
- 25:58provide two hyperparameters in it.
- 26:00First is epsilon which we discussed.
- 26:02Second is min samples which is your min
- 26:04points. So I have kept it three here,
- 26:08called db.fit and passed my X here. The
- 26:12good part about DBSCAN is that as soon
- 26:15as you train your model. After that,
- 26:19the labels attribute contains all your
- 26:23cluster results. So you can see when
- 26:27you are running this, just one second,
- 26:30as soon as you ran it. Then you are
- 26:35getting this. So there are six points.
- 26:38What did it do? It put the first three
- 26:40points in one cluster. It put these two
- 26:43points in another cluster. And what
- 26:45does DBSCAN do? Whenever there is a
- 26:48noise point, it assigns -1 to it. I
- 26:50hope you are understanding. I
- 26:52deliberately took a small example
- 26:53because it becomes easier to understand
- 26:55. Let me ask you a quick question. What
- 26:58if I change min samples from two to
- 27:01three here. What do you think will
- 27:03happen? Pause the video for a moment
- 27:05and try to think. I will show you by
- 27:07running the cell. This is the output.
- 27:10You can see only these three were added
- 27:13to one cluster. The remaining three
- 27:16points, both of these and this one too,
- 27:18were labeled as noise by our algorithm.
- 27:21Why? Because if you talk about these
- 27:23two points, they cannot have three
- 27:25points in their epsilon neighborhood
- 27:27because there are only two points here.
- 27:29So that is why these became noise
- 27:31points. They couldn't even become
- 27:33border points and they are definitely
- 27:35not core points. Okay? Similarly, what
- 27:39if I do this, that I change three to
- 27:42ten. Increase epsilon. Increasing
- 27:45epsilon means you are increasing the
- 27:47radius of that circle. Making the
- 27:49neighborhood larger. So what will
- 27:50happen? I guess again you would be able
- 27:52to understand. We added all five of
- 27:54these points into a single cluster.
- 27:57Because obviously, the cluster epsilon
- 28:00neighborhood of one point became so
- 28:03large that it reached here. You can see
- 28:05the scale is written here from zero to
- 28:0710. So you can vary epsilon and min
- 28:10samples a bit to check the results.
- 28:13Because these are hyper-parameters. So
- 28:15the control is in your hands. And here
- 28:17is a proper example where we took a
- 28:19dataset, which is a concentric circles
- 28:22dataset. And we applied DBSCAN on this
- 28:26dataset. And this is the result. I
- 28:29guess you can understand that it is
- 28:32impossible for K-means to cluster this
- 28:35kind of data. Right? It would
- 28:37definitely make a mistake. But the
- 28:38specialty of DBSCAN is that it doesn't
- 28:40care what shape and size of data you
- 28:42give it. It only cares about density
- 28:45and it is able to cluster. So, I will
- 28:48just give you this code in the
- 28:49description part. Go there and explore
- 28:52it. Change the parameters. Apply a
- 28:54different dataset and you would learn a
- 28:56lot. So guys, we have understood the
- 28:57DBSCAN algorithm. We have also seen it
- 28:59running through code. Let me summarize
- 29:01for you what the advantages and
- 29:03limitations of using DBSCAN are. So, if
- 29:06I talk about the advantages first, you
- 29:09must have noticed many advantages.
- 29:11Let's quickly discuss them. The very
- 29:15first advantage is that this algorithm
- 29:17is robust to outliers. Meaning this
- 29:20algorithm has a built-in capability to
- 29:23identify outliers. And you must have
- 29:26noticed that it can separately detect
- 29:27noise points. So, because of that,
- 29:30sometimes you also use it in anomaly
- 29:33detection type scenarios. So, a very
- 29:35good feature; K-means wasn't good at
- 29:38this. Another great thing and a
- 29:40headache that is reduced is that we
- 29:42don't have to specify how many clusters
- 29:44there should be. This algorithm figures
- 29:46it out by itself. In the example I gave
- 29:48you, two clusters were supposed to be
- 29:50formed, and DBSCAN figured out on its
- 29:52own that those two clusters would be
- 29:54formed. We didn't have to tell it like
- 29:57in K-means. Third is that it can find
- 30:00arbitrarily shaped clusters. You can
- 30:02give it any type of data. Give it this
- 30:05type of data. Or this type of data. It
- 30:07is able to perform clustering on it.
- 30:09Shape does not matter because its main
- 30:12idea is based on density. So that is
- 30:15why shape does not matter. And the best
- 30:18part is that, unlike some other machine
- 30:21learning algorithms where you have to
- 30:23remember many hyperparameters. Here you
- 30:27have to learn just two hyperparameters,
- 30:30and those are min points and epsilon.
- 30:33So, these are some of the best
- 30:35advantages I found for this algorithm.
- 30:38But it's not like it is the absolute
- 30:40best clustering algorithm. It has some
- 30:43flaws too, let's discuss those as well.
- 30:46So, a major weakness of it is that it
- 30:49is very sensitive to its
- 30:51hyperparameters. If you change the
- 30:54value of min points or epsilon even
- 30:56slightly, you will notice that your
- 30:58clustering starts to become very
- 30:59different. Very different clustering is
- 31:02formed. So this is one big problem,
- 31:05that you have to be super careful while
- 31:08putting in values for min points and
- 31:11epsilon. Right? The second problem is
- 31:15that if you give it data where you have
- 31:17two clusters. One like this and one
- 31:20like this. But the problem in both is
- 31:24that here it is a bit sparse. Meaning
- 31:26it is a bit spread out. Here it is a
- 31:29bit dense. Tightly packed. So, DBSCAN
- 31:32does not work very well on such
- 31:34clusters. Why? Because what have you
- 31:37done? You have created a single value
- 31:39for epsilon. Which you will apply to
- 31:41this and you will also apply to this.
- 31:44Now think for yourself, how will a
- 31:45single epsilon value suit these two
- 31:47clusters of different densities? So
- 31:50that is the problem. So if there is a
- 31:53scenario where the clusters you have
- 31:55have different densities, it doesn't
- 31:57perform very well. And the last
- 32:00drawback is that this algorithm cannot
- 32:03make predictions. In fact, there is no
- 32:07predict function here at all. It only
- 32:11performs clustering by running on the
- 32:14training data. It is only useful for
- 32:16clustering. It is not useful for
- 32:18prediction. Meaning if a new point
- 32:20comes tomorrow, it cannot tell if this
- 32:23new point will go here or there. And
- 32:25why does that happen? I guess you are
- 32:27able to understand. To understand where
- 32:29this new point will go. You would have
- 32:32to redo all the calculations again. You
- 32:34would have to create clusters all over
- 32:35again. You would have to train again,
- 32:37and that is why there is no prediction
- 32:40case here. Now, before ending the video
- 32:42, I want to show you a great resource.
- 32:45This is a website where you can see how
- 32:48DBSCAN works through visualization.
- 32:52There are many different types of
- 32:53datasets provided here. So, let's say I
- 32:55choose this one, the smiley face, and
- 32:58here you have set an epsilon value. You
- 33:01have set a min points value, which you
- 33:03can increase or decrease using these
- 33:04sliders. And once you click on go, it
- 33:07will actually show you the DBScan
- 33:09performance. You would actually be able
- 33:12to see how the DBScan algorithm works.
- 33:15How these circles move forward, add
- 33:18points to the cluster, how they stop,
- 33:21and how a new cluster is formed. You
- 33:24can visualize all of these things. And
- 33:27the best part is that you can change
- 33:29the epsilon and min points values to
- 33:32see what impact they have on how this
- 33:34algorithm works. So, when I was
- 33:38learning DBScan, I used this website
- 33:40quite a lot. In fact, whatever datasets
- 33:42they provided, they have given around 9
- 33:45or 10 datasets. I tested all of them. I
- 33:48wrote down my observations in a
- 33:50notebook and trust me, it helped me a
- 33:52lot to understand how DBScan works. So,
- 33:54this is a cool resource. I don't know
- 33:56how they made it, using JavaScript or
- 33:58whatever. But this is really, really
- 34:01awesome. So, that was the video, guys.
- 34:04I hope you liked the video. If you
- 34:06liked the video, please like it. Let me
- 34:08know in the comments how you found it.
- 34:10And if you haven't subscribed to the
- 34:12channel, please do subscribe. See you
- 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.